我正在开发一个游戏服务器,目前我需要能够在一个区域内吸引观众,但是我担心我正在使用的那个真的很丑而且“慢”,但我还没有遇到任何性能问题,因为我正在测试它本地不在实时服务器上。
这是我的 GetSpectators 函数:
public void GetSpectators(ref HashSet<Player> Players, Coordinate_t coordinate, bool MultiFloor = false)
{
for (int x = coordinate.X - 11; x != coordinate.X + 11; x++)
{
for (int y = coordinate.Y - 11; y != coordinate.Y + 11; y++)
{
if (MultiFloor)
{
for (int z = coordinate.Z - 2; z != coordinate.Z + 2; z++)
{
Tile tile = GetTile(x, y, z);
if (tile != null)
{
foreach (Player p in tile.Creatures)
{
Players.Add(p);
}
}
}
}
else
{
Tile tile = GetTile(x, y, coordinate.Z);
if (tile != null)
{
foreach (Player p in tile.Creatures)
{
Players.Add(p);
}
}
}
}
}
}
我有这个类 Map ,它包含另一个带有 Tile 类的字典,每个图块都用 X、Y 和 Z 坐标表示,每个图块都包含一个名为 Player 的类的列表,有些图块有玩家,有些则没有。
我需要一个好方法而不是丑陋的方法来获得例如:
例如,半径 11 内 x=100、y=100、z=7 内的所有玩家。