Added time offset sync, added web version multiplayer

This commit is contained in:
Vladislav Khorev committed 2026-02-09 22:56:48 +03:00
1 parent a59bcc0c4b
commit 2c1c077611
13 files changed
+534 -324

No files matched your search

+28 -9
View File
@@ -16,7 +16,11 @@
#endif
#ifdef NETWORK
#ifdef EMSCRIPTEN
#include "network/WebSocketClientEmscripten.h"
#else
#include "network/WebSocketClient.h"
#endif
#else
#include "network/LocalClient.h"
#endif
@@ -389,7 +393,8 @@ namespace ZL
std::to_string(speedToSend) + ":" +
std::to_string(shotCount);
networkClient->Send(fireMsg);
//Temporary disable to avoid de-sync
//networkClient->Send(fireMsg);
}
});
@@ -486,8 +491,13 @@ namespace ZL
planetObject.init();
#ifdef NETWORK
#ifdef EMSCRIPTEN
networkClient = std::make_unique<WebSocketClientEmscripten>();
networkClient->Connect("localhost", 8081);
#else
networkClient = std::make_unique<WebSocketClient>(taskManager.getIOContext());
networkClient->Connect("127.0.0.1", 8080);
networkClient->Connect("127.0.0.1", 8081);
#endif
#else
networkClient = std::make_unique<LocalClient>();
networkClient->Connect("", 0);
@@ -870,13 +880,22 @@ namespace ZL
#endif
}
int64_t Game::getSyncTimeMs() {
int64_t localNow = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
std::cout << "getSyncTimeMs localNow = " << localNow << std::endl;
std::cout << "getSyncTimeMs getTimeOffset = " << networkClient->getTimeOffset() << std::endl;
// Добавляем смещение, полученное от сервера
return localNow + networkClient->getTimeOffset(); // Нужно добавить геттер в интерфейс
}
void Game::processTickCount() {
if (lastTickCount == 0) {
//lastTickCount = SDL_GetTicks64();
lastTickCount = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
lastTickCount = getSyncTimeMs();
lastTickCount = (lastTickCount / 50) * 50;
@@ -884,15 +903,13 @@ namespace ZL
}
//newTickCount = SDL_GetTicks64();
newTickCount = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
newTickCount = getSyncTimeMs();
newTickCount = (newTickCount / 50) * 50;
if (newTickCount - lastTickCount > CONST_TIMER_INTERVAL) {
size_t delta = newTickCount - lastTickCount;
int64_t delta = newTickCount - lastTickCount;
if (delta > CONST_MAX_TIME_INTERVAL)
{
//throw std::runtime_error("Synchronization is lost");
@@ -900,6 +917,8 @@ namespace ZL
auto now_ms = newTickCount;
std::cout << "processTickCount = " << now_ms << std::endl;
sparkEmitter.update(static_cast<float>(delta));
planetObject.update(static_cast<float>(delta));