Original Post
After reading the Cowboy Programming article concerning component-based entity systems, I decided to implement my own for a small 2D game. This kind of design is probably overkill for such a simple project but I think it is a fun exercise and will help me enforce modular software design. In addition to the usual references on the subject I have also read Sneftel's thread, weighting the pros and cons of each method presented. I came up with the following requirements for my architecture:
- Type-safety must be preserved (no dynamic_cast<> or worse, static_cast<>).
- Components should be as independent as possible.
- Component and entity creation should be doable dynamically at run time in order to facilitate the integration of a contents editor.
- Communication between components of a same entity should not be done using packed (serialized) events and giant switch cases to determine their type (similar to point 1).
- Entities should be nothing more than a name common to several components (data and behaviour should be factored in components).
- Type-safety is preserved throughout the entire system. There is no casting nor RTTI used anywhere in the system.
- Components are completely independent. They subscribe to a Mediator for events they can handle and wait for them to occur. If no component can fire those specific events, the system still works without any problem.
- Entities (components) can easily be created at run time. The integration of a content editor will be a trivial task.
- Entities can only have one component of each type. This could easily be solved by using a mulimap instead of a map to store components in each subsystem.
- The mediator object can quickly become a monolithic object. This is the main problem I have with this design, but I think it's a fair compromise for the advantages it offers (type-safety, component independence and dynamic entity creation).