Game Development Reference
In-Depth Information
This class defines an enemy that follows the player if the player is moving. This is done by checking
whether the enemy is currently walking in the direction where the player is standing (only taking
the x direction into account). If not, the enemy turns around. You place a limitation on the enemy's
intelligence by doing that only if the player isn't moving in the x direction (in other words, the player's
x velocity is zero).
You should never make enemies too smart. In addition, don't make them too fast—it would be a
short game if enemies walked appreciably faster than the player while following them. Enemies are
there to be beaten by the player so the player can win the game. Playing a game where the enemies
are too smart or unbeatable isn't a lot of fun, unless you like dying over and over again!
Other Types of Enemies
Yet another enemy you can add to the game is a sneezing turtle (see Figure 27-3 ). Why a turtle, you
ask? And why a sneezing one? Well, I don't really have an answer to that question. But the idea
behind this enemy is that it has both a negative and a positive side. On the negative side, the turtle
grows spikes when it sneezes, so you shouldn't touch it. But if the turtle isn't sneezing, you can use
it to jump higher. Because you aren't dealing with interaction just yet, you only add the animated
turtle for now. The turtle can be used to jump for 5 seconds, then it sneezes and grows spikes for
5 seconds, after which it returns to the previous state for 5 seconds, and so on.
Figure 27-3. Don't jump on the spiky turtle!
The enemy is represented by the Turtle class, which is set up in a fashion similar to the previous
enemies. A turtle has two states: it's idle, or it has sneezed and therefore has dangerous spikes.
In this case, you maintain two member variables to keep track of which state the turtle is in and how
much time has passed in that state: the waitTime variable tracks how much time is left in the current
state, and the sneezing variable tracks whether the turtle is sneezing. Again, in the update method,
you handle the transition between the two phases, much as you did for the rocket and the patrolling
enemies. I don't go into further detail here because the code is very similar to the other enemy
classes. If you want to have a look at the complete code, check out the TickTick3 program in the
solution belonging to this chapter.
 
Search WWH ::




Custom Search