This tutorial will mainly focus on how the player will add in new towers through the addition of a new class called “Player.cs”.
The Player class will hold all the information about a specific player, such as how much money he has, what stage he is on etc. The reason we are putting all this information in it’s own class and not just in “Game1.cs” is that if we do decide to make this game multiplayer, it will make it much easier to store information about each person playing.
So let’s get started, the first thing we will do as I'm sure you have guessed is add a new class called “Player.cs”. To start with we are going to add a few fields and properties :
Next we are going to add in one more field and a constructor for the class :
Now we are going to add in an Update method for our Player, this is where we will handle the creation of new towers, and also the updating of existing towers.
Right, I know this method looks completely pointless but I assure you it’s not!! The first and last lines are quite straight forward, we just update the mouseState so it is correct for the current frame, and update the oldState so it is correct for the previous frame. Now I’m sure your asking your self why are we dividing the mouse position by 32 only to multiply it by 32 again.
The reason if quite simple really, if you think about what happens when you divide a floating point number by another floating point number, and then cast it to and integer, you will get the integer part number of the number. So lets take a look at an example :
In the above example if the mouse was at position (77, 114) and we use the above equation to calculate where that is in array space we get the following :
CellX = (int) (77 / 32)
= (int) (2.40625)
Which is correct, as we can see in the image the point is in the third square along. Now we know what cell the pointer is in we can work out where that cell is in level space by multiplying it by 32 (The the widow of our tiles)
TileX = 2 * 32 = 64
Which is the level space position of the top right corner of the 3rd tile along, so hopefully now you can see why this works.
All this code does is return the index of the requested cell. We can use this index to check if we are on a path or not. Right, now we are ready to add the following method to “Player.cs” :
Now, as you may have noticed in this method we are trying to access tower.Position, but we never actually created that property in the sprite class… my bad. So lets go to “Sprite.cs” and add the following property :
Now we need to modify the constructor slightly to allow for a tower texture to be passed in :
And that’s all the fields we will add for now. Next, in the Update method add the following just before where we set oldState :
We are almost finished with the Player class, but there is one big thing that we are missing, have you spotted it yet? At no point are our towers updated, we will handle this now, just under the code we just added, add the following :
Next replace the update method with this one :