2

我决定尝试使用 Xna 框架制作一个地牢爬行游戏。我是一名计算机科学专业的学生,​​对 c# 和 .net 框架非常熟悉。我对我的引擎开发的不同部分有一些疑问。

  1. 加载地图

我有一个磁贴类,用于存储磁贴的 vector2 位置、2dtexture 和尺寸。我有另一个名为 tilemap 的类,它有一个按位置索引的图块列表。我正在从上面数字格式的文本文件中读取,该文件将数字与图块列表中的索引匹配,并创建一个具有正确纹理和位置的新图块,并将其存储到另一个图块列表中。

public List<Tile> tiles = new List<Tile>(); // List of tiles that I have added to the game
public List<TileRow> testTiles = new List<TileRow>(); // Tilerow contains a list of tiles along the x axis along with there vector2 position.

读取和存储地图图块。

    using (StreamReader stream = new StreamReader("TextFile1.txt"))
                {
                    while (stream.EndOfStream != true)
                    {
                        line = stream.ReadLine().Trim(' ');
                        lineArray = line.Split(' ');
                        TileRow tileRow = new TileRow();
                        for (int x = 0; x < lineArray.Length; x++)
                        {  
        tileXCo = x * tiles[int.Parse(lineArray[x])].width;
        tileYCo =  yCo * tiles[int.Parse(lineArray[x])].height;
        tileRow.tileList.Add(new Tile(tiles[int.Parse(lineArray[x])].titleTexture, new Vector2(tileXCo,tileYCo)));
                        }
                        testTiles.Add(tileRow);
                        yCo++;
                    }
                }

用于绘制地图。

 public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            foreach (TileRow tes in testTiles)
            {
                foreach (Tile t in tes.tileList)
                {
                    spriteBatch.Draw(t.titleTexture, t.position, Color.White);
                }
            }
        }

问题:这是我应该做的正确方式,还是应该只存储一个引用我的图块列表的列表?

我将如何处理多层地图?

  1. 碰撞检测

目前我有一个方法循环遍历存储在我的 testTiles 列表中的每个图块,并检查其尺寸是否与玩家尺寸相交,然后返回所有图块的列表。我有一个名为 CollisionTile 的瓦片类的派生类,当玩家和该矩形相交时会触发碰撞。(公共类 CollisionTile :平铺)

public List<Tile> playerArrayPosition(TileMap tileMap)
        {  
            List<Tile> list = new List<Tile>();
            foreach (TileRow test in tileMap.testTiles)
            {
                foreach (Tile t in test.tileList)
                {
                    Rectangle rectangle = new Rectangle((int)tempPosition.X, (int)tempPosition.Y, (int)playerImage.Width / 4, (int)playerImage.Height / 4);
                    Rectangle rectangle2 = new Rectangle((int)t.position.X, (int)t.position.Y, t.width, t.height);
                    if (rectangle.Intersects(rectangle2))
                    {
                        list.Add(t);
                    }
                }
            }
            return list;
        }

是的,我很确定这不是检查瓷砖碰撞的正确方法。任何帮助都会很棒。

对不起,很长的帖子,任何帮助将不胜感激。

4

2 回答 2

3

你说的对。这是在瓷砖上绘制和检查碰撞的一种非常低效的方法。您应该研究的是四叉树数据结构。

四叉树将以允许您使用矩形查询世界的方式存储您的图块,并且您的四叉树将返回包含在该矩形内的所有图块。

List<Tiles> tiles = Quadtree.GetObjects(rectangle);

这允许您仅选择需要处理的图块。例如,在绘制瓷砖时,您可以指定视口大小的 Rectangle,并且只会绘制(剔除)这些瓷砖。

另一个例子是,您可以使用玩家的 Rectangle 查询世界,并且只检查为您的世界的该部分返回的图块上的碰撞。

为了加载您的图块,您可能需要考虑加载到二维数组而不是列表中。这将允许您根据其位置获取图块,而不是在两个列表之间交叉引用它。

Tile[,] tiles = new Tile[,]
Tile tile = tiles[x,y];

此外,在这种情况下,数组数据结构将比使用列表更有效。

于 2013-06-18T17:39:06.333 回答
3

对于具有标准宽度和高度的统一图块集,很容易计算哪些图块在屏幕上可见,并确定您的角色与哪些图块重叠。尽管我在乔恩的回答中写了四叉树,但我认为这太过分了。一般情况下,公式为:

tileX = someXCoordinate % tileWidth;
tileY = someYCoordinate % tileHeight;

然后你可以在 2D array 中查找它tiles[tileX, tileY]。对于绘图,这可用于确定屏幕左上角的图块,然后对右下角 (+1) 再次执行相同操作,或者将图块添加到左上角以填充屏幕。然后你的循环看起来更像:

leftmostTile = screenX % tileWidth;    // screenX is the left edge of the screen in world coords
topmostTile = screenY % tileHeight;
rightmostTile = (screenX + screenWidth) % tileWidth;
bottommostTile = (screenY + screenHeight) % tileHeight;
for(int tileX = leftmostTile; tileX <= rightmostTile; tileX++)
{
    for(int tileY = topmostTile; tileY <= bottommostTile; tileY++)
    {
        Tile t = tiles[tileX][tileY];
        // ... more stuff
    }
}

可以使用相同的简单公式来快速确定哪些图块位于矩形区域下。

但是,如果您的图块不均匀,或者您有等轴测视图,或者您想要 QuadTree 提供的附加功能,我会考虑 Jon 的回答并使用QuadTree。如果可以的话,我会尽量让瓷砖远离四叉树。

于 2013-06-18T18:51:03.960 回答