I'm making a game that plays a bit like Diablo, but you can harvest resources like in an RTS.
I have the following states in my FSM: IdleState
, ApproachingTargetCoordinateState
, ApproachingTargetEntityState
, CuttingTreeState
.
When a user clicks a tree, Player.targetEntity
is set to the corresponding Tree
instance (which inherits from Entity
) and the player's state is set to ApproachingTargetEntityState
. When the player reaches the tree, a transition is made to CuttingTreeState
. Shortly after, the tree is cut down, and state is changed back to IdleState
.
At this point, I'd like the Player.targetEntity
reference to be removed because its no longer needed. Where should I put the logic that does this?
I could do this in the CuttingTreeState#exit()
method but that would only work in situations where everything happens as described above. If for instance the user clicks a random coordinate (not a tree) before the player reaches CuttingTreeState
, CuttingTreeState#exit()
is never run and so Player.targetEntity
is never removed.
I can't put it in ApproachingTargetEntityState#exit()
either, as that would remove the reference to the tree before we get to CuttingTreeState
.
Another solution would be to just not care about Player.targetEntity
. But it just feels wrong to me that the attribute is set for example when the player is in the IdleState
.
How can I solve this problem? What is usually done?
IdleState
? \$\endgroup\$ – Nathan Reed Aug 5 '13 at 17:59ApproachingTargetCoordinateState
for a long time before hittingIdleState
, so clearing it inIdleState
is only a half solution I think. Also, if you consider the concept of "separation of concerns",IdleState
ideally shouldn't know anything about trees. \$\endgroup\$ – Rasmus Rønn Nielsen Aug 5 '13 at 18:15