I've always wanted to implement a component based system since I saw one my friends at Uni create one for his third year project. So I did some research into what the actual concept and the general design for a component based system is and it turns out it seems to change from person to person. The theory somewhat stays the same in the fact that instead of having a hierarchical design ( i.e. Base Class Pet, with derived classes of Dog and Cat ) it has a design based heavily on the Has-A relationship ( i.e. Entity Has a Bark Component ). There are many advantages for and against the component based approach and it really is a case of suiting the design to each specific game. The main reason I leant towards this is essentially for the re-usability, as once you have written a renderable component then in the next game that just needs to be included into the project and away you go.
My Component Design
The Main Loop
while( gameIsRunning )
{
ClearMessages();
HandleInput();
UpdatePhysics();
UpdateEntities();
}
- The Messages for all the Entities from last loop are cleared.
- Input is taken from the keyboard and a message is fired to the appropriate entities. (i.e. MoveMessage).
- The Physics is calculated and the Entities matrices are updated.
- The entity update loop starts, each entity runs its components in order of priority, passing in the list of messages which that entity has received.
- That component then goes through all the messages getting the information out of the messages that it knows about and then using that information to change its own or the shared data in some way.
- The component that is ran last is always the Render Component to ensure that no transformations are made after it has been rendered.
This is only my initial design and will more than likely change but for now this is working and allows the flexibility and reuse I have been wanting.
Cheers :)