Friday, April 19, 2013

Game Entity controlled states

In this post I'll show how to create a game entity controlled states.


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.


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:
  1. Calculate the angle of movement
  2. Go to the boundary of circle
  3. Standing
  4. Go to the center of circle


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:

  1. Short random movement
  2. Standing
  3. Short random movement
  4. If collision -> running


Using the states we can define any behavior for our entity. Such code is clear, understandable and reusable.

It might interest you