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

View File

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

View File

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

View File

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

View File

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

View File

@ -34,6 +34,7 @@ namespace ZL {
public:
virtual ~INetworkClient() = default;
virtual void Connect(const std::string& host, uint16_t port) = 0;
virtual void Disconnect() {}
virtual void Send(const std::string& message) = 0;
virtual bool IsConnected() const = 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() {
ws_->async_read(buffer_, [this](boost::beast::error_code ec, std::size_t bytes) {
if (!ec) {

View File

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

View File

@ -26,6 +26,15 @@ namespace ZL {
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() {
std::lock_guard<std::mutex> lock(outgoingMutex);
if (!socket_) return;

View File

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