Need some advice on designing components

So after reading about components for a bit I want to make a very simple spaceshooter game that uses components and systems. However, I'm a bit confused on how much scope each component should have. For example, let's say the game had asteroids that fly around and if you hit them, you take damage. They also have health. The asteroid entity may then contain these components:

• SpriteComponent (Texture2D texture)
• HealthComponent (int amount)
• CollidableComponent (bool rigid)
• MovementComponent (int x, int y)
• RotationComponent (int degrees)

Now let's say we had a RenderSystem. Its Update() method would require SpriteComponent, MovementComponent, and RotationComponent to draw an entity.

That's all fine and good but when would have MovementComponent and not RotationComponent? Couldn't we just have a TransformComponent that contains the x, y, and rotation? It feels a little weird designing these components to only be a container of one data-type. But then where do you draw the line? All sprites also have a position and rotation amount, so should the SpriteComponent contain the TransformComponent, or should the members of TransformComponent be moved to SpriteComponent?

But then the problem is if you wanted an entity with a position and rotation but no sprite. Then you either have to give it an empty texture (not necessarily a bad solution) or maintain TransformComponent and just make SpriteComponent contain it as well (or just pass both).

There's a lot of decisions to be made and I really feel similar to when I began object-oriented programming. "What should be objects? What should its methods be? Should this be public or private?" Obviously I've figured all that out now but this new component-based paradigm is giving me these same problems now. Does anyone have any advice or standards for component-based design for typically used components and how one normally would set something like this up?

Couldn't we just have a TransformComponent that contains the x, y, and rotation?

Yes. This is exactly what the Unity Transform component does.

It feels a little weird designing these components to only be a container of one data-type. But then where do you draw the line?

You don't have one data type. You have one responsibility.

so should the SpriteComponent contain the TransformComponent, or should the members of TransformComponent be moved to SpriteComponent?

The way Unity does it, is that all components are contained by the GameObject itself. Components don't "contain" other components, but they can have references to them.

All sprites also have a position and rotation amount..

There are two things Unity does to handle this.

1) All game objects innately have a Transform component.

2) When authoring game objects, you can specify that certain components require the game object they're attached to to have other components. They do this through their RequireComponent attribute. You can do things like say "when this component starts up, and the game object it's attached to doesn't have my required components, throw an error".

• Say "Unity" one more time... but true, I can think of much worse examples. – Engineer Oct 26 '11 at 19:31

I would suggest to read the following post from StackOverflow (if you already haven't), because it has lots of information and references to Components documentation.

Component Based Game Engine Design

As previously said, entities (or GameObjects) contain components. And components do not contain other components but references to them. I hope this helps! ;)

That's all fine and good but when would have MovementComponent and not RotationComponent? Couldn't we just have a TransformComponent that contains the x, y, and rotation?

There is no right answer to this question as it depends on the level of granularity you choose for a specific project. TransformComponent is very typical as far as I can judge. However for your test project you might want to have a PositionComponent for in-game GUI.

All sprites also have a position and rotation amount, so should the SpriteComponent contain the TransformComponent, or should the members of TransformComponent be moved to SpriteComponent?

Neither. In first case some components will become semi-entities which is an unnecessary over-complication. In second case you will have to add similar Transform into elements that need spatial info but are not visible (e.g. some AI or physics related stuff).