What have you found for these years?

2009-08-21

oop without state with fp (2)

能力暫定這些... 我一直在做重複的事 @@
Property("Health" -> 100, // hp
"Mana" -> 50, // mp
"Energy" -> 50, // resource
"Vigor" -> 50, // action point
"Strength" -> 50, // p-atk
"Endurance" -> 25, // p-def
"Imagination" -> 10, // m-atk
"Will" -> 10, // m-def,
"Agility" -> 15)) // decides speed in vm
不過想把這轉成 Footman - Health(10) ...
似乎不是很容易 @@ 還要想想看怎麼做比較漂亮。

*

上面改成這樣試試...
abstract class Property
case class Health(val pt: Int) extends Property // hp
case class Mana(val pt: Int) extends Property // mp
case class Energy(val pt: Int) extends Property // resource
case class Vigor(val pt: Int) extends Property // action point
case class Strength(val pt: Int) extends Property // p-atk
case class Endurance(val pt: Int) extends Property // p-def
case class Imagination(val pt: Int) extends Property // m-atk
case class Will(val pt: Int) extends Property // m-def
case class Agility(val pt: Int) extends Property // decides speed in vm

type State = (Health, Mana, Energy, Vigor,
Strength, Endurance, Imagination, Will,
Agility)

我想 static typing 又不支援 meta-programming 的話,
重複成這樣也許是逃不掉的。不過反正這都是靜態的,
沒視野不會亂動,因為這會影響整個設計,不可能亂改。

*

有點不知道在寫什麼了 @@
基本上 State 維持像上面的結構,但是變成:
case class State(...) extends (Health, ...)(...)
也就是說,繼承了上面定義的 tuple,
因為我需要擴充一些 method 到這個 tuple 裡面。

因此現在跑這個:

println(Footman - Health(10))

結果是:

Unit(Footman,List(Fire(100)),
(Health(90),Mana(10),Energy(50),
Vigor(50),Strength(40),Endurance(20),
Imagination(10),Will(10),Agility(15)))

println(Footman.state.agility) // Agility(15)

不過有個問題...
Footman.state.agility - 10 // 5
Footman.state.agility - Agility(10) // 5
Footman.state.agility - Health(10) // 5
先不論回傳是 Int 很怪,Agility - Health 還能成功更怪 @@
做成這樣是因為想避免重複,但... 再試試看好了。
理想上當然就是做成不能亂減,而且不能使用 Int.

也要考慮未來換 Float 的可能...

0 retries:

Post a Comment

Note: Only a member of this blog may post a comment.



All texts are licensed under CC Attribution 3.0