Tutorial 3 : The Enemy Class

So to start with add a new class called "Enemy.cs", then add the following using statements :

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Graphics;

From now on these 2 namespaces will need to be added to nearly every class, so I am just going to assume that you will remember to add these.

The first thing that we need to do is make our new class inherit from the "Sprite" class. To do this replace this line :

publicclass Enemy

with this :

publicclass Enemy : Sprite

Now we can access all of the methods and fields that we exposed in the Sprite class last time.

Every enemy is going to share some basic properties, so we will add these first, add these lines of code to the top of Entity.cs :

protectedfloat startHealth;

protectedfloat currentHealth;

protectedbool alive = true;

protectedfloat speed = 0.5f;

protectedint bountyGiven;

These fields should be pretty self explanatory so I won't insult you by explaining them to you, however one thing to notice is these fields are marked as "protected", this means that any class that inherits from this class will be able to access them.

Next we will add some properties to expose some of these feilds, so that other classes will be able "check up" on our enemy's. Add these just under the feilds we just added :

We now have the basic outline of our enemy class, so lets get drawing us some cannon fodder. Go back into "Game1.cs" and at the top, just under where we defined our level add the following :

Enemy enemy1;

Now before we can initialize our enemy, we need to add in a new sprite for the to use, so add the following image to the content project :

Note : This texture should be called "enemy.png"

With that added, we can move onto initializing and drawing our enemy!

So in the "LoadContent()" Method, add the following :

Texture2D enemyTexture = Content.Load<Texture2D>("enemy");

enemy1 = new Enemy(enemyTexture, Vector2.Zero, 100, 10, 0.5f);

This code loads in the texture for the enemy, and then passes it to our "Enemy" in the enemy's constructor. The enemy will be created in the top left corner (0, 0) with 100 health, 10 gold in his pocket, and with a slow walk.

Now to draw our enemy, add the following code in the draw method after level.Draw(spriteBatch) but before SpriteBatch.End() :

enemy1.Draw(spriteBatch);

Now Run the project, and you should see our level, with a little white circle in the top left corner, and their we have a very simple enemy class.

Before we move onto the next tutorial, we just need to add in a couple more things to the "Enemy" class. So go to the enemy class and add in the following code :

publicoverridevoid Update(GameTime gameTime)

{

base.Update(gameTime);

if (currentHealth <= 0)

alive = false;

}

This code overrides the Sprites "Update()" method. The first line just tell the Sprite base class to update itself, then in the last two lines we just check if the enemy is dead.

In the next method, we are going to override the Sprites "Draw()" method, so that we can draw our enemy a little bit differently. But first we must add a new method to "Sprite.cs". Go to "Spirte.cs" and add the following method :

publicvirtualvoid Draw(SpriteBatch spriteBatch, Color color)

{

spriteBatch.Draw(texture, center, null, color, rotation,

origin, 1.0f, SpriteEffects.None, 0);

}

This overload method allows us to "tint" a sprite with a certain colour.

Now, back to "Enemy.cs", add the following code just underneath the Update() method :

publicoverridevoid Draw(SpriteBatch spriteBatch)

{

}

Hopefully there is no explanation needed here. First we are going to check if our enemy is still alive :

if (alive)

{

Next we are going to work out what percentage of health our enemy has lost :

float healthPercentage = (float)currentHealth / (float)startHealth;

Then we will create a colour based off this information, so that our player can see how much damage he had done to his enemy :

Color color = new Color(new Vector3(1 - healthPercentage,

1 - healthPercentage, 1 - healthPercentage));

base.Draw(spriteBatch, color);

And that's it for our enemy class. Your draw method should now look like this :

publicoverridevoid Draw(SpriteBatch spriteBatch)

{

if (alive)

{

float healthPercentage = (float)currentHealth / (float)startHealth;

Color color = new Color(new Vector3(1 - healthPercentage,

1 - healthPercentage, 1 - healthPercentage));

base.Draw(spriteBatch, color);

}

}

So let's see if what we just did made any difference to our enemy ;). Go to "Game1.cs" and in the Update() method add the following :

enemy1.CurrentHealth -= 1;

enemy1.Update(gameTime);

This will knock off one health point from our enemy each frame. If you run the game now, you should see the enemy start off black, the gradually get whiter, and eventually disappears (He disappears because he is dead), and their we have it, our completed enemy class.

Just wanted to say that these tutorials are great! one problem though. You do not need to override the Draw method as you are overloading the actual method and this provides a seperate method signature and will yield an error if it does not find a method with that exact same signature. better to do public void Draw and call Draw(spriteBatch, color) in the method.

Are you sure that your Enemy class is inheriting from the sprite class (see the start of this tutorial) and if so are you sure you added all the correct methods to the sprite class? (See previous tutorial).

I think the confusion with the 'Tower_Defence.Enemy.Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch)': no suitable method found to override' error is from editing the sprite draw method.

This tutorial does not edit the Sprite Draw method from the previous tutorial, it adds a second one. When finished with this tutorial, their should be one draw method in the Enemy class and two in the Sprite class.