Many sources of movement in an entity system
- by Sticky
I'm fairly new to the idea of entity systems, having read a bunch of stuff (most usefully, this great blog and this answer).
Though I'm having a little trouble understanding how something as simple as being able to manipualate the position of an object by an undefined number of sources.
That is, I have my entity, which has a position component. I then have some event in the game which tells this entity to move a given distance, in a given time.
These events can happen at any time, and will have different values for position and time. The result is that they'd be compounded together.
In a traditional OO solution, I'd have some sort of MoveBy class, that contains the distance/time, and an array of those inside my game object class. Each frame, I'd iterate through all the MoveBy, and apply it to the position. If a MoveBy has reached its finish time, remove it from the array.
With the entity system, I'm a little confused as how I should replicate this sort of behavior.
If there were just one of these at a time, instead of being able to compound them together, it'd be fairly straightforward (I believe) and look something like this:
PositionComponent containing x, y
MoveByComponent containing x, y, time
Entity which has both a PositionComponent and a MoveByComponent
MoveBySystem that looks for an entity with both these components, and adds the value of MoveByComponent to the PositionComponent. When the time is reached, it removes the component from that entity.
I'm a bit confused as to how I'd do the same thing with many move by's.
My initial thoughts are that I would have:
PositionComponent, MoveByComponent the same as above
MoveByCollectionComponent which contains an array of MoveByComponents
MoveByCollectionSystem that looks for an entity with a PositionComponent and a MoveByCollectionComponent, iterating through the MoveByComponents inside it, applying/removing as necessary.
I guess this is a more general problem, of having many of the same component, and wanting a corresponding system to act on each one. My entities contain their components inside a hash of component type - component, so strictly have only 1 component of a particular type per entity.
Is this the right way to be looking at this?
Should an entity only ever have one component of a given type at all times?