Skip to content

Commit

Permalink
Merge pull request #38 from X-R-G-B/feature/RB-56-checkDeads
Browse files Browse the repository at this point in the history
Feature/rb 56 check deads
  • Loading branch information
Saverio976 authored Oct 3, 2023
2 parents 86cd4a9 + eb01dd8 commit 6c8c2e2
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 38 deletions.
8 changes: 2 additions & 6 deletions src/Client/Systems/Graphic/AudioSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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*/,
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Graphic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
67 changes: 67 additions & 0 deletions src/Client/Systems/Graphic/DeathSystems.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** Death systems implementation
*/

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

namespace Systems {

const std::function<void(std::size_t)> setPlayerAnimRectDeath =
[](std::size_t id) {
Registry::components<Types::AnimRect> arrAnimRect =
Registry::getInstance().getComponents<Types::AnimRect>();

if (arrAnimRect.exist(id)) {
Types::AnimRect& anim = arrAnimRect[id];
if (anim.currentRectList != Types::RectListType::DEAD) {
anim.changeRectList(Types::RectListType::DEAD);
}
}
};

const std::function<void(std::size_t)> setEnemyDeathFunc =
[](std::size_t id) {
Registry::getInstance().removeEntity(id);
};

// 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)), setPlayerAnimRectDeath},
{std::type_index(typeid(Types::Enemy)), setEnemyDeathFunc },
};

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

std::vector<std::size_t> ids = arrDead.getExistingsId();

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;
}
}
}
}

std::vector<std::function<void(std::size_t, std::size_t)>>
DeathSystems::getDeathSystems()
{
return {setEntityDeathFunction};
}
} // namespace Systems
21 changes: 21 additions & 0 deletions src/Client/Systems/Graphic/DeathSystems.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** DeathSystems
*/

#pragma once

#include <functional>
#include <vector>

namespace Systems {
namespace DeathSystems {
std::vector<std::function<void(std::size_t, std::size_t)>>
getDeathSystems();
void setEntityDeathFunction(
std::size_t /*unused*/,
std::size_t /*unused*/);
} // namespace DeathSystems
} // namespace Systems
7 changes: 7 additions & 0 deletions src/Client/Systems/Graphic/GraphicSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "GraphicSystems.hpp"
#include "AudioSystems.hpp"
#include "DeathSystems.hpp"
#include "SpriteSystems.hpp"
#include "TextSystems.hpp"

Expand All @@ -20,6 +21,8 @@ namespace Systems {
spriteSystems = getSpriteSystems();
std::vector<std::function<void(std::size_t, std::size_t)>> textSystems =
getTextSystems();
std::vector<std::function<void(std::size_t, std::size_t)>>
deathSystems = DeathSystems::getDeathSystems();

audioSystems.insert(
audioSystems.end(),
Expand All @@ -29,6 +32,10 @@ namespace Systems {
audioSystems.end(),
textSystems.begin(),
textSystems.end());
audioSystems.insert(
audioSystems.end(),
deathSystems.begin(),
deathSystems.end());
return audioSystems;
}
} // namespace Systems
14 changes: 11 additions & 3 deletions src/ECS/ECSCustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#pragma once

// all values are in percentage of the screen
#include <cstddef>
#include <functional>
#include <optional>

namespace Types {

Expand All @@ -30,10 +32,16 @@ namespace Types {
int hp;
};

struct Dammage {
int dammage;
struct Damage {
int damage;
};

struct Player { };

struct Enemy { };

struct Dead {
std::optional<std::function<void(std::size_t id)>> deathFunction;
};

} // namespace Types
2 changes: 1 addition & 1 deletion src/ECS/SparseArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class SparseArray {
}
}
for (auto it2 = _sparse.begin(); it2 != _sparse.end(); it2++) {
if (*it2 > sparseValue) {
if (static_cast<int>(*it2) > static_cast<int>(sparseValue)) {
(*it2)--;
}
}
Expand Down
89 changes: 61 additions & 28 deletions src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ namespace Systems {

static void giveDamages(std::size_t firstEntity, std::size_t secondEntity)
{
Registry::components<Types::Dammage> arrDammage =
Registry::getInstance().getComponents<Types::Dammage>();
Registry::components<Types::Damage> arrDamage =
Registry::getInstance().getComponents<Types::Damage>();
Registry::components<Types::Health> arrHealth =
Registry::getInstance().getComponents<Types::Health>();

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;
}
}
}
Expand Down Expand Up @@ -99,26 +98,56 @@ namespace Systems {
}
}

static void executeDeathFunction(
std::size_t id,
Registry::components<Types::Dead> 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<Types::Health> arrHealth =
Registry::getInstance().getComponents<Types::Health>();
Registry::components<Types::Dead> arrDead =
Registry::getInstance().getComponents<Types::Dead>();

std::vector<std::size_t> 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<Types::Position>().insertBack(
{playerData, playerData});
playerPos);
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
{playerPath, playerWidth, playerHeight, id});
Registry::getInstance().getComponents<Types::Rect>().insertBack(
Expand All @@ -129,29 +158,31 @@ namespace Systems {
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
Registry::getInstance().getComponents<Types::AnimRect>().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<Types::Player>().insertBack({});

id = Registry::getInstance().addEntity();
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
{playerPath, playerWidth, playerHeight, id});
Registry::getInstance().getComponents<Types::Rect>().insertBack(
spriteRect);
Registry::getInstance()
.getComponents<Types::CollisionRect>()
.insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Damage>().insertBack(
{playerDamage});
Registry::getInstance().getComponents<Types::Health>().insertBack(
{playerHealth});
Registry::getInstance().getComponents<Types::Player>().insertBack({});
Registry::getInstance().getComponents<Types::Dead>().insertBack(
{std::nullopt});

id = Registry::getInstance().addEntity();
Registry::getInstance().getComponents<Types::Enemy>().insertBack({});
Registry::getInstance().getComponents<Types::Position>().insertBack(
{playerData, playerData + playerData + playerData});
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
Expand All @@ -161,6 +192,8 @@ namespace Systems {
Registry::getInstance()
.getComponents<Types::CollisionRect>()
.insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Dead>().insertBack(
{std::nullopt});
Registry::getInstance().setToFrontLayers(id);

Registry::getInstance().getComponents<Raylib::Music>().insertBack(
Expand All @@ -173,17 +206,17 @@ namespace Systems {
textPos,
fontScale,
Raylib::DarkBlue});
Registry::getInstance().getComponents<Types::Dammage>().insertBack(
{playerDammage});
Registry::getInstance().getComponents<Types::Damage>().insertBack(
{enemyDamage});
Registry::getInstance().getComponents<Types::Health>().insertBack(
{playerHealth});
{playerHealth2});
SystemManagersDirector::getInstance()
.getSystemManager(managerId)
.removeSystem(systemId);
}

std::vector<std::function<void(std::size_t, std::size_t)>> getECSSystems()
{
return {windowCollision, init, entitiesCollision};
return {windowCollision, init, entitiesCollision, deathChecker};
}
} // namespace Systems

0 comments on commit 6c8c2e2

Please sign in to comment.