0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ The splitting of complex behaviours into small chunks each managed by its own system helps a lot when you have hundreds of entities that might all use different combinations of those chunks. But for the player, you usually only have a handful of them, using basically the same configuration. So it's probably not meaningful to separate the PlayerWalk and Gravity systems — if you ever have a player type that walks but has no gravity, or has gravity but doesn't walk, you can handle that with some flag/coefficient data. Split behaviour between systems when it makes things easier, not harder. \$\endgroup\$ – DMGregory Aug 14 '20 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.