No, I think your components should be as generic and reusable as possible. And the examples you gave certainly don't need specific components. Here's an example of the type of components you could be thinking about:
- TransformComponent - Has as
Position
in the world.
- SpriteComponent - Has a
Sprite
and renders it at TransformComponent.Position
- MovableComponent - Has a
Velocity
and updates TransformComponent.Position
- BoxComponent - Has a
Rectangle
and detects collisions with other BoxComponents
Then, all of these have parameters that will vary from entity to entity.
For example, and still thinking about the ball:
- The
TransformComponent
could store the ball's initial position
- The
SpriteComponent
could keep a reference to the ball sprite
- The
MovableComponent
could store the ball's initial velocity.
- The
BoxComponent
could store the ball size and have a special handler for its collision event
Then your paddles might also have some of these components, but they will probably have different initial parameters for each of them, and this is what will make them unique and different from the ball entity. A good component for them might be an InputComponent
that handles user input and updates the TransformComponent
when the player presses the up or down keys.
PS: If you have never tried Unity3D, I think it's a good place to start wrapping your head around these concepts!
SpriteComponent
usingTransformComponent.Position
. How would you suggest OP handle the coupling between them? – Mike Cluck Mar 03 '12 at 02:41GetComponent(Type)
method. Then on initialization, theSpriteComponent
could simply callentity.GetComponent(tyepof(TransformComponent))
and store it locally. Then use that reference when rendering. – David Gouveia Mar 03 '12 at 02:55TransformComponent
might know how to react to aGetPosition
message by returning its internal position, and theSpriteComponent
would simply send a message of that type through the entity when it required the position. – David Gouveia Mar 03 '12 at 03:00SpriteComponent
does not know about theTransformComponent
. All it requires it that there exists some component capable of handling theGetPosition
message, but without specifying which one. – David Gouveia Mar 03 '12 at 11:38GetPosition
message. – David Gouveia Mar 03 '12 at 14:07TransformComponent
once at initialization. I would store a reference to that component inside theSpriteComponent
and all further operations would be done with that reference. I could also assert on initialization if the component is not found. As for how to query for a component, I think a simpleDictionary<Type, Component>
on the entity would be enough. It's definitively not perfect and requires entities to be set up properly but, and this is just as a personal opinion, I think it would be enough for any of my projects – David Gouveia Mar 03 '12 at 14:18