From e80cd949d829f0bd2d79760343d38239366f15cb Mon Sep 17 00:00:00 2001 From: tenshi Date: Tue, 3 Oct 2023 15:29:34 +0200 Subject: [PATCH] GAME-LOGIC: Add funcitonnal EntityDeathFunction MINOR --- src/Client/Systems/Graphic/DeathSystems.cpp | 44 +++++++++------------ 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/src/Client/Systems/Graphic/DeathSystems.cpp b/src/Client/Systems/Graphic/DeathSystems.cpp index 36b898dc..eb5c0880 100644 --- a/src/Client/Systems/Graphic/DeathSystems.cpp +++ b/src/Client/Systems/Graphic/DeathSystems.cpp @@ -6,7 +6,6 @@ */ #include "DeathSystems.hpp" -#include #include #include #include "CustomTypes.hpp" @@ -14,47 +13,40 @@ namespace Systems { - // REPLACE BY YOUR DEATH FUNCTION + const std::function setPlayerAnimRectToDeath = [](std::size_t id) { + Registry::components arrAnimRect = + Registry::getInstance().getComponents(); - const std::function testFuncPlayer = [](std::size_t id) { - std::cout << "Player " << id << " is dead" << std::endl; + if (arrAnimRect.exist(id)) { + Types::AnimRect &anim = arrAnimRect[id]; + if (anim.currentRectList != Types::RectListType::DEAD) { + anim.changeRectList(Types::RectListType::DEAD); + } + } }; - // MAP FOR DEATH FUNCTIONS + // MAP FOR DEATH FUNCTIONS FOR EACH ENTITY const std::unordered_map> deathFunctions = { - {std::type_index(typeid(Types::Player)), testFuncPlayer}, + {std::type_index(typeid(Types::Player)), setPlayerAnimRectToDeath}, }; - static void addDeathFunction( - Registry::components &arrDead, - Registry::components &arrPlayer, - std::size_t id) - { - try { - if (arrPlayer.exist(id)) { - arrDead[id].deathFunction = - deathFunctions.at(std::type_index(typeid(Types::Player))); - } - } catch (std::out_of_range &) { - std::cerr << "No death function for this entity" << std::endl; - } - } - void DeathSystems::setEntityDeathFunction( std::size_t /*unused*/, std::size_t /*unused*/) { Registry::components arrDead = Registry::getInstance().getComponents(); - Registry::components arrPlayer = - Registry::getInstance().getComponents(); std::vector ids = arrDead.getExistingsId(); - for (auto itIds = ids.begin(); itIds != ids.end(); itIds++) { - if (arrDead[*itIds].deathFunction == std::nullopt) { - addDeathFunction(arrDead, arrPlayer, *itIds); + for (const auto& [typeIndex, function] : deathFunctions) { + std::vector entities = + Registry::getInstance().getEntitiesByComponents({typeIndex}); + for (std::size_t id : entities) { + if (arrDead.exist(id) && arrDead[id].deathFunction == std::nullopt) { + arrDead[id].deathFunction = function; + } } } }