Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and saving maps with tiles in XNA

Tags:

c#

xna

I'm trying to make a "dungeon crawler" type game demo, and this is my first time really making anything beyond Pong & Pac-Man clones. My big hang up right now is creating the actual level. I've gone through a tutorial on how to draw tiles onto the screen, but I can't find anything about where to go from there.

How do I go from one screen to making a larger dungeon? Any help is appreciated.

like image 265
Austin Avatar asked Nov 23 '11 18:11

Austin


1 Answers

You should consider starting off by using 2 dimensional arrays. In this manner, you can represent your data visually quite easily.

Start off with an initialization:

//2D array
int[,] array;

Some sample data:

array= new int[,]
        {
            {0, 2, 2, 0},
            {3, 0, 0, 3},
            {1, 1, 1, 1},
            {1, 0, 0, 0},
        };

Create yourself an enumeration, which will index each integer in your map:

enum Tiles
{
    Undefined = 0,
    Dirt = 1,
    Water = 2,
    Rock = 3
}

Then load your textures and what not by looking through your array one item at a time. Based on your texture size, you can easily draw your textures on screen as presented in your map:

for (int i = 0; i < array.Count; i++)
{
    for (int j = 0; j < array[0].Count; j++)  //assuming always 1 row
    {
       if (array[i][j] == (int)Tiles.Undefined) continue;

       Texture = GetTexture(array[i][j]);  //implement this

       spriteBatch.Draw(Texture, new Vector2(i * Texture.Width, j * Texture.Height), null, Color.White, 0, Origin, 1.0f, SpriteEffects.None, 0f);
    }
}
like image 182
jgallant Avatar answered Sep 30 '22 12:09

jgallant