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:
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
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#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
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
How can I solve this problem? What is usually done?