I am working on a 2d platformer game. Right now, I'm also using ECS. My question is how can I manage player's and AI current states in the context of ECS when some states are exclusive and some may be combined.
At this moment, for the player entity I have several components: HorizontalSpeedComponent, VerticalSpeedComponent, CollisionComponent... and also a PlayerComponent that holds a player state flag. I also have several systems:
- PlayerInputSystem that maps player input to state
- PlayerWalkSystem that manages player horizontal speed while moving
- PlayerGravitySystem that manages player vertical speed while jumping/falling and also checks if player is on the ground.
- CollisionSystem that calculates collisions
- AnimationSystem that maps entity state to animation frame
- DrawingSystem that renders every entity
Player state flag represents following states:
- Idle
- Walking
- Jumping
- Falling
- Attacking
Now some of the states are exclusive: if player is Idle, then he is not doing anything else. But if player is walking (moving horizontally), then he may also be jumping(moving vertically up) and attacking. On the other hand, if player is jumping then he is not falling.
I thought about using a single Finite State Machine system to avoid having state checks and transitions mixed in other systems, but I do not know how it could be implemented it while keeping some states exclusive and some not.