Ben's blog
Software development, games, and all that jazz
Home
Archive
Posts
Pages
Search:
Edit Post
Title:
Body:
<p>One of the things I've been doing recently is transitioning from an inheritance-based to a component-based game object system. I started the project with full knowledge that component-based game object systems are better, but intentionally didn't implement one since I thought it would be unnecessary for such a small game. It turns out that I was wrong, and since it's something I'm very interested in and have thought about a lot, and since something similar was going on at work, I thought I'd give it a go.</p> <p>Originally I had specific classes (Building, SmallHouse, Person) that handle everything about themselves (like graphics, physics, game logic) and do all their processing in a big 'update' method. I've factored out and moved around this code through several stages, keeping it building and running along the way, as it's dangerous to just rip it out and start again. Now I'm at a stage where these classes have been replaced by a GameObject class, which owns a collection of Components, whose subclasses like PhysicsComponent and GraphicsComponent are registered with and updated by PhysicsSystem and GraphicsSystem respectively. GameObjects can be created by name from a GameObjectFactory, and object types can be created and registered at runtime, or from an XML file. So I now have an 'objects.xml' file that looks a bit like this:</p> <p><code lang="xml" escaped="true" tab_size="1" nowrap="true"> <objects> <object name="SmallHouseDebris"> <component type="Graphics"> <property name="meshName" type="string"> smallHouse.mesh </property> <property name="translationOffset" type="Vector3"> 0 0 0 </property> <property name="scale" type="Vector3"> 1 1 1 </property> <property name="rotationOffset" type="Quaternion"> 1 0 0 0 </property> </component> <component type="Physics"> <property name="size" type="Vector3"> 3.8 3.8 3.8 </property> <property name="translationOffset" type="Vector3"> 0 2 0 </property> <property name="contactGroup" type="unsigned int"> 2 </property> </component> <component type="DestroyAfterTimeBehaviour"> <property name="timeUntilDestruction" type="float"> 10.0 </property> </component> </object> </objects> </code></p> <p>What a difference! If you aren't impressed by that, you're either not a programmer, or you're a programmer in a field a bit less backwards than video games.</p> <p>Now, I told a bit of a fib; actually the only class I've completely removed is 'Debris', since changing everything at once would have involved a lot of copying and pasting, as a lot of the intermediate steps were actually quite ugly and verbose. But now that I've proven the system works for Debris, I can get rid of all the other 'game object' classes. Then the only C++ code I write for game objects will be for their game logic, if that can't be reused from existing components.</p> <p>As such, the visible results you can expect from this are a variety of interesting new buildings some time soonish!</p>
Tags:
cities-in-the-sky
Body markup:
Plain Text
Textile
HTML
Markdown
ReStructuredText
Draft: