diff --git a/assets/Json/ennemyData.json b/assets/Json/ennemyData.json new file mode 100644 index 00000000..793f5b93 --- /dev/null +++ b/assets/Json/ennemyData.json @@ -0,0 +1,53 @@ +{ + "ennemy" : [ + { + "spritePath": "assets/R-TypeSheet/r-typesheet20.gif", + "damage": 10.0, + "health": 1.0, + "width": 10.0, + "height": 10.0, + "position" : { + "x": 70.0, + "y": 50.0 + }, + "rect": { + "x": 18.0, + "y": 67.0, + "width": 31.0, + "height": 30 + }, + "collisionRect": { + "width": 10.0, + "height":10.0 + }, + "velocity": { + "speedX": -0.1, + "speedY": 0.0 + }, + "animRect": { + "move": [ + { + "x": 18.0, + "y": 67.0, + "width": 31.0, + "height": 30.0 + }, + { + "x": 51.0, + "y": 67.0, + "width": 31.0, + "height": 30.0 + }, + { + "x": 85.0, + "y": 69.0, + "width": 30.0, + "height": 29.0 + } + ], + "attack": [], + "dead": [] + } + } + ] +} diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index 01edd0c8..f6807abb 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -69,7 +69,7 @@ namespace Systems { const Types::CollisionRect collisionRect = {1, 1}; const Types::Velocity velocity = {0.7F, 0.0F}; const Types::Missiles missileType = {Types::MissileTypes::CLASSIC}; - const health_s health = {1}; + const struct health_s health = {1}; const Types::Damage damage = {10}; static void createMissile(std::size_t id, Registry::components &arrPosition) @@ -88,14 +88,14 @@ namespace Systems { Registry::getInstance().getComponents().insertBack(missileType); Registry::getInstance().getComponents().insertBack({}); Registry::getInstance().getComponents().insertBack(velocity); - Registry::getInstance().getComponents().insertBack(health); + Registry::getInstance().getComponents().insertBack(health); Registry::getInstance().getComponents().insertBack(damage); Registry::getInstance().getComponents().insertBack({std::nullopt}); Registry::getInstance().setToFrontLayers(entityId); } } - const std::size_t waitTimeBullet = 1; + const std::size_t waitTimeBullet = 500; void playerShootBullet(std::size_t /*unused*/, std::size_t /*unused*/) { @@ -108,7 +108,7 @@ namespace Systems { std::vector ids = arrPlayer.getExistingsId(); if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_SPACE) - && clock_.elapsedSecondsSince(clockId) > waitTimeBullet) { + && clock_.elapsedMillisecondsSince(clockId) > waitTimeBullet) { clock_.restart(clockId); for (auto &id : ids) { createMissile(id, arrPosition); diff --git a/src/Client/Systems/Graphic/DeathSystems.cpp b/src/Client/Systems/Graphic/DeathSystems.cpp index cf0cf041..3ef68902 100644 --- a/src/Client/Systems/Graphic/DeathSystems.cpp +++ b/src/Client/Systems/Graphic/DeathSystems.cpp @@ -25,14 +25,9 @@ namespace Systems { } }; - 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*/) diff --git a/src/ECS/ECSCustomTypes.hpp b/src/ECS/ECSCustomTypes.hpp index f2905af9..06c84d69 100644 --- a/src/ECS/ECSCustomTypes.hpp +++ b/src/ECS/ECSCustomTypes.hpp @@ -39,11 +39,6 @@ namespace Types { NLOHMANN_DEFINE_TYPE_INTRUSIVE(Position, x, y); }; - struct InitialPosition { - float x; - float y; - }; - struct Damage { int damage; }; @@ -75,7 +70,10 @@ namespace Types { struct Enemy { }; - struct Parallax { }; + struct Parallax { + float x; + float y; + }; struct Dead { Dead(std::optional> func, std::size_t time = 0) diff --git a/src/ECS/Registry.cpp b/src/ECS/Registry.cpp index bb2656d8..fd6936c5 100644 --- a/src/ECS/Registry.cpp +++ b/src/ECS/Registry.cpp @@ -91,7 +91,8 @@ void Registry::setToDefaultLayer(std::size_t id) void Registry::setToFrontLayers(std::size_t id, FrontLayers layer) { - _backLayers[layer].push_back(id); + removeFromDefaultLayer(id); + _frontLayers[layer].push_back(id); } std::vector> Registry::getBackLayers() diff --git a/src/ECS/SparseArray.hpp b/src/ECS/SparseArray.hpp index 8461cffb..410b3538 100644 --- a/src/ECS/SparseArray.hpp +++ b/src/ECS/SparseArray.hpp @@ -47,7 +47,12 @@ class SparseArray { } std::size_t sparseValue = _sparse[id]; if (int(sparseValue) != -1) { - removeDenses(id, sparseValue); + removeDenses(sparseValue); + } + for (auto revIt2 = _revSparse.begin(); revIt2 != _revSparse.end(); revIt2++) { + if (*revIt2 > id) { + (*revIt2)--; + } } auto it = _sparse.begin(); std::advance(it, id); @@ -95,7 +100,7 @@ class SparseArray { } private: - void removeDenses(std::size_t id, std::size_t sparseValue) + void removeDenses(std::size_t sparseValue) { auto it = _dense.begin(); std::advance(it, sparseValue); @@ -103,11 +108,6 @@ class SparseArray { auto revIt = _revSparse.begin(); std::advance(revIt, sparseValue); _revSparse.erase(revIt); - for (auto revIt2 = _revSparse.begin(); revIt2 != _revSparse.end(); revIt2++) { - if (*revIt2 > id) { - (*revIt2)--; - } - } for (auto it2 = _sparse.begin(); it2 != _sparse.end(); it2++) { if (static_cast(*it2) > static_cast(sparseValue)) { (*it2)--; diff --git a/src/ECS/Systems/Managers/SystemManager.cpp b/src/ECS/Systems/Managers/SystemManager.cpp index 2da10c15..8a54d5cd 100644 --- a/src/ECS/Systems/Managers/SystemManager.cpp +++ b/src/ECS/Systems/Managers/SystemManager.cpp @@ -6,6 +6,7 @@ */ #include "SystemManager.hpp" +#include namespace Systems { @@ -29,32 +30,35 @@ namespace Systems { void SystemManager::updateSystems() { - std::size_t i = 0; + std::size_t i = 0; + std::size_t decrease = 0; + _toRemove.clear(); for (auto &system : getSystems()) { system(_id, i); i++; } - std::size_t decrease = 0; std::sort(_toRemove.begin(), _toRemove.end()); for (auto &id : _toRemove) { auto it = _modifiedSystems.begin(); std::advance(it, id - decrease); _modifiedSystems.erase(it); - decrease++; } - _toRemove.clear(); } void SystemManager::addSystem(std::function sys) { - _modified = true; + if (!_modified) { + _modified = true; + } _modifiedSystems.push_back(sys); } void SystemManager::removeSystem(std::size_t id) { - _modified = true; + if (!_modified) { + _modified = true; + } _toRemove.push_back(id); } diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index de8daa74..1b05e158 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -67,7 +67,8 @@ namespace Systems { { Registry::components arrDamage = Registry::getInstance().getComponents(); - Registry::components arrHealth = Registry::getInstance().getComponents(); + Registry::components arrHealth = + Registry::getInstance().getComponents(); if (checkAllies(firstEntity, secondEntity)) { return; @@ -129,8 +130,7 @@ namespace Systems { static void initParallaxEntity( nlohmann::json_abi_v3_11_2::basic_json<> ¶llaxData, - const float maxOffsideParallax = 0, - bool isCopy = false) + const float maxOffsideParallax = 0) { std::size_t id = Registry::getInstance().addEntity(); @@ -145,17 +145,13 @@ namespace Systems { {Types::Rect(parallaxData["rect"])}); } Registry::getInstance().setToBackLayers(id); - Registry::getInstance().getComponents().insertBack({}); Registry::components arrPosition = Registry::getInstance().getComponents(); - if (isCopy) { + if (maxOffsideParallax > 0) { arrPosition[id].x += maxOffsideParallax; } - Registry::getInstance().getComponents().insertBack( + Registry::getInstance().getComponents().insertBack( {arrPosition[id].x, arrPosition[id].y}); - if (parallaxData["copy"] != nullptr && parallaxData["copy"] == true && isCopy == false) { - initParallaxEntity(parallaxData, maxOutParallaxRight, true); - } } const std::string parallaxFile = "assets/Json/parallaxData.json"; @@ -166,8 +162,10 @@ namespace Systems { for (auto &e : jsonData["parallax"]) { initParallaxEntity(e); + if (e["copy"] != nullptr && e["copy"]) { + initParallaxEntity(e, maxOutParallaxRight); + } } - SystemManagersDirector::getInstance().getSystemManager(managerId).removeSystem(systemId); } @@ -224,27 +222,72 @@ namespace Systems { } } + static void initEnnemyEntity(nlohmann::json_abi_v3_11_2::basic_json<> &ennemyData) + { + std::size_t id = Registry::getInstance().addEntity(); + + Registry::getInstance().getComponents().insertBack( + Raylib::Sprite(ennemyData["spritePath"], ennemyData["width"], ennemyData["height"], id)); + Registry::getInstance().getComponents().insertBack( + {Types::Position(ennemyData["position"])}); + Registry::getInstance().getComponents().insertBack( + (Types::CollisionRect(ennemyData["collisionRect"]))); + Registry::getInstance().getComponents().insertBack((Types::Rect(ennemyData["rect"]))); + nlohmann::json animRectData = ennemyData["animRect"]; + nlohmann::json moveData = animRectData["move"]; + nlohmann::json attackData = animRectData["attack"]; + nlohmann::json deadData = animRectData["dead"]; + Registry::getInstance().getComponents().insertBack(Types::AnimRect( + Types::Rect(ennemyData["rect"]), + moveData.get>(), + attackData.get>(), + deadData.get>())); + Registry::getInstance().getComponents().insertBack( + {Types::Velocity(ennemyData["velocity"])}); + Registry::getInstance().getComponents().insertBack({ennemyData["health"]}); + Registry::getInstance().getComponents().insertBack({ennemyData["damage"]}); + } + + const std::string ennemyFile = "assets/Json/ennemyData.json"; + + void initEnnemy(std::size_t managerId, std::size_t systemId) + { + nlohmann::json jsonData = openJsonData(ennemyFile); + + if (jsonData["ennemy"] == nullptr) { + SystemManagersDirector::getInstance().getSystemManager(managerId).removeSystem(systemId); + return; + } + for (auto &ennemyData : jsonData["ennemy"]) { + initEnnemyEntity(ennemyData); + } + SystemManagersDirector::getInstance().getSystemManager(managerId).removeSystem(systemId); + } + void checkDestroyAfterDeathCallBack(std::size_t /*unused*/, std::size_t /*unused*/) { - Registry ®istry = Registry::getInstance(); - auto deadList = registry.getComponents(); - auto deadIdList = deadList.getExistingsId(); - Clock &clock = registry.getClock(); + Registry ®istry = Registry::getInstance(); + auto deadList = registry.getComponents(); + auto deadIdList = deadList.getExistingsId(); + Clock &clock = registry.getClock(); + std::size_t decrease = 0; for (auto id : deadIdList) { - Types::Dead &dead = deadList[id]; + auto tmpId = id - decrease; + Types::Dead &dead = deadList[tmpId]; if (static_cast(dead.clockId) > -1 && clock.elapsedMillisecondsSince(dead.clockId) > dead.timeToWait) { - clock.restart(dead.clockId); - registry.removeEntity(id); + registry.removeEntity(tmpId); + decrease++; } } } - static void executeDeathFunction(std::size_t id, Registry::components arrDead) + static void + executeDeathFunction(std::size_t id, Registry::components arrDead, std::size_t &decrease) { - Types::Dead &deadComp = arrDead[id]; - if (deadComp.deathFunction != std::nullopt) { + if (arrDead.exist(id) && arrDead[id].deathFunction != std::nullopt) { + Types::Dead &deadComp = arrDead[id]; if (!deadComp.launched) { deadComp.deathFunction.value()(id); deadComp.clockId = Registry::getInstance().getClock().create(); @@ -252,6 +295,23 @@ namespace Systems { } } else { Registry::getInstance().removeEntity(id); + decrease++; + } + } + + void deathChecker(std::size_t /*unused*/, std::size_t /*unused*/) + { + Registry::components arrHealth = + Registry::getInstance().getComponents(); + Registry::components arrDead = Registry::getInstance().getComponents(); + std::size_t decrease = 0; + + std::vector ids = arrHealth.getExistingsId(); + for (auto itIds = ids.begin(); itIds != ids.end(); itIds++) { + auto tmpId = (*itIds) - decrease; + if (arrHealth.exist(tmpId) && arrHealth[tmpId].hp <= 0) { + executeDeathFunction(tmpId, arrDead, decrease); + } } } @@ -267,36 +327,37 @@ namespace Systems { return (false); } - void manageOutsideWindowEntity(std::size_t /*unused*/, std::size_t /*unused*/) + void destroyOutsideWindow(std::size_t /*unused*/, std::size_t /*unused*/) { Registry::components arrPosition = Registry::getInstance().getComponents(); Registry::components arrParallax = Registry::getInstance().getComponents(); - - std::vector ids = arrPosition.getExistingsId(); + std::vector ids = + Registry::getInstance().getEntitiesByComponents({typeid(Types::Position)}); + std::size_t decrease = 0; for (auto &id : ids) { - if (!arrParallax.exist(id)) { - if (isOutsideWindow(arrPosition[id])) { - Registry::getInstance().removeEntity(id); - } + auto tmpId = id - decrease; + if (!arrParallax.exist(tmpId) && isOutsideWindow(arrPosition[tmpId])) { + Registry::getInstance().removeEntity(tmpId); + decrease++; } } } static void resetParallaxPosition( std::size_t id, - Registry::components &arrInitialPos, + Registry::components &arrParallax, Registry::components &arrPosition) { if (arrPosition[id].x <= maxOutParallaxLeft) { - if (arrInitialPos[id].x >= maxOutParallaxRight) { - arrPosition[id].x = arrInitialPos[id].x; + if (arrParallax[id].x >= maxOutParallaxRight) { + arrPosition[id].x = arrParallax[id].x; } else { - arrPosition[id].x = arrInitialPos[id].x + maxOutParallaxRight; + arrPosition[id].x = arrParallax[id].x + maxOutParallaxRight; } - arrPosition[id].y = arrInitialPos[id].y; + arrPosition[id].y = arrParallax[id].y; } } @@ -306,34 +367,17 @@ namespace Systems { Registry::getInstance().getComponents(); Registry::components arrParallax = Registry::getInstance().getComponents(); - Registry::components arrVelocity = - Registry::getInstance().getComponents(); - Registry::components arrInitialPos = - Registry::getInstance().getComponents(); - std::vector ids = arrParallax.getExistingsId(); + std::vector ids = Registry::getInstance().getEntitiesByComponents( + {typeid(Types::Position), typeid(Types::Parallax), typeid(Types::Velocity)}); for (auto &id : ids) { - if (arrPosition.exist(id) && arrVelocity.exist(id) && arrInitialPos.exist(id)) { - resetParallaxPosition(id, arrInitialPos, arrPosition); - } - } - } - - 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); - } + resetParallaxPosition(id, arrParallax, arrPosition); } } const std::string playerFile = "assets/Json/playerData.json"; + const std::size_t deathTime = 500; void initPlayer(std::size_t managerId, std::size_t systemId) { @@ -341,6 +385,7 @@ namespace Systems { std::size_t id = Registry::getInstance().addEntity(); if (jsonData["player"] == nullptr) { + SystemManagersDirector::getInstance().getSystemManager(managerId).removeSystem(systemId); return; } jsonData = jsonData["player"]; @@ -363,7 +408,7 @@ namespace Systems { Registry::getInstance().getComponents().insertBack({}); Registry::getInstance().getComponents().insertBack({jsonData["damage"]}); Registry::getInstance().getComponents().insertBack({jsonData["health"]}); - Registry::getInstance().getComponents().insertBack({std::nullopt}); + Registry::getInstance().getComponents().insertBack({std::nullopt, deathTime}); Registry::getInstance().setToFrontLayers(id); SystemManagersDirector::getInstance().getSystemManager(managerId).removeSystem(systemId); } @@ -371,17 +416,19 @@ namespace Systems { std::vector> getECSSystems() { return { - windowCollision, - initPlayer, - initParalax, - entitiesCollision, - moveEntities, #ifndef NDEBUG debugCollisionRect, #else #endif + windowCollision, + checkDestroyAfterDeathCallBack, + initPlayer, + initParalax, + initEnnemy, + entitiesCollision, + destroyOutsideWindow, deathChecker, - manageOutsideWindowEntity, + moveEntities, manageParallax}; } } // namespace Systems