I'm working on a 2d RPG game in C# using an Entity-Component-System architecture, and I'm currently working on movement. Right now, I'm wondering how to organise the code so it looks a bit more.. pretty, or dignified, than it is. Currently, I have four components:
VelocityComponent - Contains the speed (velocity) of an Entity. The behaviour currently is that if an Entity does not contain a VelocityComponent, then we continue onto the next Entity, because we cannot move if we haven't a velocity.
TransformComponent - Contains the Width, Height, X and Y fields for the Entity. Basically, the position. Similar to VelocityComponent, if an Entity does not have a TransformComponent, then the GPSSystem just continues the next iteration, because we cannot move anywhere if we don't have a position.
MovementComponent - Contains a pair of co-ordinates for use as a destination position, and a boolean indicating if the Entity is currently moving.
MoveFromKeyboardComponent - Signals whether or not a key is pressed so that the Entity that this Component is attached to should move or not.
The update loop currently goes that the GPSSystem collects a list of all entities (by accessing the EntityManager. The EntityManager simply contains a key-value pairing of Guid to Entity of all active entities. All systems have access to it), then iterates over the whole lot.. It'd be better if I just posted the code;
/// <summary>
/// Update the GPS System.
/// </summary>
/// <param name="gameTime"></param>
public override void Update(GameTime gameTime)
{
// Handle the movement.
foreach (Entity entity in entityManager.GetEntities())
{
VelocityComponent velocity = (VelocityComponent)entity.GetComponent("Velocity");
MovementComponent movement = (MovementComponent)entity.GetComponent("Movement");
TransformComponent transform = (TransformComponent)entity.GetComponent("Transform");
MovementFromKeyboardComponent movementFromKeyboard =
(MovementFromKeyboardComponent)entity.GetComponent("MovementFromKeyboard");
// can't do any movement manipulation without a transform (position) component.
if (transform == null || velocity == null)
continue;
}
base.Update(gameTime);
}
However, I am unsure of how to proceed after this point. I just don't know how to manage the movement. I am aiming to have a movement style similar to Pokémon, where all Entities move one square (16 x 16) at a time and have smooth movement across those tiles, however the player can hold down a button and keep moving square after square, and when he releases that key, he stops moving (and snaps to the nearest tile if he is in the middle of two).
So, on to the question: Am I going about this correctly? Have I got too many (or too few) components for movement? And lastly, how would I manage the fact that an Entity may have a MovementComponent and a MoveFromKeyboardComponent? I'm just confused I suppose. I am not looking for the code for that, I am just wondering whether I am going about this correctly? This is my first whack at an entity component system, you see.
Best regards.
EDIT 1: I should add I have now changed the structure of the System class, such that it now has a ComponentAdded and ComponentRemoved method; that is, we no longer iterate over every Entity in existence every Update(), instead we store the entities that have components we want to listen to in a data structure in the class. My question remains the same, though.