我决定尝试使用 Xna 框架制作一个地牢爬行游戏。我是一名计算机科学专业的学生,对 c# 和 .net 框架非常熟悉。我对我的引擎开发的不同部分有一些疑问。
- 加载地图
我有一个磁贴类,用于存储磁贴的 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 :平铺)
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;
}
是的,我很确定这不是检查瓷砖碰撞的正确方法。任何帮助都会很棒。
对不起,很长的帖子,任何帮助将不胜感激。