c# - 绘制等轴瓦图

标签 c# xna 2d isometric

我一直在使用 C#/XNA Game Studio 绘制等轴测图,虽然我已经画得很远了,但看起来不太正确,我想知道是否有人可以提供帮助。

这是我绘制 map 的代码:

public void Draw(SpriteBatch theBatch, int drawX, int drawY)
    {

        if ((drawY % 2 == 0))
            theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * length / 2), width, length), Color.White);
        else
            theBatch.Draw(tileTexture, new Rectangle(((drawX * width) + (width / 2)), (drawY * length / 2), width, length), Color.White);
    }

此方法中的代码就像在嵌套的 for 循环中一样,从左到右,从上到下绘制。当 y 值为奇数时,该行将移动以适合,但看起来有点偏离。

这是 8x5 map 的生成输出:

http://imgur.com/xpDRU.png

如您所见,它看起来不太对劲,我不确定这是我代码中的数学问题,还是与绘制所有内容的顺序有关。我我对 C# 和 sprites 还很陌生,所以非常感谢任何帮助。

因为它可能会有帮助,这里是绘制 map 的代码的其他相关部分。

整个 Tile 类:

public class Tile
{
    // Dimension variables
    int height;
    int width;
    int length;

    String type;
    Texture2D tileTexture;
    Vector2 coordinates;

    /// 
    /// Tile Constructor
    /// 
    public Tile(ContentManager theContent, String theType, Vector2 theCoordinates)
    {
        width = 68;
        length = 46;

        type = theType;
        coordinates = theCoordinates;

        // Sets the right texture to the texture ref
        if (theType == "grass")
            tileTexture = theContent.Load<Texture2D>(@"Tiles\iso_grass");
    }

    /// 
    ///  Draws the tile at the given location
    /// 
    public void Draw(SpriteBatch theBatch, int drawX, int drawY)
    {

        if ((drawY % 2 == 0))
            theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * length / 2), width, length), Color.White);
        else
            theBatch.Draw(tileTexture, new Rectangle(((drawX * width) + (width / 2)), (drawY * length / 2), width, length), Color.White);
    }


}

TileRow 类,它包含一行图 block 。

public class TileRow
{
        public List<Tile> Row = new List<Tile>();
        public int rowLength;

        public TileRow(int theLength, int yIndex, ContentManager theContent)
        {
            rowLength = theLength;
            Tile thisTile;

            // Here the tiles are created and added to the row
            for (int x = 0; x < rowLength; x++)
            {
                thisTile = new Tile(theContent, "grass", new Vector2(x, yIndex));
                Row.Add(thisTile);
            }
        }

        /// 
        /// Draw -- invokes the draw method of each tile in the row
        /// 
        public void DrawRow(SpriteBatch theBatch, int currentY)
        {
            for (int x = 0; x < rowLength; x++)
            {
                Row[x].Draw(theBatch, currentY, x);
            }
        }
    }
}

和保存所有行的 MapStruct 类

public class MapStruct
{

    public List<TileRow> allRows = new List<TileRow>();
    int depth;

    // Constructor
    public MapStruct(ContentManager theContent, int theDepth, int rowLength)
    {
        depth = theDepth;
        TileRow thisRow;

        // Here we make a row of tiles for each level of depth
        for (int y = 0; y < depth; y++)
        {
            thisRow = new TileRow(rowLength, depth, theContent);
            allRows.Add(thisRow);
        }
    }

    ///
    /// Draw - this method invokes the draw method in each tile row, which then draws each tile
    ///
    public void DrawMap(SpriteBatch theBatch)
    {
        for (int y = 0; y < depth; y++)
        {
            allRows[y].DrawRow(theBatch, y);
        }
    }
}

非常感谢任何有关如何解决此问题的帮助,以及有关如何改进我的代码的建议!

最佳答案

看起来您的循环向每一行的 Y 添加了一点点或多点。 我在您的 Tile 函数中找到了这个变量。

length = 46;

我还没有检查过,但我相信“长度”是瓷砖的高度?如果是这样,请尝试稍微调整一下。也许,您忘记减去瓷砖的高度。所以如果图 block 的边长是 6 个像素,那么偏移 pr 的长度。行只有 40。

还请记住从上到下绘制,因为必须最后绘制离相机最近的图 block ,以产生深度错觉。

关于c# - 绘制等轴瓦图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4515264/

相关文章:

image - 傅立叶移位定理matlab

javascript - 如何使用旋转矩阵旋转形状?

c# - 在 C# 中是否有产生 64 位散列大小的散列算法?

c# - 是否有等效于 C# 成员大括号初始化功能的 Objective-C/C++?

C# XNA A* 寻路敌人卡在对面的墙上

c# - 将嵌套 XML 转换为 3d 数组

c# - 如何使用 LINQ 查找 DateTime 的总和

c# - Entity Framework 中存储函数的使用

XNA:获取屏幕的宽度和高度

Android - Sprite 和线程理解问题