【发布时间】:2013-06-18 17:09:02
【问题描述】:
我决定尝试使用 Xna 框架制作一款地牢爬行游戏。我是一名计算机科学专业的学生,对 c# 和 .net 框架非常熟悉。我对我的引擎开发的不同部分有一些疑问。
- 加载地图
我有一个 tile 类,它存储了 tile 的 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); } } }
问题: 这是我应该这样做的正确方式,还是应该只存储一个引用我的图块列表的列表?
我将如何处理多层地图?
- 碰撞检测
目前我有一个方法循环遍历存储在我的 testTiles 列表中的每个图块,并检查其尺寸是否与玩家尺寸相交,然后返回所有图块的列表。我有一个名为 CollisionTile 的瓦片类的派生类,当玩家和该矩形相交时会触发碰撞。 (公共类 CollisionTile : Tile)
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;
}
是的,我很确定这不是检查瓷砖碰撞的正确方法。任何帮助都会很棒。
抱歉,帖子太长了,我们将不胜感激。
【问题讨论】:
-
您应该只绘制玩家视野中的图块。至于多层,您可以使用多种方法,例如创建另一个类
TileLayer,其中包含带有 ZIndex 的List<TileRow>,您可以通过让每个图块包含具有 ZIndex 的实体来直接在图块上绘制或合并图层。跨度> -
请不要使用标签(例如 RPG),除非您了解其含义在此站点上。 RPG 是一种主要由专业程序员使用的编程语言,用于构建用于运行我们认为理所当然的大部分业务的软件。