1
\$\begingroup\$

I've started writing a tile based game in XNA today using the tutorials here. I'm on the Part 6 - Side Topic - Auto Transitions at the moment. My transitions aren't working correctly as you can see below:

enter image description here

When moving around, several of the tiles don't transitions correctly and stay as the default square, you can see this happening with the 2 bottom right 'water' tiles, they're not transitioning with the 'dark grass' tiles.

Here's the class that does the transitioning:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame1
{
    class TileMap
    {
    public List<MapRow> Rows = new List<MapRow>();
    public int MapWidth = 32;
    public int MapHeight = 32;

    public TileMap()
    {
        for (int y = 0; y < MapHeight; y++)
        {
            MapRow thisRow = new MapRow();
            for (int x = 0; x < MapWidth; x++)
            {
                thisRow.Columns.Add(new MapCell(64));
            }
            Rows.Add(thisRow);
        }

        Rows[0].Columns[3].TileID = 96;
        Rows[0].Columns[4].TileID = 96;
        Rows[0].Columns[5].TileID = 32;
        Rows[0].Columns[6].TileID = 32;
        Rows[0].Columns[7].TileID = 32;

        Rows[1].Columns[3].TileID = 96;
        Rows[1].Columns[4].TileID = 32;
        Rows[1].Columns[5].TileID = 32;
        Rows[1].Columns[6].TileID = 32;
        Rows[1].Columns[7].TileID = 32;

        Rows[2].Columns[2].TileID = 96;
        Rows[2].Columns[3].TileID = 32;
        Rows[2].Columns[4].TileID = 32;
        Rows[2].Columns[5].TileID = 32;
        Rows[2].Columns[6].TileID = 32;
        Rows[2].Columns[7].TileID = 32;

        Rows[3].Columns[2].TileID = 96;
        Rows[3].Columns[3].TileID = 32;
        Rows[3].Columns[4].TileID = 32;
        Rows[3].Columns[5].TileID = 0;
        Rows[3].Columns[6].TileID = 0;
        Rows[3].Columns[7].TileID = 0;

        Rows[4].Columns[2].TileID = 96;
        Rows[4].Columns[3].TileID = 32;
        Rows[4].Columns[4].TileID = 32;
        Rows[4].Columns[5].TileID = 0;
        Rows[4].Columns[6].TileID = 0;
        Rows[4].Columns[7].TileID = 0;

        Rows[5].Columns[2].TileID = 96;
        Rows[5].Columns[3].TileID = 32;
        Rows[5].Columns[4].TileID = 32;
        Rows[5].Columns[5].TileID = 0;
        Rows[5].Columns[6].TileID = 0;
        Rows[5].Columns[7].TileID = 0;

        doAutoTransition();
    }

    private void doAutoTransition()
    {
        for (int y = 0; y < MapHeight; y++)
        {
            for (int x = 0; x < MapWidth; x++)
            {
                int height = getBaseTile(y, x);
                int start = GetTileBaseHeight(height) + 1;
                for (int i = start; i < 4; i++)
                {

                    int tileID = CalculateTransistionTileEdge(y, x, i);
                    if (tileID > -1)
                    {
                        Rows[y].Columns[x].AddBaseTile(i * 32 + tileID);
                    }

                    tileID = CalculateTransistionTileCorner(y, x, i);
                    if (tileID > -1)
                    {
                        Rows[y].Columns[x].AddBaseTile(i * 32 + 16 + tileID);
                    }
                }
            }
        }
    }

    private int GetTileBaseHeight(int tileID)
    {       
        return tileID / 32;
    }

    private int getBaseTile(int y, int x)
    {
        if (x < 0 || y < 0 ||
            x >= MapWidth || y >= MapHeight)
        {
            return 0;
        }

        return Rows[y].Columns[x].TileID;
    }

    private int CalculateTransistionTileEdge(int y, int x, int iHeight)
    {
        int temp = 0;

        if (GetTileBaseHeight(getBaseTile(y, x - 1)) == iHeight)
        {
            //Left
            temp += 1;
        }

        if (GetTileBaseHeight(getBaseTile(y - 1, x)) == iHeight)
        {
            //Top
            temp += 2;
        }

        if (GetTileBaseHeight(getBaseTile(y, x + 1)) == iHeight)
        {
            //Right
            temp += 4;
        }
        if (GetTileBaseHeight(getBaseTile(y + 1, x)) == iHeight)
        {
            //bottom
            temp += 8;
        }

        if (temp > 0)
        {
            return temp;
        }

        return -1;
    }

    private int CalculateTransistionTileCorner(int y, int x, int iHeight)
    {
        int temp = 0;

        if (GetTileBaseHeight(getBaseTile(y - 1, x - 1)) == iHeight)
        {
            //Left top
            temp += 1;
        }
        if (GetTileBaseHeight(getBaseTile(y - 1, x + 1)) == iHeight)
        {
            //Top right
            temp += 2;
        }
        if (GetTileBaseHeight(getBaseTile(y + 1, x + 1)) == iHeight)
        {
            //Bottem Right
            temp += 4;
        }
        if (GetTileBaseHeight(getBaseTile(y + 1, x - 1)) == iHeight)
        {
            //Bottom left
            temp += 8;
        }
        if (temp > 0)
        {
            return temp;
        }

        return -1;
    }
}

class MapRow
{
    public List<MapCell> Columns = new List<MapCell>();
   }
}
\$\endgroup\$
1
  • \$\begingroup\$ "Debug my code for me" questions are really too localised for this site; we're trying to generate an eternal knowledge-base which will be useful for Google searches. For personal help like what you're asking for here, our chat or a discussion forum like gamedev.net are better places to ask. :) \$\endgroup\$ Commented Feb 4, 2013 at 4:26

1 Answer 1

0
\$\begingroup\$

Turns out I had the wrong SpriteSortMode when drawing the tiles to the screen. Changed it to SpriteSortMode.Immediate and works correctly now.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ You should not use SpriteSortMode.Immediate like this - it does as little batching as possible and will surely cause you a lot of performance gripes in the future. I'd highly advise you try Defered first and if this does not work check out your drawing order and re-arrange it in a way that will give the correct behavior while still retaining performance. \$\endgroup\$ Commented Feb 2, 2013 at 23:45
  • \$\begingroup\$ Deferred works just as well, thanks for the heads up! \$\endgroup\$
    – Tom O
    Commented Feb 3, 2013 at 0:36

Not the answer you're looking for? Browse other questions tagged .