Now that we have made our class, we are ready to implement it into Game1.cs
As it is easier, I will list all of the code as I have in the previous parts and then section it out via comments.
Everything that is part of the base class I will section off in RED, my comments will stay green, with added code going untouched.
namespace TileEngine
{
public class Game1 : Microsoft.Xna.Framework.Game
{
/* First of all up in global variables section, we want to define our variables /*
/* Firstly we have our actual level "object", this is what we will manipulate and use to create our tile map */
TileMap tilemap = new TileMap();
/* Secondly is our "MapArray", this is the array that we want our tiles to represent. For now, this must be represented in 1's and 0's as we only have 2 textures */
int[,] levelmap = new int[,]
/* The actual array can involve any shape you want, as long as it square and complete, for example we have a "T" shape shown below, this will create a dirt path in the shape of a T surrounded by grass */
{
{1,1,1,1,1,1,1,1,1},
{0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,0,0,0,0},
};
public Game1()
{
graphics =
Content.RootDirectory = new GraphicsDeviceManager(this);"Content";protected override void Initialize()
}
/* In the initialize section we define what we want to happen on the game's start-up. This is going to include the initialization of our level, as well as resolution of the gamescreen */
/* Firstly we initialize our tilemap, with the levelmap that we created in our variable section, as that is what we said we needed when we created the initialize function */
tilemap.Initialize(levelmap);
/* We now need to ensure that the gamescreen is scaled to the size of the tilemap. This is done by just multiplying the tilemap height and width by 32 (32 is the size of our textures in pixels) */
graphics.PreferredBackBufferHeight = 32 * tilemap.Height;
graphics.PreferredBackBufferWidth = 32 * tilemap.Width;
graphics.ApplyChanges();
base.Initialize();
protected override void LoadContent()
spriteBatch = new SpriteBatch(GraphicsDevice);/* In the LoadContent section... we do what you might guess, Load Content. */
/* First lets load in our textures, you want to find a 32x32 grass and mud texture from somewhere and just load them into your project, save them as "grass" and "mud" */
Texture2D grass = Content.Load<Texture2D>("grass");
Texture2D mud = Content.Load<Texture2D>("mud");
/* We then want to add these textures to our tilemaps texture list. We must do them in the correct order, as dependant upon thier position they will have a different number to associate them with. Do grass first, as that will be "0" */tilemap.tiletextures.Add(grass);
tilemap.tiletextures.Add(mud);}
protected override void UnloadContent(
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
/* Finally, we just need to draw our tilemap, all the hard work was done in the class, so we simply just tell it to draw */spriteBatch.Begin();
tilemap.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
Thats it!
You should now be able to run the program and get a tilemap with a T in it. This is very simple but can be adapted and improved on to do anything. I would recommend making Tower Defence games with something this simple, to improve your knowledge.
No comments:
Post a Comment