1

我正在为我的第一个游戏构建一个磁贴引擎,并且我一直在逐步遵循指南(无法链接它,因为我仅限于 2 个)。但是,我对 tilemap 进行了一些修改,以便我可以继续我的项目。在测试我的程序时发现了一个错误,我希望你们能帮助我解决......

...瓷砖引擎正在绘制错误的瓷砖!

是我的 TileMap(为每个图块分配了数字),就是我的引擎绘制的。虽然它应该遵循如下所示的顺序(代码是不言自明的)

        rows[0].columns[3].tileID = 0;
        rows[0].columns[4].tileID = 1;
        rows[0].columns[5].tileID = 2;
        rows[0].columns[6].tileID = 3;
        rows[0].columns[7].tileID = 4;

        rows[1].columns[3].tileID = 5;
        rows[1].columns[4].tileID = 6;
        rows[1].columns[5].tileID = 7;
        rows[1].columns[6].tileID = 8;
        rows[1].columns[7].tileID = 9;

        rows[2].columns[3].tileID = 10;
        rows[2].columns[4].tileID = 11;
        rows[2].columns[5].tileID = 12;
        rows[2].columns[6].tileID = 13;
        rows[2].columns[7].tileID = 14;

        rows[3].columns[3].tileID = 15;
        rows[3].columns[4].tileID = 16;
        rows[3].columns[5].tileID = 17;
        rows[3].columns[6].tileID = 18;
        rows[3].columns[7].tileID = 19;

        rows[4].columns[3].tileID = 20;
        rows[4].columns[4].tileID = 21;
        rows[4].columns[5].tileID = 22;
        rows[4].columns[6].tileID = 23;
        rows[4].columns[7].tileID = 24;

        rows[5].columns[3].tileID = 25;
        rows[5].columns[4].tileID = 26;
        rows[5].columns[5].tileID = 27;
        rows[5].columns[6].tileID = 28;
        rows[5].columns[7].tileID = 29;

代码要点

我的代码的重要部分

Tile.class

我在哪里定义瓷砖大小并具有导航瓷砖地图以找到我的 deiserd 瓷砖的功能。

namespace TileEngine
{
    static class Tile
    {
        static public Texture2D tileSetTexture;
        static public int tileWidth = 48;
        static public int tileHeight = 48;

        static public Rectangle getSourceRectangle(int tileIndex)
        {
            int tileY = tileIndex / (tileSetTexture.Width / tileWidth);
            int tileX = tileIndex % (tileSetTexture.Height / tileHeight);

            return new Rectangle(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight);
        }
    }
}

地图单元

我在哪里定义 tileID

class MapCell
    {
        public List<int> baseTiles = new List<int>();
        public int tileID 
        {
            get { return baseTiles.Count > 0 ? baseTiles[0] : 0; }
            set
            {
                if (baseTiles.Count > 0)
                    baseTiles[0] = value;
                else
                    addBaseTile(value);
            } 

        }

        public void addBaseTile(int tileID)
        {
            baseTiles.Add(tileID);
        }

        public MapCell(int tileID)
        {
            this.tileID = tileID;
        }
    }

瓦片地图

我在哪里构建地图

 class MapRow
    {
        public List<MapCell> columns = new List<MapCell>();
    }

    class TileMap
    {
        public List<MapRow> rows = new List<MapRow>();
        public int mapWidth = 50;
        public int mapHeight = 50;

        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(0));
                }
                rows.Add(thisRow);
            }
        }
    }

最后是我的绘图代码。

protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();

            Vector2 firstSquare = new Vector2(Camera.location.X / Tile.tileWidth, Camera.location.Y / Tile.tileHeight);
            int firstX = (int)firstSquare.X;
            int firstY = (int)firstSquare.Y;

            Vector2 squareOffset = new Vector2(Camera.location.X % Tile.tileWidth, Camera.location.Y % Tile.tileHeight);
            int offsetX = (int)squareOffset.X;
            int offsetY = (int)squareOffset.Y;

            for (int y = 0; y < squaresDown; y++)
            {
               for (int x = 0; x < squaresAcross; x++)
                {
                    foreach (int tileID in myMap.rows[y + firstY].columns[x + firstX].baseTiles)
                    {
                        spriteBatch.Draw(
                            Tile.tileSetTexture,
                            new Rectangle(
                                (x * Tile.tileWidth) - offsetX, (y * Tile.tileHeight) - offsetY,
                                Tile.tileWidth, Tile.tileHeight),
                            Tile.getSourceRectangle(tileID),
                            Color.White);
                    }
                } 
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }
4

0 回答 0