Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rb 56 check deads #38

Merged
merged 30 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fb9a775
GAME-LOGIC: Modify screen size
TTENSHII Oct 1, 2023
0bd76aa
GAME-LOGIC: Add deathChecker func
TTENSHII Oct 1, 2023
6c4a7f0
GAME-LOGIC: Add begin of client side system
TTENSHII Oct 2, 2023
6f72625
GAME-LOGIC: Add v1 of setEntityDeathFunction
TTENSHII Oct 2, 2023
d8c3ffd
Merge branch 'dev' into feature/RB-56-checkDeads
TTENSHII Oct 2, 2023
e562010
FORMAT-AUTO: automatic format on pull request #38
github-actions[bot] Oct 2, 2023
1d96a5b
GAME-LOGIC: Add character run
TTENSHII Oct 2, 2023
884b448
Merge branch 'feature/RB-56-checkDeads' of github.com:X-R-G-B/R-Bus i…
TTENSHII Oct 2, 2023
eb80397
GAME-LOGIC: Fix deathSystems return value
TTENSHII Oct 2, 2023
3c2d55a
Merge branch 'dev' into feature/RB-56-checkDeads
TTENSHII Oct 3, 2023
30277f8
GAME-LOGIC: Fix ternary
TTENSHII Oct 3, 2023
e80cd94
GAME-LOGIC: Add funcitonnal EntityDeathFunction
TTENSHII Oct 3, 2023
d58aad4
FORMAT-AUTO: automatic format on pull request #38
github-actions[bot] Oct 3, 2023
cf12c46
GAME-LOGIC: Add test
TTENSHII Oct 3, 2023
c99020e
CLIENT-GAME: Fix test func
TTENSHII Oct 3, 2023
b10872d
FORMAT-AUTO: automatic format on pull request #38
github-actions[bot] Oct 3, 2023
10d5d6c
GAME-LOGIC: Add enemy test
TTENSHII Oct 3, 2023
9b7614b
Merge branch 'feature/RB-56-checkDeads' of github.com:X-R-G-B/R-Bus i…
TTENSHII Oct 3, 2023
67ad627
FORMAT-AUTO: automatic format on pull request #38
github-actions[bot] Oct 3, 2023
cf6bf99
GAME-LOGIC: Add debug
TTENSHII Oct 3, 2023
e883a93
FORMAT-AUTO: automatic format on pull request #38
github-actions[bot] Oct 3, 2023
e623b10
Merge branch 'dev' into feature/RB-56-checkDeads
guillaumeAbel Oct 3, 2023
1a778a6
Merge branch 'dev' into feature/RB-56-checkDeads
TTENSHII Oct 3, 2023
98a7736
Merge branch 'feature/RB-56-checkDeads' of github.com:X-R-G-B/R-Bus i…
TTENSHII Oct 3, 2023
d751c66
CLIENT-GAME: Add tests for debug
TTENSHII Oct 3, 2023
63fcc33
FORMAT-AUTO: automatic format on pull request #38
github-actions[bot] Oct 3, 2023
225cfcb
GAME-LOGIC: Fix sparse bug
TTENSHII Oct 3, 2023
67f2c43
GAME-LOGIC: Fix merge requests
TTENSHII Oct 3, 2023
71c9419
GAME-LOGIC: Fix animation rect
TTENSHII Oct 3, 2023
eb01dd8
FORMAT-AUTO: automatic format on pull request #38
github-actions[bot] Oct 3, 2023
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
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi supprimer ?

#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