Factory pattern and such

Published January 18, 2009
Advertisement
So, did a few things of interest today:
wrote a system for loading keymappings from file. The number of config files my game will use is rapidly growing. the file looks like this:


KEYBOARD
w FORWARD
s BACK
a LEFT
d RIGHT
r RELOAD
e ACTION1
f ACTION2
1 WEAPON_1
2 WEAPON_2
3 WEAPON_3
4 ITEM_1
5 ITEM_2
6 ITEM_3
7 ITEM_4
8 ITEM_5
SHIFT SPRINT
C CREEP
/KEYBOARD


This lets me have more than one keymapping, and lets users customise their keymappings with little effort from myself. I could provide an in-game interface later if I wish.

refactored the system so the world is spawning objects and the scene is only aware of what to draw. Thusly, each entity has different and unrelated data structures within each system, and systems communicate between each other via event queues. This loose coupling makes refactoring easy

Created a factory system for spawning a "Thing" from a prototype, just by its name. This loads from a text file like this:

PROTOTYPES
PROTOTYPE NAME "TV" WEIGHT 35.0 MODEL "TV" SCRIPT "TV" /PROTOTYPE
PROTOTYPE NAME "broom" WEIGHT 2.0 MODEL "broom" SCRIPT "broom" /PROTOTYPE
PROTOTYPE NAME "safety_stick" WEIGHT 4.0 MODEL "safety_stick" SCRIPT "safety_stick" /PROTOTYPE
PROTOTYPE NAME "bench" WEIGHT 150.0 MODEL "bench" SCRIPT "chair3" /PROTOTYPE
PROTOTYPE NAME "street lamp" WEIGHT 300.0 MODEL "street lamp" SCRIPT "street lamp" /PROTOTYPE
PROTOTYPE NAME "cine_camera" WEIGHT 45.0 MODEL "cine_camera" SCRIPT "cine camera" /PROTOTYPE
PROTOTYPE NAME "street lamp 2" INHERIT "street lamp" MODEL "street lamp 2" /PROTOTYPE
/PROTOTYPES


The advantage here is that some objects will be able to inherit the properties of another object, and then simply override the ones that are different. Note that "street lamp 2" inherits street lamp but has a different model. The rule here is that an object can inherit the properties of an object that appeared above it in the file. Very simple but creates lots of opportunities.

I can spawn an object from this system at any time by just having the world send an "this object spawned" event, and the other systems will instantiate the appropriate object within their own ranks to match it.

My interpretation of the factory pattern is quite literal; I have an actual object being the factory, and this object creates the prototypes based on the data in a file, such as the one above. Each prototype knows how to instantiate an object with those properties.

The factory knows how to spawn only one class of object, so for each type of entity I have a different factory object.

Next weeks objective: Have characters spawning from a factory, and moving around the world, represented by a piece of debug geometry.
Next Entry Change of plans
0 likes 4 comments

Comments

benryves
Looks good, but is WASD as a default a sensible option? Not everyone uses QWERTY layouts; surely the cursor keys are a better default?
January 18, 2009 04:41 PM
speciesUnknown
as a default layout i think yes, it is most sensible. its what pretty much every game uses and is a de-facto standard. it leaves the left hand and the right spaced in an ergonomic fashion, compared to the arrow keys which put them at an awkward angle.

i would be very please if you were to write me a key map file for other keyboards, like your azwerty KB.
;)
January 18, 2009 05:52 PM
benryves
Quote:Original post by speciesUnknown
as a default layout i think yes, it is most sensible. its what pretty much every game uses and is a de-facto standard. it leaves the left hand and the right spaced in an ergonomic fashion, compared to the arrow keys which put them at an awkward angle.
I'm not saying WASD is bad as such, I'm saying that defaulting to WASD - when those keys may be all over the place on non-QWERTY keyboards - could be problematic. [wink]
Quote:i would be very please if you were to write me a key map file for other keyboards, like your azwerty KB.
Which is far too much hassle, and why I just default to the cursor keys which will work on every layout. That said, I have no idea what most games do these days.

Is your text format based on an existing standard, or something you invented yourself? It looks like a trimmed down XML. [smile]
January 18, 2009 07:30 PM
speciesUnknown
WASD is what most gamers expect, and the point of having an external file that can be edited is that users of non qwerty keyboards can make their own key map. It's also entirely possible that I could have a bunch of standard setups selectable from the options menu.

I could even offer multiple downloads, for different languages, along with a default setup to match that language. I would use the same system for in-game messages rather than hard coding them, so that I can easily put the game into another language just by changing the contents of this file.

The format I'm using is my own, but uses the XML principle of /TOKEN to end a section.

KEYBOARD
MAPPING
/KEYBOARD
MOUSE
MAPPING
/MOUSE

(Since yesterday I added mouse mappings to the same file)
It has no disadvantages that I can see, compared to XML. For a more complex tree, I would use TinyXML.
January 19, 2009 08:28 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement