Wednesday, 1 February 2012

XNA Tile Engine - Introduction


In some 2D games, it can be very useful to use a tile based engine for your gameplay. If you think about a tower defence game, they are nearly always tile-based, with the towers taking up a square and a path being drawn in squares throughout the map. In old fashioned 2D RPG's we also see a tile engine taking the forefront, with the character moving through a tile-based world with 1 item taking up 1 tile.

In this tutorial I aim to introduce the beginning of a tile engine. It is VERY simple and isn't written in the best possible way. I must also note that this tutorial is written for XNA 4.0. I am also to assume you can at least code a little bit and can use Visual Studio.


So firstly, you want to be opening an XNA game project as you usually would (name is TileEngine for consistency's sake).
As you already know, Game1.cs is used as a catalyst, to bring all your classes and such together. So we need to create a new class to handle the creation of our tile map. Name this TileMap.cs.

Within TileMap.cs, we now need to import some XNA libraries. This is simple, just copy and paste the following from Game1.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

And place it by all the other "using" statements.

Once that is over and done with, we can get on with the real programming!

In this tutorial, I will just cover the variables that we need, the functions and usage will be covered later
So, our variables. These are the things that our class needs in order to function, the pieces of information that are held and piece together our tile map.

When in coded sections like below, all the information regarding what is going on will be found in the "commented sections" which I will colour green.

namespace TileEngine
{
    class TileMap
    {

 /* Firstly we have our MapArray, this is what will store our tile map's shape and decide what resides in the map. This is an array because the square shape of an array can represent a grid. Later on we will see how this works. /*
        public int[,] MapArray;


/* This is a simple variable to store the height of our array (how many squares our tile map will be) */     

        public int Height;
 
// This is the same as above but for the width
        public int Width;

 
/* This is the list of textures that will make our tiles different from each other. This is where you would store the different images for say a path or for some grass. The usage of this will be revealed later */
        public List<Texture2D> tiletextures = new List<Texture2D>();


In the next tutorial we will cover our class's functions, our class will then be ready to use.

No comments:

Post a Comment