Heh - yeah, I hear you. This topic is a complex one - I read some articles at gamedev.net and various other sources before even getting an idea on how this could work for us. That is to say, the most articles are about component systems which handle everything, from rendering to physics implementations and all that other stuff which happens behind the scences. I was lucky that a good part of that stuff is taken care by
FIFE, our game engine.
So in our case, we just have to munge game data with FIFE instances (those pesky little things that move around the map :D), which saves us quite some complexity.
I'll try to give you a before-after description - maybe that helps a bit:
Before, the implementation was like this:
- Agent class (basic connection between FIFE & Zero)
- Hero class (player character 'controller')
- NPC class (npc 'controller')
- Container class (non-living objects which have an inventory)
- Light class (a light surprise! :D)
So there were major classes which could do anything, like Hero & NPC, and both of them (!) had a slightly different implementation for basically the same stuff. An NPC has attack() - and the Hero has attack() too - code duplication at it's best. :/ If I wanted to add a new feature like e.g. sneaking, I had to implement that in both classes.
Now if you want to make a container have a Light - or the NPC or every other interactive Instance - it basically meant to copy the Light implementation and adjust it. Teh horror.
Now the implementation looks like this:
- ZeroInstance (component controller)
- ZeroAction (transition layer between FSM states and FIFE actions)
- FSM (finite state machine)
- LightSource
- DoorProperty
- LockProperty
- AI (controller, switches between 'Agenda AI' and 'Combat AI')
- Ruleset (controller)
Now there is only one implementation, which makes sure to always behave the same. If I tell a 'light' to join a combat, the CombatManager can control it because it has the same interface a "npc" has, without writing extra code.
So the
main idea about the concept "component based game entities" is - forget about NPC class - or Raider class - or Ship class. The implementation details heavily depend on your code / type of game / engine. I still have not crasped the idea in a whole - because I have a hard time right now to redesign the Hero class to make it fit into the system as well. The player has a lot more interfacing going on with the rest of the game code then any other game entitiy ever will have.
My approach is rather basic - as I 'only' cover game entities which have a direct representation on the map.
Unknown Horizons (a project using FIFE, too) for example extends this design even to their gui and various other areas to make the complete game extendable for non-programmer contributors.
Davaris
About looking at our code: we are open source, but not yet (no release yet - which makes us closed source for some people out there) - but PM me and I'll give you a sneak peek if you want.