基本上我正在尝试在 XNA 2D 中制作一个磁贴引擎,我目前正在使用一个大的磁贴列表(我的类保存磁贴的所有数据)并选择我的视图范围内的那些,然后将它们显示在屏幕上。
显然,我遇到的问题是,我的 Tiles 整体列表越大,我在尝试挑选范围内的 Tiles 时遇到的延迟就越多。我目前在 for 循环中使用 Linq 来选择图块,如下所示:
//loop from the maximum view at the top of the screen to the maximum view at the bottom
for (int height = topView; height < bottomView; height++)
{
//loop from the maximum view to the left of the screen to the maximum view of the right
for (int width = leftView; width < rightView; width++)
{
//select the tile at this grid position
Tile t = tileList.Where(x => x.GridPosition == new Vector2(width, height)).FirstOrDefault();
if (t != null)
{
//calculate the screen position and add it to the list of local tiles within range
t.ScreenPosition = new Vector2(screenWidth * 30, screenHeight * 30);
localList.Add(t);
}
else
{
//the tile wasn't found, add a random blank one to fill the gap.
Tile brokenTile = new Tile(game, new Vector2(width, height), 9001);
brokenTile.ScreenPosition = new Vector2(screenWidth * 30, screenHeight * 30);
localList.Add(brokenTile);
}
//increment the screen width used to calculate the screen position
screenWidth++;
}
//increment the screen height used to calculate the screen position and reset the width
screenHeight++;
screenWidth = 1;
}
我想知道是否有一种方法可以更有效地做到这一点,理想情况下可以减少在增加“地图”的整体大小并选择范围内的这些图块时所经历的滞后。
我唯一能想到的是在加载地图时将总列表分成“块”的某种方法,并且只查看每个块以拉出瓷砖..但是我不太确定我会怎么做因为如果我需要从多个“块”中拉出节点可能会很麻烦但是任何关于这样做的好方法的帮助也会很棒!
非常感谢!:)
编辑:这里有一些截图:http ://i.imgur.com/RJmSYYF.png 与http://i.imgur.com/LgwB8CJ.png相比