Major refactoring for game menu

This commit is contained in:
Vladislav Khorev committed 2026-02-22 19:50:13 +03:00
1 parent 5b57696acf
commit b59a10b7e6
8 files changed
+142 -103

No files matched your search

+96 -33
View File
@@ -21,10 +21,11 @@
#else
#include "network/WebSocketClient.h"
#endif
#else
#include "network/LocalClient.h"
#endif
#include "network/LocalClient.h"
namespace ZL
{
#ifdef EMSCRIPTEN
@@ -34,12 +35,6 @@ namespace ZL
const char* CONST_ZIP_FILE = "";
#endif
bool g_exitBgAnimating = false;
bool firePressed = false;
float x = 0;
Game::Game()
: window(nullptr)
, glContext(nullptr)
@@ -88,21 +83,30 @@ namespace ZL
#endif
menuManager.setupMenu();
renderer.InitOpenGL();
space.setup();
menuManager.onSingleplayerPressed = [this]() {
networkClient = std::make_unique<LocalClient>();
networkClient->Connect("", 0);
spaceGameStarted = 1;
};
menuManager.onMultiplayerPressed = [this]() {
#ifdef NETWORK
#ifdef EMSCRIPTEN
networkClient = std::make_unique<WebSocketClientEmscripten>();
networkClient->Connect("192.168.131.143", 8081);
networkClient = std::make_unique<WebSocketClientEmscripten>();
networkClient->Connect("192.168.131.143", 8081);
#else
networkClient = std::make_unique<WebSocketClient>(taskManager.getIOContext());
networkClient->Connect("127.0.0.1", 8081);
networkClient = std::make_unique<WebSocketClient>(taskManager.getIOContext());
networkClient->Connect("127.0.0.1", 8081);
#endif
#else
networkClient = std::make_unique<LocalClient>();
networkClient->Connect("", 0);
#endif
lastTickCount = 0;
spaceGameStarted = 1;
};
renderer.InitOpenGL();
space.setup();
}
@@ -131,9 +135,21 @@ namespace ZL
CheckGlError();
}
void Game::drawScene() {
void Game::drawUnderMainMenu()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
space.drawScene();
}
void Game::drawScene() {
if (spaceGameStarted) {
space.drawScene();
}
else
{
drawUnderMainMenu();
}
drawUI();
CheckGlError();
}
@@ -142,7 +158,14 @@ namespace ZL
int64_t localNow = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
// Добавляем смещение, полученное от сервера
return localNow + networkClient->getTimeOffset(); // Нужно добавить геттер в интерфейс
if (networkClient)
{
return localNow + networkClient->getTimeOffset();
}
else
{
return localNow;
}
}
void Game::processTickCount() {
@@ -167,8 +190,9 @@ namespace ZL
//throw std::runtime_error("Synchronization is lost");
}
space.processTickCount(newTickCount, delta);
if (spaceGameStarted) {
space.processTickCount(newTickCount, delta);
}
menuManager.uiManager.update(static_cast<float>(delta));
lastTickCount = newTickCount;
}
@@ -268,14 +292,8 @@ namespace ZL
}
if (event.type == SDL_KEYUP) {
if (event.key.keysym.sym == SDLK_i) {
x = x + 1;
}
if (event.key.keysym.sym == SDLK_k) {
x = x - 1;
}
if (event.key.keysym.sym == SDLK_a) {
Environment::shipState.position = { 9466.15820, 1046.00159, 18531.2090 };
//Environment::shipState.position = { 9466.15820, 1046.00159, 18531.2090 };
}
}
#endif
@@ -293,22 +311,67 @@ namespace ZL
}
mainThreadHandler.processMainThreadTasks();
space.update();
if (spaceGameStarted) {
space.update();
}
}
void Game::handleDown(int mx, int my)
{
space.handleDown(mx, my);
int uiX = mx;
int uiY = Environment::height - my;
menuManager.uiManager.onMouseDown(uiX, uiY);
bool uiHandled = false;
for (const auto& button : menuManager.uiManager.findButton("") ? std::vector<std::shared_ptr<UiButton>>{} : std::vector<std::shared_ptr<UiButton>>{}) {
(void)button;
}
auto pressedSlider = [&]() -> std::shared_ptr<UiSlider> {
for (const auto& slider : menuManager.uiManager.findSlider("") ? std::vector<std::shared_ptr<UiSlider>>{} : std::vector<std::shared_ptr<UiSlider>>{}) {
(void)slider;
}
return nullptr;
}();
if (!menuManager.uiManager.isUiInteraction()) {
if (spaceGameStarted) {
space.handleDown(mx, my);
}
}
}
void Game::handleUp(int mx, int my)
{
space.handleUp(mx, my);
int uiX = mx;
int uiY = Environment::height - my;
menuManager.uiManager.onMouseUp(uiX, uiY);
if (!menuManager.uiManager.isUiInteraction()) {
if (spaceGameStarted) {
space.handleUp(mx, my);
}
}
}
void Game::handleMotion(int mx, int my)
{
space.handleMotion(mx, my);
int uiX = mx;
int uiY = Environment::height - my;
menuManager.uiManager.onMouseMove(uiX, uiY);
if (!menuManager.uiManager.isUiInteraction()) {
if (spaceGameStarted) {
space.handleMotion(mx, my);
}
}
}