Tuesday, April 14, 2009
Gaming helper Mini-Framework
I’ve been programming games for quite some time now. Every time I build a new game, there are many elements that keeps repeating … character behaviors like movement, jumping, ducking, chasing target etc. I know there are many game development suite that comes with many built in features & defining a character & behavior is rather very easy there … But (and its an important But! ) they come at a very high price, and there’s other problem of integrating with your code. So, I decided to build a mini-framework that would help me out with the mundane tasks (not to mention that I really got the kick out of this :) ). Here’s a simplistic view of the design approach (not all classes are included):
Next, I implemented XML serialization/de-serialization of above model. Here’s a Mario Character definition that worked for me:
Example 1: Mario
<?xml version="1.0" encoding="utf-8"?>
<SpriteXMLParser xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/XMLSchema.xsd">
<Identity>
<Name>Mario</Name>
<Group>Ram.Test</Group>
<Version>0.0.0.1</Version>
<Author>Ram Adhikari</Author>
<DateOfCreation>2009-04-28</DateOfCreation>
</Identity>
<States startState="NORMAL">
<State id="NORMAL">
<!-- NOTE: State ids are case sensitive,
Omitted other attributes because of lack of space -->
<Draw contentPath="mario-standingImage" />
</State>
<State id="STANDING">
<Draw contentPath="mario-standingImage" />
</State>
<State id="DUCK">
<Draw contentPath="mario-duckImage" />
</State>
<State id="RAISE-HAND">
<Draw contentPath="mario-raise-handImage" />
</State>
<State id="RUN1">
<Draw contentPath="mario-running-1Image" />
</State>
<State id="RUN2">
<Draw contentPath="mario-running-2Image" />
</State>
<State id="LEFT_RUN1">
<Draw contentPath="mario-running-1Image" />
</State>
<State id="LEFT_RUN2">
<Draw contentPath="mario-running-2Image" />
</State>
<State id="INVISIBLE" />
</States>
<Transitions>
<Transition fromStateId="NORMAL" toStateId="STANDING" onInput="mouse.in-click" />
<Transition fromStateId="STANDING" toStateId="NORMAL" onInput="mouse.out-click" />
<Transition fromStateId="STANDING" toStateId="INVISIBLE" onInput="keys.Escape" />
<Transition fromStateId="STANDING" toStateId="RUN1" onInput="keys.Right" />
<Transition fromStateId="STANDING" toStateId="LEFT_RUN1" onInput="keys.Left" />
<Transition fromStateId="STANDING" toStateId="DUCK" onInput="keys.Down" />
<Transition fromStateId="STANDING" toStateId="RAISE-HAND" onInput="keys.Up" />
<Transition fromStateId="INVISIBLE" toStateId="STANDING" onInput="keys.S" />
<Transition fromStateId="RUN1" toStateId="NORMAL" onInput="mouse.out-click" />
<Transition fromStateId="RUN1" toStateId="RUN2" onInput="keys.Right" />
<Transition fromStateId="RUN1" toStateId="STANDING" onInput="keys.Left" />
<Transition fromStateId="RUN2" toStateId="NORMAL" onInput="mouse.out-click" />
<Transition fromStateId="RUN2" toStateId="RUN1" onInput="keys.Right" />
<Transition fromStateId="RUN2" toStateId="STANDING" onInput="keys.Left" />
<Transition fromStateId="LEFT_RUN1" toStateId="NORMAL" onInput="mouse.out-click" />
<Transition fromStateId="LEFT_RUN1" toStateId="LEFT_RUN2" onInput="keys.Left" />
<Transition fromStateId="LEFT_RUN1" toStateId="STANDING" onInput="keys.Right" />
<Transition fromStateId="LEFT_RUN2" toStateId="NORMAL" onInput="mouse.out-click" />
<Transition fromStateId="LEFT_RUN2" toStateId="LEFT_RUN1" onInput="keys.Left" />
<Transition fromStateId="RUN2" toStateId="STANDING" onInput="keys.Right" />
<Transition fromStateId="DUCK" toStateId="NORMAL" onInput="mouse.out-click" />
<Transition fromStateId="DUCK" toStateId="STANDING" onInput="keys.Up" />
<Transition fromStateId="RAISE-HAND" toStateId="NORMAL" onInput="mouse.out-click" />
<Transition fromStateId="RAISE-HAND" toStateId="STANDING" onInput="keys.Down" />
</Transitions>
</SpriteXMLParser>
Now, it will be foolish to assume that such a simple model will work for every game character definition. So, what I do is to generate the class definition of a character from the XML (this reduces a lot of effort) & do the customization. It generate the boiler code for handling input (specific to the character) & state management.
Subscribe to Posts [Atom]
Post a Comment