Skip to content

Commit

Permalink
Merge branch 'freature/RB-159-enhance-waves' of github.com:X-R-G-B/R-…
Browse files Browse the repository at this point in the history
…Bus into freature/RB-159-enhance-waves
  • Loading branch information
romainpanno committed Nov 2, 2023
2 parents 4b6e440 + 4052022 commit 4fae04c
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 11 deletions.
63 changes: 63 additions & 0 deletions src/Client/Systems/Events/EventsSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,69 @@ namespace Systems {
}
}

static bool isGameWin()
{
Registry &registry = Registry::getInstance();
std::vector<std::size_t> 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<std::size_t> ids =
Registry::getInstance().getEntitiesByComponents({typeid(Raylib::Text)});
auto &textArray = Registry::getInstance().getComponents<Raylib::Text>();

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<Raylib::Text>().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<std::function<void(std::size_t, std::size_t)>> EventsSystems::getEventSystems()
{
return {playerMovement, changeScene, playerShootBullet};
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Events/EventsSystems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::function<void(std::size_t, std::size_t)>> getEventSystems();
} // namespace EventsSystems
} // namespace Systems
8 changes: 6 additions & 2 deletions src/Client/Systems/Network/ClientNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */)
Expand Down Expand Up @@ -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<std::mutex> lock(director.mutex);
director.getSystemManager(static_cast<std::size_t>(Scene::SystemManagers::EVENTS))
.addSystem(EventsSystems::handleEndGameEvent);
}

std::vector<std::function<void(std::size_t, std::size_t)>> getNetworkSystems()
Expand Down
4 changes: 2 additions & 2 deletions src/ECS/ECSCustomTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ namespace Types {
}
}
Registry::getInstance().addEntity();
Raylib::Text test = Raylib::Text(text, {20, 10}, fontSize, Raylib::WHITE, textKeyword);
Registry::getInstance().getComponents<Raylib::Text>().insertBack(test);
Raylib::Text endWaveText = Raylib::Text(text, {20, 10}, fontSize, Raylib::WHITE, textKeyword);
Registry::getInstance().getComponents<Raylib::Text>().insertBack(endWaveText);
}
#endif
_waitingForNextWave = value;
Expand Down
4 changes: 2 additions & 2 deletions src/ECS/ECSCustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ namespace Types {
std::lock_guard<std::mutex> 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)
{
}

Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/Nitwork/ANitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#pragma once

#include <condition_variable>
#include <iostream>
#include <list>
#include <memory>
#include <mutex>
Expand Down
1 change: 1 addition & 0 deletions src/Nitwork/NitworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
** NitworkServer
*/

#include <iostream>
#include "NitworkServer.hpp"
#include "ECSCustomTypes.hpp"
#include "Logger.hpp"
Expand Down
3 changes: 0 additions & 3 deletions src/Server/Systems/WaveSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
#include "SystemManager.hpp"
#include "Systems.hpp"

#include <iostream>

std::size_t Wave::_clockId = 0;
std::mutex Wave::_mutex;

Expand Down Expand Up @@ -103,7 +101,6 @@ namespace Systems {
std::lock_guard<std::mutex> lock(registry.mutex);

if (waveHandler.isGameEnded()) {
std::cout << "------------------ GAME ENDED ------------------" << std::endl;
Nitwork::NitworkServer::getInstance().addEndGameMsg();
SystemManagersDirector::getInstance().getSystemManager(managerId).removeSystem(systemId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Systems/WaveSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class Wave {
private:

std::vector<std::size_t> _wavesId;
std::size_t _msBeforeNextWave;
int _waveIndex;
std::size_t _msBeforeNextWave;
bool _isGameEnded;
bool _isTimeBetweenWaves;
static std::mutex _mutex;
Expand Down

0 comments on commit 4fae04c

Please sign in to comment.