diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index 26aa534d..7a40f6da 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -212,6 +212,69 @@ namespace Systems { } } + static bool isGameWin() + { + Registry ®istry = Registry::getInstance(); + std::vector ids = + registry.getEntitiesByComponents({typeid(Types::Player), typeid(Types::OtherPlayer)}); + + if (ids.empty()) { + return false; + } + return true; + } + + static void modifEndGameText(const std::string &endGameMessage) + { + bool found = false; + const Raylib::Vector2 pos = {0, 2}; + constexpr std::size_t fontSize = 2; + const std::string textKeywordWaveEnd = "WaveText"; + const std::string textKeyWordGameEnd = "endGameText"; + + std::vector ids = + Registry::getInstance().getEntitiesByComponents({typeid(Raylib::Text)}); + auto &textArray = Registry::getInstance().getComponents(); + + for (auto &id : ids) { + if (textArray[id].getKeyword() == textKeyWordGameEnd) { + textArray[id].setText(endGameMessage); + found = true; + } + if (textArray[id].getKeyword() == textKeywordWaveEnd) { + Registry::getInstance().removeEntity(id); + } + } + + if (found == false) { + Registry::getInstance().addEntity(); + Raylib::Text endGameText = + Raylib::Text(endGameMessage, pos, fontSize, Raylib::WHITE, textKeyWordGameEnd); + Registry::getInstance().getComponents().insertBack(endGameText); + } + } + + void EventsSystems::handleEndGameEvent(std::size_t /*unused*/, std::size_t /*unused*/) + { + constexpr std::size_t secondBeforeEnd = 5; + static std::size_t clockId = Registry::getInstance().getClock().create(false); + std::size_t elapsedSeconds = Registry::getInstance().getClock().elapsedSecondsSince(clockId); + std::string seconds = std::to_string(secondBeforeEnd - elapsedSeconds); + std::string endGameMessage; + + if (isGameWin() == true) { + endGameMessage = "You win! Redirecting to menu in " + seconds + " seconds"; + } else { + endGameMessage = "You lose! Redirecting to menu in " + seconds + " seconds"; + } + + modifEndGameText(endGameMessage); + + if (Registry::getInstance().getClock().elapsedSecondsSince(clockId) >= secondBeforeEnd) { + Scene::SceneManager::getInstance().changeScene(Scene::Scene::MENU); + } + } + std::vector> EventsSystems::getEventSystems() { return {playerMovement, changeScene, playerShootBullet}; diff --git a/src/Client/Systems/Events/EventsSystems.hpp b/src/Client/Systems/Events/EventsSystems.hpp index 826f8a4b..abed988b 100644 --- a/src/Client/Systems/Events/EventsSystems.hpp +++ b/src/Client/Systems/Events/EventsSystems.hpp @@ -17,6 +17,7 @@ namespace Systems { void playerMovement(std::size_t /*unused*/, std::size_t /*unused*/); void changeScene(std::size_t /*unused*/, std::size_t /*unused*/); void shootBullet(std::size_t /*unused*/, std::size_t /*unused*/); + void handleEndGameEvent(std::size_t /*unused*/, std::size_t /*unused*/); std::vector> getEventSystems(); } // namespace EventsSystems } // namespace Systems diff --git a/src/Client/Systems/Network/ClientNetwork.cpp b/src/Client/Systems/Network/ClientNetwork.cpp index b7efca52..6b8a1f9f 100644 --- a/src/Client/Systems/Network/ClientNetwork.cpp +++ b/src/Client/Systems/Network/ClientNetwork.cpp @@ -8,6 +8,7 @@ #include "SceneManager.hpp" #include "SystemManagersDirector.hpp" #include "Systems.hpp" +#include "EventsSystems.hpp" namespace Systems { void receiveLifeUpdate(std::any &any, boost::asio::ip::udp::endpoint & /* unused */) @@ -272,9 +273,12 @@ namespace Systems { } } - void receiveEndGame(std::any &any, boost::asio::ip::udp::endpoint &endpoint) + void receiveEndGame(std::any& /*unused*/, boost::asio::ip::udp::endpoint& /*unused*/) { - Logger::info("End game received"); + auto &director = SystemManagersDirector::getInstance(); + std::lock_guard lock(director.mutex); + director.getSystemManager(static_cast(Scene::SystemManagers::EVENTS)) + .addSystem(EventsSystems::handleEndGameEvent); } std::vector> getNetworkSystems() diff --git a/src/ECS/ECSCustomTypes.cpp b/src/ECS/ECSCustomTypes.cpp index e58ff365..c66b9a94 100644 --- a/src/ECS/ECSCustomTypes.cpp +++ b/src/ECS/ECSCustomTypes.cpp @@ -130,8 +130,8 @@ namespace Types { } } Registry::getInstance().addEntity(); - Raylib::Text test = Raylib::Text(text, {20, 10}, fontSize, Raylib::WHITE, textKeyword); - Registry::getInstance().getComponents().insertBack(test); + Raylib::Text endWaveText = Raylib::Text(text, {20, 10}, fontSize, Raylib::WHITE, textKeyword); + Registry::getInstance().getComponents().insertBack(endWaveText); } #endif _waitingForNextWave = value; diff --git a/src/ECS/ECSCustomTypes.hpp b/src/ECS/ECSCustomTypes.hpp index caa1ad54..32d03b16 100644 --- a/src/ECS/ECSCustomTypes.hpp +++ b/src/ECS/ECSCustomTypes.hpp @@ -220,7 +220,7 @@ namespace Types { std::lock_guard lock(_mutex); } - Enemy(struct enemy_id_s _constId, enum enemy_type_e _type) : constId(_constId), type(_type) + Enemy(struct enemy_id_s _constId, enum enemy_type_e _type) : type(_type), constId(_constId) { } @@ -250,8 +250,8 @@ namespace Types { return _enemyNb; } - enemy_id_s constId; enum enemy_type_e type; + enemy_id_s constId; private: static unsigned int _enemyNb; diff --git a/src/Nitwork/ANitwork.hpp b/src/Nitwork/ANitwork.hpp index 41848d5a..a22350a2 100644 --- a/src/Nitwork/ANitwork.hpp +++ b/src/Nitwork/ANitwork.hpp @@ -8,7 +8,6 @@ #pragma once #include -#include #include #include #include diff --git a/src/Nitwork/NitworkServer.cpp b/src/Nitwork/NitworkServer.cpp index a2679a5b..0d4a8fe2 100644 --- a/src/Nitwork/NitworkServer.cpp +++ b/src/Nitwork/NitworkServer.cpp @@ -5,6 +5,7 @@ ** NitworkServer */ +#include #include "NitworkServer.hpp" #include "ECSCustomTypes.hpp" #include "Logger.hpp" diff --git a/src/Server/Systems/WaveSystem.cpp b/src/Server/Systems/WaveSystem.cpp index 5a08ba30..3e9a4345 100644 --- a/src/Server/Systems/WaveSystem.cpp +++ b/src/Server/Systems/WaveSystem.cpp @@ -13,8 +13,6 @@ #include "SystemManager.hpp" #include "Systems.hpp" -#include - std::size_t Wave::_clockId = 0; std::mutex Wave::_mutex; @@ -103,7 +101,6 @@ namespace Systems { std::lock_guard lock(registry.mutex); if (waveHandler.isGameEnded()) { - std::cout << "------------------ GAME ENDED ------------------" << std::endl; Nitwork::NitworkServer::getInstance().addEndGameMsg(); SystemManagersDirector::getInstance().getSystemManager(managerId).removeSystem(systemId); } diff --git a/src/Server/Systems/WaveSystem.hpp b/src/Server/Systems/WaveSystem.hpp index 5d1be4f0..ef70959b 100644 --- a/src/Server/Systems/WaveSystem.hpp +++ b/src/Server/Systems/WaveSystem.hpp @@ -27,8 +27,8 @@ class Wave { private: std::vector _wavesId; - std::size_t _msBeforeNextWave; int _waveIndex; + std::size_t _msBeforeNextWave; bool _isGameEnded; bool _isTimeBetweenWaves; static std::mutex _mutex;