Skip to content

Commit

Permalink
GAME-LOGIC: Add funcitonnal EntityDeathFunction
Browse files Browse the repository at this point in the history
MINOR
  • Loading branch information
TTENSHII committed Oct 3, 2023
1 parent 30277f8 commit e80cd94
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions src/Client/Systems/Graphic/DeathSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,47 @@
*/

#include "DeathSystems.hpp"
#include <iostream>
#include <optional>
#include <unordered_map>
#include "CustomTypes.hpp"
#include "Registry.hpp"

namespace Systems {

// REPLACE BY YOUR DEATH FUNCTION
const std::function<void(std::size_t)> setPlayerAnimRectToDeath = [](std::size_t id) {
Registry::components<Types::AnimRect> arrAnimRect =
Registry::getInstance().getComponents<Types::AnimRect>();

const std::function<void(std::size_t)> 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<std::type_index, std::function<void(std::size_t)>>
deathFunctions = {
{std::type_index(typeid(Types::Player)), testFuncPlayer},
{std::type_index(typeid(Types::Player)), setPlayerAnimRectToDeath},
};

static void addDeathFunction(
Registry::components<Types::Dead> &arrDead,
Registry::components<Types::Player> &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<Types::Dead> arrDead =
Registry::getInstance().getComponents<Types::Dead>();
Registry::components<Types::Player> arrPlayer =
Registry::getInstance().getComponents<Types::Player>();

std::vector<std::size_t> 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<std::size_t> entities =
Registry::getInstance().getEntitiesByComponents({typeIndex});
for (std::size_t id : entities) {
if (arrDead.exist(id) && arrDead[id].deathFunction == std::nullopt) {
arrDead[id].deathFunction = function;
}
}
}
}
Expand Down

0 comments on commit e80cd94

Please sign in to comment.