我正在开发一款多人 Flash 游戏。服务器通知每个客户端有哪些其他玩家在玩家附近。为此,服务器必须不断检查哪些客户端彼此靠近。以下是我目前正在使用的临时解决方案:
private function checkVisibilities()
{
foreach ($this->socketClients as $socketClient1)
{ //loop every socket client
if (($socketClient1->loggedIn()) && ($socketClient1->inWorld()))
{ //if this client is logged in and in the world
foreach ($this->socketClients as $cid2 => $socketClient2)
{ //loop every client for this client to see if they are near
if ($socketClient1 != $socketClient2)
{ //if it is not the same client
if (($socketClient2->loggedIn()) && ($socketClient2->inWorld())
{ //if this client is also logged in and also in the world
if ((abs($socketClient1->getCharX() - $socketClient2->getCharX()) + abs($socketClient1->getCharY() - $socketClient2->getCharY())) < Settings::$visibilities_range)
{ //the clients are near each other
if (!$socketClient1->isVisible($cid2))
{ //not yet visible -> add
$socketClient1->addVisible($cid2);
}
}
else
{ //the clients are not near each other
if ($socketClient1->isVisible($cid2))
{ //still visible -> remove
$socketClient1->removeVisible($cid2);
}
}
}
else
{ //the client is not logged in
if ($socketClient1->isVisible($cid2))
{ //still visible -> remove
$socketClient1->removeVisible($cid2);
}
}
}
}
}
}
它工作正常。然而,到目前为止,我一次只和两个玩家一起玩。此函数为每个客户端循环每个客户端。因此,对于 100 名玩家,每次运行该函数时将有 100 * 100 = 10.000 次循环。这似乎不是最好或最有效的方法。
现在我想知道你们对我目前的设置有什么看法,以及你们是否有任何关于更好地处理这些可见性的建议。
更新:我忘了提到世界是无限的。它实际上是“宇宙”。没有地图。此外,它是一个二维 (2D) 游戏。
提前致谢。