I'm working on a Zelda-like game (github), and I made a component-based entity system.
- I've a
Scene
class which holds astd::vector
ofSceneObject
. SceneObject
is a collection of componentsScene
uses systems, likeMovementSystem
,DrawingSystem
,CollisionSystem
to perform actions using components, or directly on components themselves. For example, this isMovementSystem
:#include "CollisionComponent.hpp" #include "MovementComponent.hpp" #include "MovementSystem.hpp" #include "PositionComponent.hpp" void MovementSystem::process(SceneObject &object) { if(object.has<PositionComponent>() && object.has<MovementComponent>()) { auto &positionComponent = object.get<PositionComponent>(); auto &movementComponent = object.get<MovementComponent>(); movementComponent.movement->process(object); movementComponent.isBlocked = false; if(object.has<CollisionComponent>()) { object.get<CollisionComponent>().checkCollisions(object); } movementComponent.isMoving = (movementComponent.vx || movementComponent.vy) ? true : false; positionComponent.move(movementComponent.vx * movementComponent.speed, movementComponent.vy * movementComponent.speed); movementComponent.vx = 0; movementComponent.vy = 0; } }
My problem is that I don't know how to handle states for characters.
For example, let's say Link has three states: Standing
, Moving
and Pushing
.
In each state, a different animation is played, so DrawingSystem
have to know about this, but I can't put it in a StateComponent
because all the objects which need a state don't always need an information on animation.
In each state, I must check for conditions to change the current state and process an action. For example, if Link's current state is Standing
, I must check if he's moving or not, to change his state to Moving
.
Which components/system do I need to implement this?