我正在制作一个基于回合的六角网格游戏。玩家选择单位并在六角网格中移动它们。网格中的每个图块都属于特定的地形类型(例如沙漠、丘陵、山脉等),并且每种单位类型在地形上移动时具有不同的能力(例如,有些可以轻松越过山脉,有些比较困难,有些一点也不)。
每个单位都有一个移动值,每个图块根据其地形类型和单位类型进行一定的移动。例如,在沙漠上移动需要花费 1 辆坦克,在沼泽上需要花费 4 辆,并且无法在所有山脉上移动。飞行单位以 1 为代价移动所有东西。
我遇到的问题是,当一个单位被选中时,我想突出显示它周围的一个区域,显示它可以移动的位置,这意味着计算出通过周围六角形的所有可能路径,每条路径将采取多少移动并点亮基于该信息的瓷砖。
我用递归函数得到了这个,发现计算时间太长了,我把函数移动到一个线程中,这样它就不会阻塞游戏,但线程仍然需要大约 2 秒来计算一个可移动区域单位移动为 8。它的递归超过一百万次,这显然是有问题的。
我想知道是否有人对如何优化这个问题有聪明的想法。
这是我目前正在使用的递归函数(它的 C# 顺便说一句):
private void CalcMoveGridRecursive(int nCenterIndex, int nMoveRemaining)
{
//List of the 6 tiles adjacent to the center tile
int[] anAdjacentTiles = m_ThreadData.m_aHexData[nCenterIndex].m_anAdjacentTiles;
foreach(int tileIndex in anAdjacentTiles)
{
//make sure this adjacent tile exists
if(tileIndex == -1)
continue;
//How much would it cost the unit to move onto this adjacent tile
int nMoveCost = m_ThreadData.m_anTerrainMoveCost[(int)m_ThreadData.m_aHexData[tileIndex].m_eTileType];
if(nMoveCost != -1 && nMoveCost <= nMoveRemaining)
{
//Make sure the adjacent tile isnt already in our list.
if(!m_ThreadData.m_lPassableTiles.Contains(tileIndex))
m_ThreadData.m_lPassableTiles.Add(tileIndex);
//Now check the 6 tiles surrounding the adjacent tile we just checked (it becomes the new center).
CalcMoveGridRecursive(tileIndex, nMoveRemaining - nMoveCost);
}
}
}
在递归结束时,m_lPassableTiles 包含该单元可能到达的所有图块的索引列表,并且它们会发光。
这一切都有效,只是时间太长了。有谁知道更好的方法?