From 058c46c9342598e6883b61038a25b691093871cc Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Sat, 30 Sep 2023 18:23:17 +0200 Subject: [PATCH] ECS: Add function dense SparseArrays MINOR --- src/Client/SceneManager.cpp | 4 +- src/Client/SceneManager.hpp | 6 -- src/Client/Systems/Events/EventsSystems.cpp | 4 +- src/Client/Systems/Graphic/GraphicSystems.cpp | 11 ++-- src/ECS/ECSCustomTypes.hpp | 3 +- src/ECS/Registry.cpp | 30 +--------- src/ECS/Registry.hpp | 39 +------------ src/ECS/SparseArray.hpp | 29 ++++++---- src/ECS/Systems/Systems.cpp | 57 ++++++++----------- 9 files changed, 56 insertions(+), 127 deletions(-) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index 9e73f6ec..5cfcbeba 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -65,8 +65,6 @@ int SceneManager::run() auto &director = Systems::SystemManagersDirector::getInstance(); try { - Registry::getInstance().initCustomSparseArrays( - _scenesCustomIndexes.at(_currentScene)); while (!_stop && !Raylib::windowShouldClose()) { Raylib::beginDrawing(); Raylib::clearBackground(Raylib::DarkGray); @@ -87,7 +85,7 @@ int SceneManager::run() void SceneManager::changeScene(Scene scene) { _currentScene = scene; - Registry::getInstance().clear(_scenesCustomIndexes.at(_currentScene)); + Registry::getInstance().clear(); Systems::SystemManagersDirector::getInstance().resetChanges(); } diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index f39135c0..cbc95f83 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -12,8 +12,6 @@ #include #include -enum CustomIndex { BULLET, PLAYER, ENNEMY }; - enum ReturnValue { OK = 0, ERROR = 84 }; enum Scene { MENU, MAIN_GAME, SCENE_MAX }; @@ -37,10 +35,6 @@ class SceneManager { std::vector {EVENTS, GAME, DISPLAY}, std::vector {EVENTS, GAME, DISPLAY} }; - const std::array, 2> _scenesCustomIndexes = { - std::vector {PLAYER }, - std::vector { PLAYER, BULLET, ENNEMY} - }; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) static bool _init; diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index 0a8591d9..293357c2 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -19,7 +19,9 @@ namespace Systems { Registry::components arrPosition = Registry::getInstance().getComponents(); - std::vector playerId = Registry::getInstance().getCustomSparseArray().getExistingsId(); + std::vector playerId = Registry::getInstance() + .getComponents() + .getExistingsId(); for (std::size_t id : playerId) { if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) { diff --git a/src/Client/Systems/Graphic/GraphicSystems.cpp b/src/Client/Systems/Graphic/GraphicSystems.cpp index b0f0a751..756bb20b 100644 --- a/src/Client/Systems/Graphic/GraphicSystems.cpp +++ b/src/Client/Systems/Graphic/GraphicSystems.cpp @@ -36,8 +36,8 @@ namespace Systems { (position.y * static_cast(Raylib::getScreenHeight())) / denominator; - float width = (rectangle.width - * static_cast(Raylib::getScreenWidth())) + float width = + (rectangle.width * static_cast(Raylib::getScreenWidth())) / denominator; float height = (rectangle.height * static_cast(Raylib::getScreenHeight())) @@ -118,10 +118,7 @@ namespace Systems { for (auto id : spriteIndexes) { if (arrRect.exist(id) && arrPosition.exist(id)) { - drawSpriteWithRect( - arrPosition[id], - arrSprite[id], - arrRect[id]); + drawSpriteWithRect(arrPosition[id], arrSprite[id], arrRect[id]); } else if (arrPosition.exist(id)) { drawSpriteWithoutRect(arrPosition[id], arrSprite[id]); } @@ -182,7 +179,7 @@ namespace Systems { Registry::components arrText = Registry::getInstance().getComponents(); - for (auto &textIt: arrText) { + for (auto &textIt : arrText) { drawTextResponsive(textIt); } } diff --git a/src/ECS/ECSCustomTypes.hpp b/src/ECS/ECSCustomTypes.hpp index 2d38457f..0aa01b59 100644 --- a/src/ECS/ECSCustomTypes.hpp +++ b/src/ECS/ECSCustomTypes.hpp @@ -26,7 +26,6 @@ namespace Types { float y; }; - struct Player { - }; + struct Player { }; } // namespace Types diff --git a/src/ECS/Registry.cpp b/src/ECS/Registry.cpp index 6eb34ba5..008c44e6 100644 --- a/src/ECS/Registry.cpp +++ b/src/ECS/Registry.cpp @@ -32,40 +32,14 @@ void Registry::removeEntity(std::size_t id) } } -void Registry::clear(std::vector indexes) +void Registry::clear() { _data.clear(); _addComponentPlaceFunctions.clear(); _removeComponentFunctions.clear(); _entitiesNb = 0; - _customSparseArrays.clear(); - initCustomSparseArrays(indexes); } -std::size_t Registry::getEntitiesNb() const +Registry::Registry() : _entitiesNb(0) { - return (_entitiesNb); -} - -void Registry::initCustomSparseArrays(std::vector indexes) -{ - for (auto index : indexes) { - addCustomSparseIndex(static_cast(index)); - } -} - -Registry::Registry() : _entitiesNb(0), _maxCustomId(0) -{ -} - -void Registry::addCustomSparseIndex(std::size_t id) -{ - if (_customSparseArrays.find(id) != _customSparseArrays.end()) { - throw std::runtime_error( - "addCustomSparseIndex: id" + std::to_string(id) + " already exist"); - }; - _customSparseArrays[id] = SparseArray(); - if (id > _maxCustomId) { - _maxCustomId = id; - } } diff --git a/src/ECS/Registry.hpp b/src/ECS/Registry.hpp index 176e64b7..88e520ca 100644 --- a/src/ECS/Registry.hpp +++ b/src/ECS/Registry.hpp @@ -37,39 +37,7 @@ class Registry { void removeEntity(std::size_t /*id*/); - void clear(std::vector); - - template - components getCustomSparseArray(std::size_t id) - { - if (_customSparseArrays.find(id) == _customSparseArrays.end()) { - throw std::runtime_error( - "getCustomSparseArray ID not in :" + std::to_string(id)); - } - try { - components castedComponent = - std::any_cast>( - _customSparseArrays[id]); - - return castedComponent; - } catch (const std::bad_any_cast &e) { - throw std::runtime_error("Bad cast: " + std::string(e.what())); - } - } - - template - std::size_t addCustomSparseArray() - { - _maxCustomId++; - std::size_t id = _maxCustomId; - - _customSparseArrays[id] = SparseArray(); - return (id); - } - - std::size_t getEntitiesNb() const; - - void initCustomSparseArrays(std::vector indexes); + void clear(); Registry &operator=(const Registry &) = delete; Registry(const Registry &) = delete; @@ -114,8 +82,6 @@ class Registry { _data[typeid(Component)]); } - void addCustomSparseIndex(std::size_t id); - // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) static Registry _instance; // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) @@ -126,8 +92,5 @@ class Registry { _removeComponentFunctions; std::unordered_map _data; - std::unordered_map _customSparseArrays; - std::size_t _maxCustomId; - std::size_t _entitiesNb; }; diff --git a/src/ECS/SparseArray.hpp b/src/ECS/SparseArray.hpp index bb236d6f..d571451e 100644 --- a/src/ECS/SparseArray.hpp +++ b/src/ECS/SparseArray.hpp @@ -8,7 +8,6 @@ #pragma once #include -#include #include template @@ -16,19 +15,23 @@ class SparseArray { public: void add() { - _sparse.push_back(-1); + _sparse.push_back(std::size_t(-1)); } + void insertBack(const Component &value) { insert(_sparse.size() - 1, value); } - void insert(size_t id, const Component &value) { - if (id >= sparse.size()) { - throw std::runtime_error("SparseArrays::insert: ID out of bounds!"); + + void insert(size_t id, const Component &value) + { + if (id >= _sparse.size()) { + throw std::runtime_error( + "SparseArrays::insert: ID out of bounds!"); } if (_sparse[id] > -1) { - _dense[_sparse[id]] = value; + _dense[_sparse[id]] = value; _revSparse[_sparse[id]] = id; return; } @@ -37,10 +40,12 @@ class SparseArray { _dense.push_back(value); _revSparse.push_back(id); } + void erase(std::size_t id) { - if (id >= sparse.size()) { - throw std::runtime_error("SparseArrays::erase: ID out of bounds!"); + if (id >= _sparse.size()) { + throw std::runtime_error( + "SparseArrays::erase: ID out of bounds!"); } if (_sparse[id] != -1) { auto it = _dense.begin(); @@ -54,6 +59,7 @@ class SparseArray { std::advance(it, id); _sparse.erase(it); }; + Component &operator[](size_t id) { throwIfDontExist(id); @@ -65,13 +71,16 @@ class SparseArray { return _dense.begin(); } - typename std::vector>::iterator end() + typename std::vector::iterator end() { return _dense.end(); } - bool exist(std::size_t id) { + + bool exist(std::size_t id) + { return id < _sparse.size() && _sparse[id] != -1; } + std::vector getExistingsId() { return _revSparse; diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index f63a50fe..a55f6cb9 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -20,7 +20,9 @@ namespace Systems { Registry::components arrCollisionRect = Registry::getInstance().getComponents(); - std::vector playerIdx = Registry::getInstance().getCustomSparseArray().getExistingsId(); + std::vector playerIdx = Registry::getInstance() + .getComponents() + .getExistingsId(); const float maxPercent = 100.0F; for (std::size_t id : playerIdx) { @@ -30,17 +32,11 @@ namespace Systems { if (arrPosition[id].y < 0) { arrPosition[id].y = 0; } - if (arrPosition[id].x - + arrCollisionRect[id].width - > maxPercent) { - arrPosition[id].x = - maxPercent - arrCollisionRect[id].width; + if (arrPosition[id].x + arrCollisionRect[id].width > maxPercent) { + arrPosition[id].x = maxPercent - arrCollisionRect[id].width; } - if (arrPosition[id].y - + arrCollisionRect[id].height - > maxPercent) { - arrPosition[id].y = maxPercent - - arrCollisionRect[id].height; + if (arrPosition[id].y + arrCollisionRect[id].height > maxPercent) { + arrPosition[id].y = maxPercent - arrCollisionRect[id].height; } } } @@ -61,29 +57,26 @@ namespace Systems { void init(std::size_t managerId, std::size_t systemId) { Registry::getInstance().addEntity(); - Registry::getInstance().getComponents().insertBack({ - playerData, - playerData}); - Registry::getInstance().getComponents().insertBack({ - playerPath, - playerWidth, - playerHeight}); + Registry::getInstance().getComponents().insertBack( + {playerData, playerData}); + Registry::getInstance().getComponents().insertBack( + {playerPath, playerWidth, playerHeight}); Registry::getInstance().getComponents().insertBack( spriteRect); - Registry::getInstance().getComponents().insertBack( - collisionRect); - Registry::getInstance().getComponents().insertBack(); - Registry::getInstance().getComponents().insertBack({ - musicPath, - musicVolume}); - Registry::getInstance().getComponents().insertBack({ - soundPath, - soundVolume}); - Registry::getInstance().getComponents().insertBack({ - "Press space to play music, enter to play sound", - textPos, - fontScale, - Raylib::DarkBlue}); + Registry::getInstance() + .getComponents() + .insertBack(collisionRect); + Registry::getInstance().getComponents().insertBack({}); + Registry::getInstance().getComponents().insertBack( + {musicPath, musicVolume}); + Registry::getInstance().getComponents().insertBack( + {soundPath, soundVolume}); + Registry::getInstance().getComponents().insertBack( + {"Press SPACE to play music, ENTER to play sound, J to reset " + "scene, ARROWS to move", + textPos, + fontScale, + Raylib::DarkBlue}); SystemManagersDirector::getInstance() .getSystemManager(managerId) .removeSystem(systemId);