I'm trying to create a Entity
for a game that uses both the Component and State design patterns. Let me explain.
The Components that make up an Entity
will consist of an InputComponent
, PhysicsComponent
, and a GraphicsComponent
(for now). Each Component will their own class to keep the clean nice and decoupled. This way you can implement your own components like for example a PlayerInputComponent
to represent player input, and then create an Entity
like this -> Entity player = new Entity(input, physics, graphics)
.
This system alone works really well for decoupling the code that makes up an Entity
. It makes the Entity
class flexible enough to accept all different types of components allowing for many variations. However, as stated in the question, I also want to use a State design pattern and I can't think of a way to make them coexist nicely.
The State design pattern is going to be used to represent a finite set of states that an Entity
can be in. For example, there would be a RunningState
, IdleState
, JumpingState
, etc... These states would be able to process input and update, deciding when to change states, and what state to change to. For example, if the movement keys are pressed in the IdleState
, the IdleState
would process this and decide to switch to a RunningState
. This makes keeping track of animations easy and separates out the logic for changing states into their own class avoiding complex logic statements.
My question is how can I mix both of these patterns so they work well together? I need all Components to be able to access these States because state transitions may occur in the InputComponent
or the PhysicsComponent
(for right now), and the states also have to be accessible in the GraphicsComponent
so I can draw the right frame for the current animation.
What's the best way to setup my Entity
class so it can implement both patterns and have them interact with each other without creating a mess in the Entity
class. Thanks!