假设您有一个2D网格图块(这是基于2D图块的游戏),大多数图块占据1个点,但是一些较大的“对象”可以填充多个点。我在我的数组上使用一个索引器来自动“引用”这些对象到它们的基本tile。因此,如果我在3,4上有一个2x2对象,并且我访问4,4,它将自动重定向并获得3,4的瓦片。但是,如果我需要获取确切的tile,我可以指定一个参数来绕过这个功能。 (关于GameDev的my old question更好的解释)
另一种看待它的方法是游戏世界中的门对象,用户可以点击它上面的任何地方打开它,但每个单独的部分可以包含其他属性,如不同的背景和光照值。
注意我只是一个业余爱好者程序员,所以这可能不对(我为什么寻求你的建议)每个“超大”磁贴将以X,Y
的形式存储对它的基本磁贴的引用position(这应该是对内存中实际对象的引用吗?)
public class TileWrapper
{
public int Width = 0;
public int Height = 0;
private Tile[] tiles; //Backing Store
public TileWrapper()
{
tiles = new Tile[Width * Height];
}
public TileWrapper(int width, int height)
{
Width = width;
Height = height;
tiles = new Tile[Width * Height];
}
/// <summary>
/// Accessor for tiles
/// </summary>
/// <param name="x">X Position</param>
/// <param name="y">Y Position</param>
/// <param name="overide">Bool to "override" the get, if true, it wont get the reference tile and will bypass the checks</param>
public Tile this[int x, int y, bool override = false]
{
get
{
//If we do not want to bypass the checks, AND the current tile is > than 1x1
if (!override && tiles[y * Width + x].IsLarge)
return tiles[tiles[y * Width + x].refY * Width + tiles[y * Width + x].refX]; //Use the reference positions to get the main position of the tile
//If we want to bypass the checks or the tile wasn't large, get the absolute position
else
return tiles[y * Width + x];
}
set
{
//Same thing for SET
if (!override && tiles[y * Width + x].IsLarge) //Set base tile if the large tile has a reference
tiles[tiles[y * Width + x].refY * Width + tiles[y * Width + x].refX] = value;
else //Set absolute tile
tiles[y * Width + x] = value;
}
}
很抱歉,如果使用2D到1D转换有点难以阅读,但在进行一些测试之后,看起来内部使用1D阵列会更快一点。
IsLarge
是一个简单检查瓷砖是否很大(大于1x1)的属性
我已经有了逻辑,可以在放置一个大瓷砖时填充相邻瓷砖的参考,并相应地删除它们。
在对游戏进行剖析时,我发现磁贴的获取访问器占用了大量CPU,每帧渲染数百次,用于照明,渲染,碰撞等。
如何提高此代码的性能和效率?
基准测试(英特尔四核i7 2670QM上平均30k次迭代)
Tile t = tiles[100, 100];
- 160 ns和175 ns WITH 2D Internal Array
Tile t = tiles[100, 100, true];
- 137 ns和264 ns WITH 2D Internal Array(odd)
100,100
不是一块大瓷砖,请注意这些瓷砖并不常见。我在屏幕上有一个房子,你可以有一些大的瓷砖(门,桌子),但有很多泥土/石头/木头。
答案 0 :(得分:0)
我建议对于逻辑上连接在一起的每个方块集合,您可以创建一个类似的实例:
private class TileRef { public Tile tile; public int X,Y,Width,Height;}
然后有一个2维或锯齿状的TileRef数组。连接在一起的组中的所有方块都应该包含对同一TileRef
个对象的引用。这应该可以让您快速找到与板上任何方块相关联的TileRef
,而无需条件逻辑来处理不同大小的区块。
构建一个大小为一的图块网格:
TileRef[,] tiles;
TileMapper(int xsize, int ysize)
{
tiles = new TileRef[xsize,ysize];
for (int x=0; x < xsize; x++)
for (int y=0; y < xsize; y++)
{
var thisRef = new TileRef();
thisRef.X = x;
thisRef.Y = y;
thisRef.Width = 1;
thisRef.Height = 1;
thisRef.Tile = new Tile(); // Make a default tile instance somehow
tiles[x][y] = thisRef;
}
}
To join a bunch of squares together into a blob:
public JoinSquares(int x, int y, int width, int height)
{
var thisRef = new TileRef();
thisRef.X = x;
thisRef.Y = y;
thisRef.Width = 1;
thisRef.Height = 1;
thisRef.Tile = new Tile(); // Make a default tile instance somehow
for (i=0; i<width; i++)
for (j=0; j<height; j++)
tiles[x+i,y+j] = thisRef;
}
public SeparateSquares(int x, int y)
{
var oldRef = tiles[x,y];
x=oldref.X;
y=oldref.Y;
var width=oldref.Width;
var height=oldref.Height;
for (i=0; i<width; i++)
for (j=0; j<height; j++)
{
var thisRef = new TileRef();
thisRef.X = x+i;
thisRef.Y = y+j;
thisRef.Width = 1;
thisRef.Height = 1;
thisRef.Tile = new Tile(); // Make a default tile instance somehow
tiles[x+i,y+j] = thisRef;
}
}
To change the `Tile` associated with a "blob", simply
public Tile this[int x, int y]
{
get
{
return tiles[x,y].Tile;
}
set
{
tiles[x,y].Tile = value;
}
}
需要循环才能将正方形连接在一起或将它们分开,但不能通过更改其Tile
属性来简单地更改与blob关联的属性。