diff --git a/server/server.cpp b/server/server.cpp index 1853f88..27ea090 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -155,7 +155,7 @@ int main() { try { net::io_context ioc; tcp::acceptor acceptor{ ioc, {tcp::v4(), 8080} }; - int next_id = 1000; + int next_id = 0; std::cout << "Server started on port 8080...\n"; diff --git a/src/Game.cpp b/src/Game.cpp index e849a84..cad4592 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -656,6 +656,7 @@ namespace ZL } drawShip(); drawRemoteShips(); + drawRemoteShipsLabels(); drawBoxes(); drawBoxesLabels(); @@ -729,6 +730,59 @@ namespace ZL CheckGlError(); } + void Game::drawRemoteShipsLabels() + { + if (!textRenderer) return; + + #ifdef NETWORK + // 2D поверх 3D + glDisable(GL_DEPTH_TEST); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + // Берем удаленных игроков + latestRemotePlayers = networkClient->getRemotePlayers(); + + auto now = std::chrono::system_clock::now(); + now -= std::chrono::milliseconds(CLIENT_DELAY); + + for (auto const& [id, remotePlayer] : latestRemotePlayers) + { + if (!remotePlayer.canFetchClientStateAtTime(now)) + continue; + + ClientState st = remotePlayer.fetchClientStateAtTime(now); + + // Позиция корабля в мире + Vector3f shipWorld = st.position; + + // Значение положения лейбла + Vector3f labelWorld = shipWorld + Vector3f{ 0.0f, -5.0f, 0.0f }; + + float sx, sy, depth; + if (!worldToScreen(labelWorld, sx, sy, depth)) + continue; + + float uiX = sx; + float uiY = sy; + + // Масштаб по расстоянию + float dist = (Environment::shipState.position - shipWorld).norm(); + float scale = std::clamp(140.0f / (dist + 1.0f), 0.6f, 1.2f); + + // Дефолтный лейбл + std::string label = "Player (" + std::to_string(st.id) + ")"; + + // TODO: nickname sync + + textRenderer->drawText(label, uiX, uiY, scale, true); + } + + glDisable(GL_BLEND); + glEnable(GL_DEPTH_TEST); + #endif + } + void Game::processTickCount() { if (lastTickCount == 0) { @@ -749,7 +803,7 @@ namespace ZL size_t delta = newTickCount - lastTickCount; if (delta > CONST_MAX_TIME_INTERVAL) { - throw std::runtime_error("Synchronization is lost"); + //throw std::runtime_error("Synchronization is lost"); } auto now_ms = newTickCount; diff --git a/src/Game.h b/src/Game.h index 5488563..0c20b82 100644 --- a/src/Game.h +++ b/src/Game.h @@ -50,6 +50,7 @@ namespace ZL { void drawBoxesLabels(); void drawUI(); void drawRemoteShips(); + void drawRemoteShipsLabels(); void fireProjectiles(); bool worldToScreen(const Vector3f& world, float& outX, float& outY, float& outDepth) const;