Game Development Reference
In-Depth Information
using
System;
1
using
Microsoft.Xna.Framework;
2
3
4
class
PlayerFollowingEnemy : PatrollingEnemy
{
5
public override void
Update(GameTime gameTime)
6
{
7
GameObjectList gameWorld = Root
as
GameObjectList;
8
Player player = gameWorld.Find("player")
as
Player;
9
float
direction = player.Position.X
−
position.X;
10
if
(Math.Sign(direction) != Math.Sign(velocity.X) && player.Velocity.X != 0.0f
11
&& velocity.X != 0.0f)
12
TurnAround();
13
base
.Update(gameTime);
14
}
15
}
16
Listing 28.3
An enemy that follows the player when the player is moving
in the right direction. Finally, we call the
Update
method of the base class, so that
the right animation is selected, collisions with the player are dealt with, and so on.
Another variety that we can think of is an enemy that follows the player instead
of simply walking from the left to the right and back again. Here also, we inherit
from the
PatrollingEnemy
class. Listing
28.3
shows the class
PlayerFollowingEnemy
that
defines an enemy following the player if the player is moving around. This is done
by checking if 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.
We have placed a limitation on the enemy's intelligence by only doing that if the
player is not moving in the
x
direction (in other words: the player's
x
velocity is
zero). You should never make your enemies too smart. Enemies are there to be
beaten by the player so that the player can win the game. Playing a game where the
enemies are too smart or unbeatable is not a lot of fun, unless you like dying over
and over again!
28.4 Other Types of Enemies
Yet another enemy we would like to add to the game is a sneezing turtle. Why a
turtle, you might ask. And why a sneezing one? Well, we do not really have the
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 should not touch it. But if the turtle is not sneezing, you can use the turtle to
jump higher. Since we will not deal with interaction just yet, we will only add the
animated turtle for now. The turtle can be used for jumping during 5 seconds, then