Standing
A entity has only one state: stand. It is very easy to implement. It is quite common that after some period of inactivity entity performs some action. For example, it looks around, shuffle around, etc.
Our entity turns direction after some time of inactivity.
Looking
A entity has two states: moveTo, stand. An entity can move only in a circle, because we want to keep it at a specified location. The space in which it can move determine the circle radius.
States loop:
- Calculate the angle of movement
- Go to the boundary of circle
- Standing
- Go to the center of circle
Hen
A entity slowly moves across the surface. After contact runs. It has states: graze, run. Moving space is unlimited, but for border it can use the same technique as example above. After contact with the player it uses “easing” technique.
States loop:
- Short random movement
- Standing
- Short random movement
- If collision -> running
Conclusion
Using the states we can define any behavior for our entity. Such code is clear, understandable and reusable.
It might interest you
- HTML5 game cookbook - examples of code
- Dialogues Builder for video games