diff --git a/src/Client/Systems/Graphic/AudioSystems.cpp b/src/Client/Systems/Graphic/AudioSystems.cpp index aa8330e4..2c4b0cec 100644 --- a/src/Client/Systems/Graphic/AudioSystems.cpp +++ b/src/Client/Systems/Graphic/AudioSystems.cpp @@ -42,12 +42,8 @@ namespace Systems { } } - const std::string musicPath = "assets/Audio/Musics/Title.mp3"; - const std::string soundPath = "assets/Audio/Sounds/fire.ogg"; - const std::string playerPath = "assets/R-TypeSheet/r-typesheet14.gif"; - const Types::Rect spriteRect = {2, 2, 48, 48}; - const Types::CollisionRect collisionRect = {46, 46}; - const Raylib::Vector2 textPos = {20, 50}; + const std::string musicPath = "assets/Audio/Musics/Title.mp3"; + const std::string soundPath = "assets/Audio/Sounds/fire.ogg"; void GraphicSystems::playSoundWithKey( std::size_t /*unused*/, diff --git a/src/Client/Systems/Graphic/CMakeLists.txt b/src/Client/Systems/Graphic/CMakeLists.txt index dc89eecf..190f84c8 100644 --- a/src/Client/Systems/Graphic/CMakeLists.txt +++ b/src/Client/Systems/Graphic/CMakeLists.txt @@ -13,4 +13,5 @@ target_sources( ${CMAKE_CURRENT_SOURCE_DIR}/AudioSystems.cpp ${CMAKE_CURRENT_SOURCE_DIR}/SpriteSystems.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TextSystems.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/DeathSystems.cpp ) diff --git a/src/Client/Systems/Graphic/DeathSystems.cpp b/src/Client/Systems/Graphic/DeathSystems.cpp new file mode 100644 index 00000000..76fd212a --- /dev/null +++ b/src/Client/Systems/Graphic/DeathSystems.cpp @@ -0,0 +1,67 @@ +/* +** EPITECH PROJECT, 2023 +** R-Bus +** File description: +** Death systems implementation +*/ + +#include "DeathSystems.hpp" +#include +#include +#include "CustomTypes.hpp" +#include "Registry.hpp" + +namespace Systems { + + const std::function setPlayerAnimRectDeath = + [](std::size_t id) { + Registry::components arrAnimRect = + Registry::getInstance().getComponents(); + + if (arrAnimRect.exist(id)) { + Types::AnimRect& anim = arrAnimRect[id]; + if (anim.currentRectList != Types::RectListType::DEAD) { + anim.changeRectList(Types::RectListType::DEAD); + } + } + }; + + const std::function setEnemyDeathFunc = + [](std::size_t id) { + Registry::getInstance().removeEntity(id); + }; + + // MAP FOR DEATH FUNCTIONS FOR EACH ENTITY + const std::unordered_map> + deathFunctions = { + {std::type_index(typeid(Types::Player)), setPlayerAnimRectDeath}, + {std::type_index(typeid(Types::Enemy)), setEnemyDeathFunc }, + }; + + void DeathSystems::setEntityDeathFunction( + std::size_t /*unused*/, + std::size_t /*unused*/) + { + Registry::components arrDead = + Registry::getInstance().getComponents(); + + std::vector ids = arrDead.getExistingsId(); + + 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; + } + } + } + } + + std::vector> + DeathSystems::getDeathSystems() + { + return {setEntityDeathFunction}; + } +} // namespace Systems diff --git a/src/Client/Systems/Graphic/DeathSystems.hpp b/src/Client/Systems/Graphic/DeathSystems.hpp new file mode 100644 index 00000000..3d5b1f24 --- /dev/null +++ b/src/Client/Systems/Graphic/DeathSystems.hpp @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2023 +** R-Bus +** File description: +** DeathSystems +*/ + +#pragma once + +#include +#include + +namespace Systems { + namespace DeathSystems { + std::vector> + getDeathSystems(); + void setEntityDeathFunction( + std::size_t /*unused*/, + std::size_t /*unused*/); + } // namespace DeathSystems +} // namespace Systems diff --git a/src/Client/Systems/Graphic/GraphicSystems.cpp b/src/Client/Systems/Graphic/GraphicSystems.cpp index fb3222f3..b0f22b61 100644 --- a/src/Client/Systems/Graphic/GraphicSystems.cpp +++ b/src/Client/Systems/Graphic/GraphicSystems.cpp @@ -7,6 +7,7 @@ #include "GraphicSystems.hpp" #include "AudioSystems.hpp" +#include "DeathSystems.hpp" #include "SpriteSystems.hpp" #include "TextSystems.hpp" @@ -20,6 +21,8 @@ namespace Systems { spriteSystems = getSpriteSystems(); std::vector> textSystems = getTextSystems(); + std::vector> + deathSystems = DeathSystems::getDeathSystems(); audioSystems.insert( audioSystems.end(), @@ -29,6 +32,10 @@ namespace Systems { audioSystems.end(), textSystems.begin(), textSystems.end()); + audioSystems.insert( + audioSystems.end(), + deathSystems.begin(), + deathSystems.end()); return audioSystems; } } // namespace Systems diff --git a/src/ECS/ECSCustomTypes.hpp b/src/ECS/ECSCustomTypes.hpp index 36383426..1f118b70 100644 --- a/src/ECS/ECSCustomTypes.hpp +++ b/src/ECS/ECSCustomTypes.hpp @@ -7,7 +7,9 @@ #pragma once -// all values are in percentage of the screen +#include +#include +#include namespace Types { @@ -30,10 +32,16 @@ namespace Types { int hp; }; - struct Dammage { - int dammage; + struct Damage { + int damage; }; struct Player { }; + struct Enemy { }; + + struct Dead { + std::optional> deathFunction; + }; + } // namespace Types diff --git a/src/ECS/SparseArray.hpp b/src/ECS/SparseArray.hpp index 1a2fb90d..17baa3a2 100644 --- a/src/ECS/SparseArray.hpp +++ b/src/ECS/SparseArray.hpp @@ -112,7 +112,7 @@ class SparseArray { } } for (auto it2 = _sparse.begin(); it2 != _sparse.end(); it2++) { - if (*it2 > sparseValue) { + if (static_cast(*it2) > static_cast(sparseValue)) { (*it2)--; } } diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index 384e5697..880f93eb 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -44,15 +44,14 @@ namespace Systems { static void giveDamages(std::size_t firstEntity, std::size_t secondEntity) { - Registry::components arrDammage = - Registry::getInstance().getComponents(); + Registry::components arrDamage = + Registry::getInstance().getComponents(); Registry::components arrHealth = Registry::getInstance().getComponents(); - if (arrDammage.exist(firstEntity) - && arrDammage[firstEntity].dammage > 0) { + if (arrDamage.exist(firstEntity) && arrDamage[firstEntity].damage > 0) { if (arrHealth.exist(secondEntity)) { - arrHealth[secondEntity].hp -= arrDammage[firstEntity].dammage; + arrHealth[secondEntity].hp -= arrDamage[firstEntity].damage; } } } @@ -99,26 +98,56 @@ namespace Systems { } } + static void executeDeathFunction( + std::size_t id, + Registry::components arrDead) + { + if (arrDead[id].deathFunction != std::nullopt) { + arrDead[id].deathFunction.value()(id); + } else { + Registry::getInstance().removeEntity(id); + } + } + + void deathChecker(std::size_t /*unused*/, std::size_t /*unused*/) + { + Registry::components arrHealth = + Registry::getInstance().getComponents(); + Registry::components arrDead = + Registry::getInstance().getComponents(); + + std::vector ids = arrHealth.getExistingsId(); + for (auto itIds = ids.begin(); itIds != ids.end(); itIds++) { + if (arrHealth.exist(*itIds) && arrHealth[*itIds].hp <= 0 + && arrDead.exist(*itIds)) { + executeDeathFunction(*itIds, arrDead); + } + } + } + const std::string musicPath = "assets/Audio/Musics/Title.mp3"; const std::string soundPath = "assets/Audio/Sounds/fire.ogg"; const std::string playerPath = "assets/R-TypeSheet/r-typesheet14.gif"; const Types::Rect spriteRect = {2, 2, 48, 48}; - const Types::CollisionRect collisionRect = {6, 6}; + const Types::CollisionRect collisionRect = {25, 25}; const Raylib::Vector2 textPos = {20, 50}; constexpr int playerData = 10; - constexpr int playerDammage = 10; - constexpr int playerHealth = 1; + const Types::Position playerPos = {50, 50}; + constexpr int playerDamage = 1; + constexpr int enemyDamage = 1; + constexpr int playerHealth = 5; + constexpr int playerHealth2 = 5; constexpr float musicVolume = 0.02F; constexpr float soundVolume = 0.1F; constexpr float fontScale = 2.0F; - const float playerWidth = 10.0F; - const float playerHeight = 10.0F; + const float playerWidth = 25.0F; + const float playerHeight = 25.0F; void init(std::size_t managerId, std::size_t systemId) { std::size_t id = Registry::getInstance().addEntity(); Registry::getInstance().getComponents().insertBack( - {playerData, playerData}); + playerPos); Registry::getInstance().getComponents().insertBack( {playerPath, playerWidth, playerHeight, id}); Registry::getInstance().getComponents().insertBack( @@ -129,29 +158,31 @@ namespace Systems { // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) Registry::getInstance().getComponents().insertBack({ spriteRect, - {spriteRect, - {2, 51, 46, 47}, + {{2, 51, 46, 47}, {101, 2, 48, 47}, {152, 2, 46, 47}, - {201, 2, 46, 47}}, + {201, 2, 46, 47} }, + {{2, 51, 46, 47}, + {101, 2, 48, 47}, + {152, 2, 46, 47}, + {201, 2, 46, 47} }, + {{180, 140, 18, 12}, + {211, 140, 18, 12}, + {230, 140, 18, 12}, + {250, 140, 18, 12}} }); // NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) Registry::getInstance().setToBackLayers(id); Registry::getInstance().getComponents().insertBack({}); - - id = Registry::getInstance().addEntity(); - Registry::getInstance().getComponents().insertBack( - {playerPath, playerWidth, playerHeight, id}); - Registry::getInstance().getComponents().insertBack( - spriteRect); - Registry::getInstance() - .getComponents() - .insertBack(collisionRect); + Registry::getInstance().getComponents().insertBack( + {playerDamage}); Registry::getInstance().getComponents().insertBack( {playerHealth}); - Registry::getInstance().getComponents().insertBack({}); + Registry::getInstance().getComponents().insertBack( + {std::nullopt}); id = Registry::getInstance().addEntity(); + Registry::getInstance().getComponents().insertBack({}); Registry::getInstance().getComponents().insertBack( {playerData, playerData + playerData + playerData}); Registry::getInstance().getComponents().insertBack( @@ -161,6 +192,8 @@ namespace Systems { Registry::getInstance() .getComponents() .insertBack(collisionRect); + Registry::getInstance().getComponents().insertBack( + {std::nullopt}); Registry::getInstance().setToFrontLayers(id); Registry::getInstance().getComponents().insertBack( @@ -173,10 +206,10 @@ namespace Systems { textPos, fontScale, Raylib::DarkBlue}); - Registry::getInstance().getComponents().insertBack( - {playerDammage}); + Registry::getInstance().getComponents().insertBack( + {enemyDamage}); Registry::getInstance().getComponents().insertBack( - {playerHealth}); + {playerHealth2}); SystemManagersDirector::getInstance() .getSystemManager(managerId) .removeSystem(systemId); @@ -184,6 +217,6 @@ namespace Systems { std::vector> getECSSystems() { - return {windowCollision, init, entitiesCollision}; + return {windowCollision, init, entitiesCollision, deathChecker}; } } // namespace Systems