Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Client/SceneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Scene {
static void initSystemManagers()
{
auto &director = Systems::SystemManagersDirector::getInstance();
std::lock_guard<std::mutex> lock(director.mutex);

for (auto systems : Systems::getSystemsGroups()) {
director.addSystemManager(systems);
Expand All @@ -60,18 +61,25 @@ namespace Scene {
Raylib::closeWindow();
}

int SceneManager::run()
static void updateSystemManagers(std::vector<SystemManagers> &scene)
{
auto &director = Systems::SystemManagersDirector::getInstance();

for (auto &systemManager : scene) {
std::unique_lock<std::mutex> lock(director.mutex);
director.getSystemManager(static_cast<std::size_t>(systemManager)).updateSystems();
lock.unlock();
}
}

int SceneManager::run()
{
try {
while (!_stop && !Raylib::windowShouldClose()) {
Raylib::beginDrawing();
Raylib::clearBackground(Raylib::DarkGray);
auto scene = _scenes.at(static_cast<std::size_t>(_currentScene));
for (auto &systemManager : scene) {
director.getSystemManager(static_cast<std::size_t>(systemManager)).updateSystems();
}
updateSystemManagers(scene);
Raylib::endDrawing();
}
destroyRaylib();
Expand Down
3 changes: 3 additions & 0 deletions src/Client/Systems/Events/EventsSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Systems {

void EventsSystems::playerMovement(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry &registry = Registry::getInstance();
Registry::components<Types::Position> arrPos = registry.getComponents<Types::Position>();
Registry::components<struct health_s> arrHealth = registry.getComponents<struct health_s>();
Expand Down Expand Up @@ -66,6 +67,7 @@ namespace Systems {

void playerShootBullet(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
static const std::size_t waitTimeBullet = 500;
static const std::string soundPathShoot = "assets/Audio/Sounds/laser.ogg";
Registry &registry = Registry::getInstance();
Expand Down Expand Up @@ -107,6 +109,7 @@ namespace Systems {

void EventsSystems::changeScene(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_J)) {
auto &sceneManager = Scene::SceneManager::getInstance();
if (sceneManager.getCurrentScene() == Scene::Scene::MAIN_GAME) {
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Graphic/AudioSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace Systems {

void GraphicSystems::initAudio(std::size_t managerId, std::size_t systemId)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
constexpr float musicVolume = 0.60F;
constexpr float soundVolume = 0.63F;

Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Graphic/DeathSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace Systems {

void DeathSystems::setEntityDeathFunction(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry::components<Types::Dead> arrDead = Registry::getInstance().getComponents<Types::Dead>();

std::vector<std::size_t> ids = arrDead.getExistingsId();
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Graphic/ParallaxSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace Systems::ParallaxSystems {

void initParalax(std::size_t managerId, std::size_t systemId)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
std::vector<nlohmann::basic_json<>> parallaxData =
Json::getInstance().getDataByJsonType("parallax", JsonType::DEFAULT_PARALLAX);

Expand Down
4 changes: 4 additions & 0 deletions src/Client/Systems/Graphic/SpriteSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ namespace Systems {

void GraphicSystems::rectIncrementation(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry &registry = Registry::getInstance();
Registry::components<Types::AnimRect> arrAnimRect = registry.getComponents<Types::AnimRect>();
Registry::components<Types::Rect> arrRect = registry.getComponents<Types::Rect>();
Expand All @@ -96,6 +97,7 @@ namespace Systems {

void GraphicSystems::rectRenderer(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry &registry = Registry::getInstance();
Registry::components<Types::Position> arrPosition = registry.getComponents<Types::Position>();
Registry::components<Types::RectangleShape> arrRect =
Expand Down Expand Up @@ -193,6 +195,7 @@ namespace Systems {

void GraphicSystems::spriteRenderer(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry &registry = Registry::getInstance();
std::vector<std::vector<std::size_t>> backLayers = registry.getBackLayers();
std::vector<std::size_t> defaultLayer = registry.getDefaultLayer();
Expand All @@ -209,6 +212,7 @@ namespace Systems {

void GraphicSystems::createSprite(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
auto &arrSpriteDatas = Registry::getInstance().getComponents<Types::SpriteDatas>();
auto &arrSprite = Registry::getInstance().getComponents<Raylib::Sprite>();

Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Graphic/TextSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace Systems {

void GraphicSystems::textRenderer(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry::components<Raylib::Text> arrText = Registry::getInstance().getComponents<Raylib::Text>();

std::vector<std::size_t> ids = arrText.getExistingsId();
Expand Down
16 changes: 8 additions & 8 deletions src/Client/Systems/Network/ClientNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ namespace Systems {

void handleStartWave(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
auto &director = SystemManagersDirector::getInstance();
std::lock_guard<std::mutex> lock(director.mutex);
const auto wave = std::any_cast<struct msgStartWave_s>(any);
Types::Enemy::setEnemyNb(wave.enemyNb);
SystemManagersDirector::getInstance()
director
.getSystemManager(static_cast<std::size_t>(Scene::SystemManagers::GAME))
.addSystem(initWave);
Logger::info("Wave started");
Expand All @@ -60,21 +61,20 @@ namespace Systems {
const auto playerInit = std::any_cast<struct msgPlayerInit_s>(any);

Logger::info("Your player id is: " + std::to_string(playerInit.playerId));
initPlayer(JsonType::DEFAULT_PLAYER, playerInit.playerId);
initPlayer(playerInit.playerId);
}

void receiveNewEnemy(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto newEnemy = std::any_cast<struct msgNewEnemy_s>(any);

Logger::debug("ROLLBACK RECREATE ENEMY !!!!!");
initEnemy(newEnemy.enemyInfos.type, {0.0, 0.0}, true, newEnemy.enemyInfos.id);
Types::Position pos = {
static_cast<float>(newEnemy.enemyInfos.pos.x),
static_cast<float>(newEnemy.enemyInfos.pos.y)};

Logger::debug("ROLLBACK RECREATE ENEMY !!!!!");
initEnemy(newEnemy.enemyInfos.type, pos, true, newEnemy.enemyInfos.id);
struct health_s hp = newEnemy.enemyInfos.life;
Registry::getInstance().getComponents<Types::Position>().insertBack(pos);
Registry::getInstance().getComponents<struct health_s>().insertBack(hp);
}

Expand All @@ -84,7 +84,7 @@ namespace Systems {
const auto newAllie = std::any_cast<struct msgNewAllie_s>(any);

Logger::info("New Ally created with id: " + std::to_string(newAllie.playerId));
initPlayer(JsonType::DEFAULT_PLAYER, newAllie.playerId, true);
initPlayer(newAllie.playerId, true);
}

void receiveRelativePosition(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
Expand Down
26 changes: 26 additions & 0 deletions src/ECS/Json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ class Json {
return jsonData[index].get<T>();
}

template <typename T>
T getDataByJsonType(const std::string &index, JsonType dataType)
{
return getDataFromJson<T>(_jsonDatas[dataType], index);
}

template <typename T>
T getDataByVector(const std::vector<std::string> &indexes, JsonType dataType)
{
auto datas = getDataByJsonType(dataType);
auto begin = indexes.begin();

if (indexes.empty()) {
Logger::fatal(std::string("(getDataByVector<T>): empty list"));
throw std::runtime_error("Json error");
}
for (; begin + 1 != indexes.end(); begin++) {
if (datas[*begin] == nullptr) {
Logger::fatal(std::string("(getDataByVector<T>) Key : " + *begin + " is not valid"));
throw std::runtime_error("Json error");
}
datas = datas[*begin];
}
return getDataFromJson<T>(datas, *(indexes.end() - 1));
}

private:
Json() = default;
~Json() = default;
Expand Down
8 changes: 0 additions & 8 deletions src/ECS/Systems/Managers/SystemManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ namespace Systems {

void SystemManager::addSystem(std::function<void(std::size_t, std::size_t)> sys)
{
std::lock_guard<std::mutex> lock(SystemManagersDirector::getInstance().mutex);

if (!_modified) {
_modified = true;
}
Expand All @@ -60,8 +58,6 @@ namespace Systems {

void SystemManager::removeSystem(std::size_t id)
{
std::lock_guard<std::mutex> lock(SystemManagersDirector::getInstance().mutex);

if (!_modified) {
_modified = true;
}
Expand All @@ -70,16 +66,12 @@ namespace Systems {

void SystemManager::resetChanges()
{
std::lock_guard<std::mutex> lock(SystemManagersDirector::getInstance().mutex);

_modified = false;
_modifiedSystems = _originalSystems;
}

std::vector<std::function<void(std::size_t, std::size_t)>> &SystemManager::getSystems()
{
std::lock_guard<std::mutex> lock(SystemManagersDirector::getInstance().mutex);

if (_modified) {
return _modifiedSystems;
}
Expand Down
53 changes: 28 additions & 25 deletions src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ extern "C"
namespace Systems {
constexpr float maxPercent = 100.0F;

void windowCollision(std::size_t /*unused*/, std::size_t /*unused*/)
static void checkOutsideWindow(std::vector<std::size_t> ids)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry &registry = Registry::getInstance();
Registry &registry = Registry::getInstance();
Registry::components<Types::Position> arrPosition = registry.getComponents<Types::Position>();
Registry::components<Types::CollisionRect> arrCollisionRect =
registry.getComponents<Types::CollisionRect>();
std::vector<std::size_t> ids = registry.getEntitiesByComponents(
{typeid(Types::Player), typeid(Types::Position), typeid(Types::CollisionRect)});
registry.getComponents<Types::CollisionRect>();

for (std::size_t id : ids) {
if (arrPosition[id].x < 0) {
Expand All @@ -55,6 +52,19 @@ namespace Systems {
}
}

void windowCollision(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry &registry = Registry::getInstance();
std::vector<std::size_t> ids = registry.getEntitiesByComponents(
{typeid(Types::Player), typeid(Types::Position), typeid(Types::CollisionRect)});
std::vector<std::size_t> idsOtherPlayer = registry.getEntitiesByComponents(
{typeid(Types::OtherPlayer), typeid(Types::Position), typeid(Types::CollisionRect)});

checkOutsideWindow(ids);
checkOutsideWindow(idsOtherPlayer);
}

static bool checkAllies(std::size_t fstId, std::size_t scdId)
{
Registry &registry = Registry::getInstance();
Expand Down Expand Up @@ -189,22 +199,17 @@ namespace Systems {
Json::getInstance().getDataByJsonType("enemy", jsonType);

for (auto &elem : enemyData) {
#ifdef CLIENT
std::size_t id = Registry::getInstance().addEntity();
#else
Registry::getInstance().addEntity();
#endif

#ifdef CLIENT

Types::SpriteDatas enemy = {
Json::getInstance().getDataFromJson<std::string>(elem, "spritePath"),
Json::getInstance().getDataFromJson<float>(elem, "width"),
Json::getInstance().getDataFromJson<float>(elem, "height"),
LayerType::DEFAULTLAYER,
0};

Types::Rect rect = Json::getInstance().getDataFromJson<Types::Rect>(elem, "rect");
auto rect = Json::getInstance().getDataFromJson<Types::Rect>(elem, "rect");

nlohmann::basic_json<> animRectData =
Json::getInstance().getDataFromJson<nlohmann::basic_json<>>(elem, "animRect");
Expand Down Expand Up @@ -249,6 +254,7 @@ namespace Systems {

void manageBoss(std::size_t managerId, std::size_t systemId)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const float posToGo = 65.0;
const float bossSpeed = 0.2F;
Registry::components<Types::Position> &arrPosition =
Expand All @@ -266,19 +272,20 @@ namespace Systems {
for (auto &id : ids) {
if (arrPosition[id].x <= posToGo && arrVelocity[id].speedY == 0) {
arrVelocity[id].speedX = 0;
arrVelocity[id].speedY = 0.2;
arrVelocity[id].speedY = static_cast<float>(0.2);
}
if (arrPosition[id].y < 0) {
arrVelocity[id].speedY = bossSpeed;
}
if (arrPosition[id].y + arrCollisonRect[id].height > maxPercent) {
arrVelocity[id].speedY = -bossSpeed;
arrVelocity[id].speedY -= bossSpeed;
}
}
}

void initWave(std::size_t managerId, std::size_t systemId)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
static std::size_t enemyNumber =
Json::getInstance().getDataByVector({"wave", "nbrEnemy"}, JsonType::WAVE);
const std::size_t spawnDelay = 2;
Expand Down Expand Up @@ -426,17 +433,17 @@ namespace Systems {
}
}

void initPlayer(JsonType playerType, unsigned int constId, bool otherPlayer)
void initPlayer(unsigned int constId, bool otherPlayer)
{
#ifdef CLIENT
std::size_t id = Registry::getInstance().addEntity();
#else
JsonType playerType = JsonType::DEFAULT_PLAYER;

Registry::getInstance().addEntity();
#endif

Types::Dead deadComp = {Json::getInstance().getDataByVector({"player", "deadTime"}, playerType)};
Logger::info("player avant template");
Types::Dead deadComp = {Json::getInstance().getDataByVector<std::size_t>({"player", "deadTime"}, playerType)};
struct health_s healthComp = {
Json::getInstance().getDataByVector({"player", "health"}, playerType)};
Json::getInstance().getDataByVector<int>({"player", "health"}, playerType)};
Logger::info("player after template");
Types::Damage damageComp = {Json::getInstance().getDataByVector({"player", "damage"}, playerType)};
#ifdef CLIENT
Types::SpriteDatas playerDatas(
Expand Down Expand Up @@ -488,11 +495,7 @@ namespace Systems {

void createMissile(Types::Position &pos, Types::Missiles &typeOfMissile)
{
#ifdef CLIENT
std::size_t entityId = Registry::getInstance().addEntity();
#else
Registry::getInstance().addEntity();
#endif

constexpr float bulletWidth = 5.0F;
constexpr float bulletHeight = 5.0F;
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Systems/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ namespace Systems {
void deathChecker(std::size_t, std::size_t);
void initWave(std::size_t managerId, std::size_t systemId);
void createMissile(Types::Position &pos, Types::Missiles &typeOfMissile);
void initPlayer(JsonType playerType, unsigned int constId, bool otherPlayer = false);
void initPlayer(unsigned int constId, bool otherPlayer = false);
std::vector<std::function<void(std::size_t, std::size_t)>> getECSSystems();
} // namespace Systems
Loading