0
\$\begingroup\$

I have been looking at tile engines for the past few days and decided to have a go at my own one for educational purposes. I want to use different textures for the tiles of my map, for example 1 = grass, 2 = sky, 3 = dirt etc.. I am having this problem where the tiles will draw using only one specific texture although I provide multiples. I have been following tutorials here and there and searching online to try and find this problem but to no avail.

In the example below in the LoadContent function I load three different textues into my TileMap object and Console.WriteLine states that there actually are 3 textures loaded into the list.

I was under the impression that the draw method in the TileMap would set the index to the tile ID corresponding to a tile texture and then that specific tile would be drawn on the map in game. for example, index 1 would equal the first texture in the list, index 2 would equal the second texture and so on. Is this a correct assumption or am I way off?

Also, can you give me any help/pointers on ways to fix this issue of only one texture showing on the screen.

Thank you in advance.

Tile Map

 class TileMap
    {
        private MapCell[,] mapCell;
        public const int TILE_WIDTH = 64;
        public const int TILE_HEIGHT = 64;

    private List<Texture2D> tileList = new List<Texture2D>();

    public TileMap(int[,] exisitingMap)
    {
        //initialise this to a new multidimensional array;
        mapCell = new MapCell[exisitingMap.GetLength(0), exisitingMap.GetLength(1)];
        // x always starts on one 
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(0); y++)
            {
                mapCell[y, x] = new MapCell(exisitingMap[y, x]);
            }
        }       
     }

    public void loadTextureFiles(ContentManager content, params string[] fileNames)
    {
        Texture2D tileTexture;
        foreach (string fileName in fileNames)
        {
            tileTexture = content.Load<Texture2D>(fileName);
            tileList.Add(tileTexture);
            Console.WriteLine(tileList.Count + " Tile texture count ");
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {

        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(0); y++)
            {
                // setting the index to the tile ID
                int index = mapCell[y,x].TileID;
                Texture2D texture = tileList[index];

                spriteBatch.Draw(texture, new Rectangle( x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT), Color.White);

            }
        }   
    }
}

Map Cell

class MapCell
{
    public int TileID { get; set; }

    public MapCell(int tileID) 
    {
        tileID = TileID;
    }
}

Game1

TileMap tileMap = new TileMap(new int[,]
    {
        { 0,0,0,0,0,0,0 },
        { 1,1,1,1,1,1,1 },
        { 0,1,1,2,1,1,1 },
        { 0,2,1,3,1,3,0 },
        { 0,3,0,3,0,0,0 },
        { 0,0,0,2,0,0,0 },
        { 0,0,0,1,0,1,0 },
    });

 protected override void LoadContent()
    {
        new SpriteBatch(GraphicsDevice);
        tileMap.loadTextureFiles(Content, "Tiles/Tile1", "Tiles/sky", "Tiles/dirt" );  
    }

protected override void Draw(GameTime gameTime)
    {
        spriteBatch.Begin();
        tileMap.Draw(spriteBatch);
        base.Draw(gameTime);

        spriteBatch.End();
    }
\$\endgroup\$
2
\$\begingroup\$

In the constructor of MapCell swap tileID with TileID:

public MapCell(int tileID)
{
    TileID = tileID;
}

Also in the Draw code you need to change:

int index = mapCell[y, x].TileID;
if (index > 0) //check if there is a tile
{
    //subtract the index by one to start by the first index, which is 0
    //[0] = tex1, [1] = tex2, [2] = tex3
    Texture2D texture = tileList[index - 1];

    spriteBatch.Draw(texture, new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT), Color.White);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.