저는 멀티 플레이어 플래시 게임을하고 있습니다. 서버는 각 클라이언트에게 다른 플레이어가 플레이어 근처에 무엇이 있는지 알려줍니다. 이를 위해 서버는 어떤 클라이언트가 서로 가까이 있는지 확인해야합니다. 다음은 임시 솔루션으로 현재이 순간에 사용하고있는 내용입니다.서버가 게임 클라이언트에게 주변에 보이는 다른 플레이어를 효율적으로 알리는 방법?
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);
}
}
}
}
}
}
잘 작동합니다. 그러나 지금까지 한 번에 2 명의 플레이어 만 플레이했습니다. 이 함수는 모든 클라이언트에 대해 모든 클라이언트를 루핑합니다. 그래서 100 명의 플레이어가 100 * 100 = 10.000 루프가 될 때마다 기능이 실행됩니다. 이것은 최선 또는 가장 효율적인 방법으로 보이지 않습니다.
이제는 여러분이 내 현재 설정에 대해 어떻게 생각하는지, 그리고 이러한 가시성을 다루는 더 좋은 방법에 대한 제안이 있다면 궁금합니다.
업데이트 : 전 세계가 무한하다는 것을 잊었습니다. 그것은 실제로 "우주"입니다. 지도가 없습니다. 또한 2 차원 게임입니다.
미리 감사드립니다.
_|____|____|____|_
| | | |
_|____|____|____|_
| | | |
_|____|____|____|_
| | | |
_|____|____|____|_
| | | |
을 그리고 서로 교차 어떤 격자 타일로 당신의 개체 삽입 :
이제_|____|____|____|_
| @ | | |
_|____|____|____|_
| |d d | |
_|____|____|____|_
| | d | d |
_|____|____|____|_
| | | |
이 할을
추가하는 것을 잊어 버렸습니다. 내 게임의 세계가 무한합니다. 지도가 없습니다. – Tom
당신의 대답에 영향을 미칩니 까? – Tom
X 위치 주변의 고정 된 공간 대신 전체 우주를 '근처'로 표시하면 그 대답에 영향을 미칠 것입니다.이 답변은 여전히 유효하다고 생각합니다. – pyrocumulus