Question

I am working on a 2d game in unity3d (c#) with the character controlled by the arrow keys and with 2 action buttons.

My problem is simple, I can solve it, but the solution seems bit dirty to me. I am using a very simple state machine, the hero is moving in horizontal or vertical way (floor and ladder)

my current state extending is:

HeroState -> HeroStateStatic -> HeroStateMovement

Then I extend static or movement to final states:

HeroStateMovement -> HeroStateRunning or HeroStateStatic -> HeroStateLookUp

however I am always calling a huge external function (in Hero class) which decides what to do next and then switch the state

I believe I should have this reasoning within the state itself but states at ladder has different set of checks and possible choices then states at floor and I don't know how to rework those classes to have all necessary code written just one (that's why now is in hero and not in the states) ... is there anything like cross-extending technique?

(I know how to use abstract classes and interfaces but it probably does not solve my trouble)

thanks for your help

Was it helpful?

Solution

Your problem can really be solved with a state machine, but since you have a huge class deciding what to do, it seems that you are having trouble implementing the state pattern.

This blog post has a good explanation about implementing the state pattern in an Unity application, and you can easily google for more resources.

Once I wanted to create a simple state machine that used methods as states, just for convenience (because the pattern use classes as state). One could use this as a way to have state machines inside states (which also can be achieved with the regular state pattern), so if you want to know about this you can read my stack overflow question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top