Fixing bugs

This commit is contained in:
Vladislav Khorev 2026-03-06 23:15:56 +03:00
parent 41f370f2ee
commit 7ccda081b0
10 changed files with 50 additions and 11 deletions

View File

@ -146,6 +146,13 @@ namespace ZL
menuManager.setupMenu(); menuManager.setupMenu();
menuManager.onMainMenuEntered = [this]() {
if (networkClient) {
networkClient->Disconnect();
networkClient.reset();
}
};
menuManager.onSingleplayerPressed = [this](const std::string& nickname, int shipType) { menuManager.onSingleplayerPressed = [this](const std::string& nickname, int shipType) {
Environment::shipState.nickname = nickname; Environment::shipState.nickname = nickname;
Environment::shipState.shipType = shipType; Environment::shipState.shipType = shipType;
@ -169,6 +176,7 @@ namespace ZL
networkClient = std::unique_ptr<INetworkClient>(localClient); networkClient = std::unique_ptr<INetworkClient>(localClient);
networkClient->Connect("", 0); networkClient->Connect("", 0);
space.resetPlayerState();
lastTickCount = 0; lastTickCount = 0;
}; };
@ -195,6 +203,7 @@ namespace ZL
space.boxAlive.clear(); space.boxAlive.clear();
space.serverBoxesApplied = false; space.serverBoxesApplied = false;
space.resetPlayerState();
connectingStartTicks = SDL_GetTicks(); connectingStartTicks = SDL_GetTicks();
lastTickCount = 0; lastTickCount = 0;
}; };

View File

@ -35,6 +35,8 @@ namespace ZL {
state = GameState::MainMenu; state = GameState::MainMenu;
uiManager.replaceRoot(mainMenuRoot); uiManager.replaceRoot(mainMenuRoot);
if (onMainMenuEntered) onMainMenuEntered();
uiManager.setButtonCallback("singleButton", [this](const std::string&) { uiManager.setButtonCallback("singleButton", [this](const std::string&) {
enterShipSelectionSingle(); enterShipSelectionSingle();
}); });

View File

@ -66,6 +66,7 @@ namespace ZL {
void notifyConnectionFailed(); void notifyConnectionFailed();
// Callbacks set by Game/Space // Callbacks set by Game/Space
std::function<void()> onMainMenuEntered;
std::function<void()> onRestartPressed; std::function<void()> onRestartPressed;
std::function<void(float)> onVelocityChanged; std::function<void(float)> onVelocityChanged;
std::function<void()> onFirePressed; std::function<void()> onFirePressed;

View File

@ -252,20 +252,26 @@ namespace ZL
Space::~Space() { Space::~Space() {
} }
void Space::setup() { void Space::resetPlayerState()
{
shipAlive = true;
menuManager.onRestartPressed = [this]() { gameOver = false;
this->shipAlive = true; showExplosion = false;
this->gameOver = false; explosionEmitter.setEmissionPoints(std::vector<Vector3f>());
this->showExplosion = false;
this->explosionEmitter.setEmissionPoints(std::vector<Vector3f>());
Environment::shipState.position = Vector3f{ 0, 0, 45000.f }; Environment::shipState.position = Vector3f{ 0, 0, 45000.f };
Environment::shipState.velocity = 0.0f; Environment::shipState.velocity = 0.0f;
Environment::shipState.rotation = Eigen::Matrix3f::Identity(); Environment::shipState.rotation = Eigen::Matrix3f::Identity();
Environment::inverseShipMatrix = Eigen::Matrix3f::Identity(); Environment::inverseShipMatrix = Eigen::Matrix3f::Identity();
Environment::zoom = DEFAULT_ZOOM; Environment::zoom = DEFAULT_ZOOM;
Environment::tapDownHold = false; Environment::tapDownHold = false;
playerScore = 0;
}
void Space::setup() {
menuManager.onRestartPressed = [this]() {
resetPlayerState();
if (networkClient) { if (networkClient) {
try { try {
@ -276,7 +282,6 @@ namespace ZL
std::cerr << "Client: Failed to send RESPAWN\n"; std::cerr << "Client: Failed to send RESPAWN\n";
} }
} }
this->playerScore = 0;
std::cerr << "Game restarted\n"; std::cerr << "Game restarted\n";
}; };

View File

@ -141,6 +141,7 @@ namespace ZL {
void drawTargetHud(); // рисует рамку или стрелку void drawTargetHud(); // рисует рамку или стрелку
int pickTargetId() const; // ???????? ???? (????: ????????? ????? ????????? ?????) int pickTargetId() const; // ???????? ???? (????: ????????? ????? ????????? ?????)
void resetPlayerState();
void clearTextRendererCache(); void clearTextRendererCache();
// Crosshair HUD // Crosshair HUD

View File

@ -34,6 +34,7 @@ namespace ZL {
public: public:
virtual ~INetworkClient() = default; virtual ~INetworkClient() = default;
virtual void Connect(const std::string& host, uint16_t port) = 0; virtual void Connect(const std::string& host, uint16_t port) = 0;
virtual void Disconnect() {}
virtual void Send(const std::string& message) = 0; virtual void Send(const std::string& message) = 0;
virtual bool IsConnected() const = 0; virtual bool IsConnected() const = 0;
virtual void Poll() = 0; // ƒл¤ обработки вход¤щих пакетов virtual void Poll() = 0; // ƒл¤ обработки вход¤щих пакетов

View File

@ -28,6 +28,15 @@ namespace ZL {
} }
} }
void WebSocketClient::Disconnect() {
if (!ws_ || !connected) return;
connected = false;
try {
boost::beast::get_lowest_layer(*ws_).cancel();
}
catch (...) {}
}
void WebSocketClient::startAsyncRead() { void WebSocketClient::startAsyncRead() {
ws_->async_read(buffer_, [this](boost::beast::error_code ec, std::size_t bytes) { ws_->async_read(buffer_, [this](boost::beast::error_code ec, std::size_t bytes) {
if (!ec) { if (!ec) {

View File

@ -54,6 +54,7 @@ namespace ZL {
{} {}
void Connect(const std::string& host, uint16_t port) override; void Connect(const std::string& host, uint16_t port) override;
void Disconnect() override;
void Poll() override; void Poll() override;

View File

@ -26,6 +26,15 @@ namespace ZL {
connected = false; connected = false;
} }
void WebSocketClientEmscripten::Disconnect() {
if (socket_ > 0) {
emscripten_websocket_close(socket_, 1000, "User disconnected");
emscripten_websocket_delete(socket_);
socket_ = 0;
}
connected = false;
}
void WebSocketClientEmscripten::flushOutgoingQueue() { void WebSocketClientEmscripten::flushOutgoingQueue() {
std::lock_guard<std::mutex> lock(outgoingMutex); std::lock_guard<std::mutex> lock(outgoingMutex);
if (!socket_) return; if (!socket_) return;

View File

@ -28,6 +28,7 @@ namespace ZL {
virtual ~WebSocketClientEmscripten() = default; virtual ~WebSocketClientEmscripten() = default;
void Connect(const std::string& host, uint16_t port) override; void Connect(const std::string& host, uint16_t port) override;
void Disconnect() override;
void Send(const std::string& message) override; void Send(const std::string& message) override;
void Poll() override; void Poll() override;