Game Development Reference
In-Depth Information
Sparky is the final enemy type that you add to the game. Just like the other enemies, Sparky has two
states (see Figure 27-4 ). Sparky is a very dangerous, electricity-loving enemy. He hangs quietly in the
air until he receives a bolt of energy. When that happens, he falls down. While Sparky is hanging in
the air, he isn't dangerous; but as soon as he falls, don't touch him! Have a look at the Sparky class
to see the code.
Figure 27-4. Sparky is dangerous when he is electrified
ENEMY SOFTWARE ARCHITECTURE
All these different types of enemies look different and behave differently, but they generally have a common class design.
You could probably design a better way to define these enemies by using a couple of generic classes that let you define
states and transitions between them. There could be conditions attached to each transition, such as that a certain
amount of time must have passed or that an animation should be finished playing. Such a structure is called a finite state
machine . It's a very common technique using in artificial intelligence systems. If you're up to the challenge, try to write a
finite state machine library and redefine the existing enemies to use it!
Loading the Different Types of Enemies
Now that you've defined different varieties of enemies, the only thing left to do is load them when
you read the level data variable. The sprites for the different enemies are identified using characters.
You store these enemy characters in a GameObjectList object, which you create in the Level class
constructor:
this._enemies = new powerupjs.GameObjectList(ID.layer_objects);
Depending on the character you read when loading the level, you call a different method to load the
enemy, by adding a few cases to the switch instruction in the Level class:
case 'R':
return this.loadRocketTile(x, y, true);
case 'r':
return this.loadRocketTile(x, y, false);
case 'S':
return this.loadSparkyTile(x, y);
 
Search WWH ::




Custom Search