Game Development Reference
In-Depth Information
it sneezes and grows spikes for 5 seconds, after which it returns to the previous state
again for 5 seconds, and so on.
The enemy is represented by the Tu r t l e class, which is setup in a similar fashion
to the previous enemies. A turtle has two states: it has spikes, or not. In this case,
we maintain two member variables to maintain in which state we are and how much
time has passed in that state: the idleTime variable maintains how much time is left in
the 'idle' state, and the sneezeTime variable maintains how much time is left in the
'sneezing' (dangerous) state. Again, in the Update method we handle the transition
between the two phases, in a similar fashion to the rocket and the patrolling enemies.
We will not go into detail here, since the code is so similar. If you want to have a
look at the complete code, check out the TickTick3 program in the solution belonging
to this chapter.
Sparky is the final enemy type that we will add to the game. Just like the other
enemies, Sparky also has two states. Sparky is a very dangerous electricity-loving
enemy. Sparky 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 is not dangerous, but
as soon as he falls, do not touch him! Have a look at the Sparky class to see what the
code looks like.
Enemy software architecture— As you can see, all these different types of
enemies look different and behave different, but they generally have a very
common class design. You could probably design a better way to define these
enemies by using a couple of generic classes that allow one to define states and
transitions between them. There could be conditions attached to each transi-
tion, such as that a certain amount of time must have passed or that an anima-
tion should be finished playing. We call such a structure a finite state machine
It is a very common technique using in artificial intelligent systems. If you
are up to the challenge, try to write a finite state machine library, and redefine
the existing enemies to use it!
28.5 Loading the Different Types of Enemies
Now that we have defined different varieties of enemies, the only thing left to do
is load them from the level definition file. We have designed the sprites for each of
these different enemies, which we identify using characters. We store these enemy
characters in a GameObjectList object, which we create in the Level class constructor:
this .Add( new GameObjectList(2, "enemies"));
Depending on the character that we read when loading the level, we call a different
method for loading the enemy, by adding a few cases to the switch instruction in the
Level class:
Search WWH ::




Custom Search