Now that we have our variables covered (and understand their uses to a certain extent) we can have a look at the functions we will need to call to use our class.
At this stage, we need our tile engine to do 2 things. Initialize, which is another way of saying "start" or "begin", and draw, which will draw our tile map to the screen.
/* Starting with our Initialize class, we want to firstly ensure it is a "void" function. This tells the compiler that this function does not return a value. Secondly, we need to define what we need in order initialize our tile map. What we need is an array, this array will be the map which will define our tile map. This is entered in the brackets after the function name. The name also should not conflict with our variable of a similar name*/
public void Initialize(int[,] maparray)
{
/* Now, want to take the array that has been given to us and store it inside our variable, essentially saving it and making it easier to re-use */
MapArray = maparray;
/* We then want to store the height and width of our array. Unlike you would expect, height is the first dimension of an array (therefore a 0 is used) and width is the 2nd dimension (therefore a 1 is used) /*
Height = maparray.GetLength(0);
Width = maparray.GetLength(1);
}
/* Moving on to the draw method now. The draw method will be utilized within Game1's draw method and use its spritebatch, so we ensure that our function needs one /*
public void Draw(SpriteBatch spritebatch)
{
/* We first need to loop through our MapArray, we will go through each co-ordinate via the Width and Height that we defined earlier. We need to ensure we apply some constraints to stop us looping too far. SO we define x and y as less than their worded counter-parts */
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
/* Within the loop we are purely finding the value that resides in the chosen MapArray co-ordinate and storing it in a local variable names tileindex */
int tileindex = MapArray[y, x];
/* Now we need to find the texture that corresponds to the number stored in tileindex(the co-ordinate we are currently on in looping of the MapArray). The tiletextures list will store each texture into it linearly, meaning that the first texture loaded into it will be number 0 and so on. Thats how we find what texture corresponds to each number in our MapArray */
Texture2D tiletexture = tiletextures[tileindex];
/* It is now time to draw the texture we have founbd to the corresponding square on the grid, we take the spritebatch and command it to draw the following /*
spritebatch.Draw(tiletexture, new Rectangle(x * 32, y * 32,
32, 32),Color.White);
/* firstly we tell the spritebatch the texture it needs to draw(tiletexture), then tell it where the square it needs to draw to is located(new rectangle 32 pixels by 32 pixels with the x and y corresponding to where it was located in the MapArray, and finally the color is white for transparent(no tint)) */
}
In the next tutorial, we will look at how we use our class and finally wrap this up into a tile map that you can see!
No comments:
Post a Comment