From 97df4e4160ca1e57821c49fcc7fd0cbfe4671060 Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Mon, 25 Sep 2023 09:39:26 +0200 Subject: [PATCH 01/14] ECS: Add current progress implement SceneManager --- src/Client/CMakeLists.txt | 2 +- src/Client/SceneManager.cpp | 28 ++++++++++++++++ src/Client/SceneManager.hpp | 33 +++++++++++++++++++ .../Systems/Managers/GraphicManager.hpp | 4 +-- .../Systems/Managers/SystemEventsManager.hpp | 2 +- src/ECS/Systems/Managers/ASystemManager.hpp | 11 +++---- src/ECS/Systems/Managers/ISystemManager.hpp | 20 +++++++++++ 7 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 src/Client/SceneManager.cpp create mode 100644 src/Client/SceneManager.hpp create mode 100644 src/ECS/Systems/Managers/ISystemManager.hpp diff --git a/src/Client/CMakeLists.txt b/src/Client/CMakeLists.txt index ddaf84ab..d27625a3 100644 --- a/src/Client/CMakeLists.txt +++ b/src/Client/CMakeLists.txt @@ -9,7 +9,7 @@ target_include_directories( target_sources( ${PROJECT_NAME_CLIENT} PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/SceneManager.cpp ) add_subdirectory(Systems) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp new file mode 100644 index 00000000..10d691e8 --- /dev/null +++ b/src/Client/SceneManager.cpp @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2023 +** R-Bus +** File description: +** SceneManager implementation +*/ + +#include "SceneManager.hpp" +#include "GameManager.hpp" +#include "SystemEventManager.hpp" + +SceneManager SceneManager::_instance = SceneManager(); + +SceneManager::SceneManager() : _currentScene(Scene::GAME) +{ + _logicManagers.push_back(GameManager()); + _eventManagers.push_back(SystemEventManager()); +} + +SceneManager &SceneManager::getInstance() +{ + return _instance; +} + +void SceneManager::changeScene() +{ + +} diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp new file mode 100644 index 00000000..30fb44d5 --- /dev/null +++ b/src/Client/SceneManager.hpp @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2023 +** R-Bus +** File description: +** SceneManager +*/ + +#pragma once + +#include +#include "ISystemManager.hpp" +#include "GraphicManager.hpp" + +enum Scene { + GAME, + Scene_max +}; + +class SceneManager { + public: + static SceneManager &getInstance(); + void changeScene(); + private: + SceneManager(); + std::list _logicManagers; + std::list _eventManagers; + GraphicManager _graphicManager; + Scene _currentScene; + + // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) + static SceneManager _instance; + // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) +}; diff --git a/src/Client/Systems/Managers/GraphicManager.hpp b/src/Client/Systems/Managers/GraphicManager.hpp index 02dfd779..ddaf4bc4 100644 --- a/src/Client/Systems/Managers/GraphicManager.hpp +++ b/src/Client/Systems/Managers/GraphicManager.hpp @@ -12,9 +12,9 @@ namespace Systems { class GraphicManager : public ASystemManager { public: - ~GraphicManager() override; + ~GraphicManager() final; static GraphicManager &getInstance(); - void updateSystems() override; + void updateSystems() final; GraphicManager(GraphicManager const &) = delete; GraphicManager(GraphicManager const &&) = delete; diff --git a/src/Client/Systems/Managers/SystemEventsManager.hpp b/src/Client/Systems/Managers/SystemEventsManager.hpp index 7528dc08..ec4c85ca 100644 --- a/src/Client/Systems/Managers/SystemEventsManager.hpp +++ b/src/Client/Systems/Managers/SystemEventsManager.hpp @@ -12,7 +12,7 @@ namespace Systems { class SystemEventsManager : public ASystemManager { public: - ~SystemEventsManager() override = default; + ~SystemEventsManager() final = default; static SystemEventsManager &getInstance(); SystemEventsManager(SystemEventsManager const &) = delete; diff --git a/src/ECS/Systems/Managers/ASystemManager.hpp b/src/ECS/Systems/Managers/ASystemManager.hpp index 89dee908..c2cc73bb 100644 --- a/src/ECS/Systems/Managers/ASystemManager.hpp +++ b/src/ECS/Systems/Managers/ASystemManager.hpp @@ -7,25 +7,24 @@ #pragma once -#include #include #include #include +#include "ISystemManager.hpp" namespace Systems { - class ASystemManager { + class ASystemManager : public ISystemManager { public: - virtual ~ASystemManager() = default; ASystemManager(const ASystemManager &) = delete; ASystemManager &operator=(const ASystemManager &) = delete; ASystemManager(ASystemManager &&) = delete; ASystemManager &operator=(ASystemManager &&) = delete; - virtual void updateSystems(); + void updateSystems() override; - virtual void addSystem(std::function /*sys*/); + void addSystem(std::function /*sys*/) override; - virtual void removeSystem(std::size_t /*id*/); + void removeSystem(std::size_t /*id*/) override; protected: ASystemManager() = default; diff --git a/src/ECS/Systems/Managers/ISystemManager.hpp b/src/ECS/Systems/Managers/ISystemManager.hpp new file mode 100644 index 00000000..f86fa7c1 --- /dev/null +++ b/src/ECS/Systems/Managers/ISystemManager.hpp @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2023 +** R-Bus +** File description: +** ISystemManager +*/ + +#pragma once + +#include +#include + +class ISystemManager { + public: + virtual ~ASystemManager() = default; + virtual ISystemManager &getInstance() = 0; + virtual void updateSystems() = 0; + virtual void addSystem(std::function /*sys*/) = 0; + virtual void removeSystem(std::size_t /*id*/) = 0; +} From 8abbd630190d1ca65019f1f28dd9081c3a557e6e Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Wed, 27 Sep 2023 11:11:44 +0200 Subject: [PATCH 02/14] ECS: Add current progress bugging registry --- src/Client/SceneManager.cpp | 14 ++++++++++--- src/Client/SceneManager.hpp | 1 - src/Client/Systems/ClientSystems.cpp | 20 +++++++++++++++++++ src/Client/Systems/ClientSystems.hpp | 8 ++------ src/Client/Systems/Events/EventsSystems.cpp | 11 ++++++++++ src/Client/Systems/Events/EventsSystems.hpp | 4 +--- src/Client/Systems/Graphic/GraphicSystems.cpp | 10 ++++++++++ src/Client/Systems/Graphic/GraphicSystems.hpp | 8 +------- .../Managers/SystemManagersDirector.cpp | 6 ------ .../Managers/SystemManagersDirector.hpp | 2 +- src/ECS/Systems/Systems.cpp | 6 ++++++ src/ECS/Systems/Systems.hpp | 4 +--- 12 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 src/Client/Systems/ClientSystems.cpp diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index b2d3e053..5600acb9 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -33,8 +33,11 @@ static void initRaylib() InitAudioDevice(); Registry ®istry = Registry::getInstance(); registry.addEntity(); + std::cout << "init2" << std::endl; + Registry::components res = registry.getComponents(); + std::cout << "init3" << std::endl; + res.back() = {10, 20}; registry.getComponents().back() = {0, 0}; - registry.getComponents().back() = {10, 20}; registry.getComponents().back() = { "./assets/R-TypeSheet/r-typesheet18.gif", 10, @@ -54,6 +57,7 @@ static void initRaylib() registry.addEntity(); registry.getComponents().back() = Types::SoundEffect("assets/Audio/Sounds/yes.ogg"); + std::cout << "init4" << std::endl; registry.addEntity(); registry.getComponents().back() = Types::MusicStream("assets/Audio/Musics/Title.mp3"); @@ -70,14 +74,18 @@ static void destroyRaylib() SceneManager::SceneManager() : _currentScene(Scene::MAIN_GAME), _stop(false) { + std::cout << "start" << std::endl; auto &director = Systems::SystemManagersDirector::getInstance(); - for (auto systems : Systems::systemsGroups) { - std::cout << systems.size() << std::endl; + for (auto systems : Systems::getSystemsGroups()) { + std::cout << "loop" << std::endl; director.addSystemManager(systems); } + std::cout << "before" << std::endl; initRaylib(); + std::cout << "after" << std::endl; while (!_stop && !WindowShouldClose()) { + std::cout << "main loop" << std::endl; BeginDrawing(); ClearBackground(RAYWHITE); auto scene = _scenes.at(_currentScene); diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index ad866d33..308c363b 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -24,7 +24,6 @@ enum SystemManagers { MANAGER_MAX }; - class SceneManager { public: static SceneManager &getInstance(); diff --git a/src/Client/Systems/ClientSystems.cpp b/src/Client/Systems/ClientSystems.cpp new file mode 100644 index 00000000..6db152ad --- /dev/null +++ b/src/Client/Systems/ClientSystems.cpp @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2023 +** R-Bus +** File description: +** ClientSystems +*/ + +#include "ClientSystems.hpp" + + +namespace Systems { + const std::array>, 3> getSystemsGroups() + { + return { + getECSSystems(), + EventsSystems::getEventSystems(), + GraphicSystems::getGraphicsSystems() + }; + } +} diff --git a/src/Client/Systems/ClientSystems.hpp b/src/Client/Systems/ClientSystems.hpp index 613c81b1..2ee87afd 100644 --- a/src/Client/Systems/ClientSystems.hpp +++ b/src/Client/Systems/ClientSystems.hpp @@ -2,7 +2,7 @@ ** EPITECH PROJECT, 2023 ** R-Bus ** File description: -** Systems +** ClientSystems */ #pragma once @@ -13,9 +13,5 @@ #include "EventsSystems.hpp" namespace Systems { - const std::array>, 3> systemsGroups { - ecsSystems, - EventsSystems::eventSystems, - GraphicSystems::graphicSystems - }; + const std::array>, 3> getSystemsGroups(); } // namespace Systems diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index df582ea6..13eea733 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -5,11 +5,16 @@ ** EventsSystems implementation */ +#include + #include "EventsSystems.hpp" +#include "Registry.hpp" +#include "CustomTypes.hpp" namespace Systems { void EventsSystems::playerMovement(std::size_t /*unused*/) { + std::cout << "movement" << std::endl; Registry::components arrPosition = Registry::getInstance().getComponents(); @@ -39,4 +44,10 @@ namespace Systems { playerIt++; } } + const std::vector> EventsSystems::getEventSystems() + { + return { + playerMovement + }; + } } diff --git a/src/Client/Systems/Events/EventsSystems.hpp b/src/Client/Systems/Events/EventsSystems.hpp index a84a9da7..0cb5fe57 100644 --- a/src/Client/Systems/Events/EventsSystems.hpp +++ b/src/Client/Systems/Events/EventsSystems.hpp @@ -14,8 +14,6 @@ namespace Systems { namespace EventsSystems { void playerMovement(std::size_t /*unused*/); - const std::vector> eventSystems { - playerMovement - }; + const std::vector> getEventSystems(); } // namespace EventsSystems } diff --git a/src/Client/Systems/Graphic/GraphicSystems.cpp b/src/Client/Systems/Graphic/GraphicSystems.cpp index f93b77f1..f54c7fe6 100644 --- a/src/Client/Systems/Graphic/GraphicSystems.cpp +++ b/src/Client/Systems/Graphic/GraphicSystems.cpp @@ -206,4 +206,14 @@ namespace Systems { textIt++; } } + const std::vector> GraphicSystems::getGraphicsSystems() + { + return { + rectRenderer, + spriteRenderer, + textRenderer, + musicPlayer, + soundEffectPlayer + }; + } } // namespace Systems diff --git a/src/Client/Systems/Graphic/GraphicSystems.hpp b/src/Client/Systems/Graphic/GraphicSystems.hpp index aa199155..7240b7ce 100644 --- a/src/Client/Systems/Graphic/GraphicSystems.hpp +++ b/src/Client/Systems/Graphic/GraphicSystems.hpp @@ -18,12 +18,6 @@ namespace Systems { void textRenderer(std::size_t /*unused*/); void soundEffectPlayer(std::size_t /*unused*/); void musicPlayer(std::size_t /*unused*/); - const std::vector> graphicSystems { - rectRenderer, - spriteRenderer, - textRenderer, - musicPlayer, - soundEffectPlayer - }; + const std::vector> getGraphicsSystems(); } // namespace GraphicSystems } diff --git a/src/ECS/Systems/Managers/SystemManagersDirector.cpp b/src/ECS/Systems/Managers/SystemManagersDirector.cpp index 239ff303..13a20d57 100644 --- a/src/ECS/Systems/Managers/SystemManagersDirector.cpp +++ b/src/ECS/Systems/Managers/SystemManagersDirector.cpp @@ -44,10 +44,4 @@ namespace Systems { std::advance(it, id); _systemManagers.erase(it); } - - SystemManagersDirector::SystemManagersDirector() - { - addSystemManager(ecsSystems); - } - } // namespace Systems diff --git a/src/ECS/Systems/Managers/SystemManagersDirector.hpp b/src/ECS/Systems/Managers/SystemManagersDirector.hpp index 1548ecdd..1cb714e4 100644 --- a/src/ECS/Systems/Managers/SystemManagersDirector.hpp +++ b/src/ECS/Systems/Managers/SystemManagersDirector.hpp @@ -23,7 +23,7 @@ namespace Systems { private: std::vector _systemManagers; - SystemManagersDirector(); + SystemManagersDirector() = default; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) static SystemManagersDirector _instance; diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index ccc389c0..73b96837 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -52,4 +52,10 @@ namespace Systems { playerIt++; } } + + const std::vector> getECSSystems() { + return { + windowCollision + }; + } } // namespace Systems diff --git a/src/ECS/Systems/Systems.hpp b/src/ECS/Systems/Systems.hpp index 17323ba6..264f4c3d 100644 --- a/src/ECS/Systems/Systems.hpp +++ b/src/ECS/Systems/Systems.hpp @@ -13,7 +13,5 @@ namespace Systems { void windowCollision(std::size_t); - const std::vector> ecsSystems { - windowCollision - }; + const std::vector> getECSSystems(); } // namespace Systems From a1d68d936ed7c36590e2a55b3fff7faef7e76f9a Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Wed, 27 Sep 2023 16:00:25 +0200 Subject: [PATCH 03/14] ECS: Add current progress --- src/Client/SceneManager.cpp | 37 ++++++++++++--------- src/Client/SceneManager.hpp | 14 ++++---- src/Client/Systems/Events/EventsSystems.cpp | 9 +++-- src/ECS/Registry.cpp | 7 ++++ src/ECS/Registry.hpp | 2 ++ src/main_client.cpp | 3 ++ 6 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index 5600acb9..b0d25cfc 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -18,9 +18,17 @@ constexpr int screenWidth = 1920; constexpr int screenHeight = 1080; constexpr int fps = 60; -// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) -SceneManager SceneManager::_instance = SceneManager(); -// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) +std::optional SceneManager::_instance = std::nullopt; + +SceneManager &SceneManager::getInstance() +{ + if (_instance == std::nullopt) { + std::cout << "SCENE CONTRUCTION" << std::endl; + const std::optional a = SceneManager(); + _instance = a; + } + return _instance.value(); +} static void initRaylib() { @@ -33,17 +41,15 @@ static void initRaylib() InitAudioDevice(); Registry ®istry = Registry::getInstance(); registry.addEntity(); - std::cout << "init2" << std::endl; Registry::components res = registry.getComponents(); - std::cout << "init3" << std::endl; res.back() = {10, 20}; + registry.getComponents().back() = Types::Player(true); registry.getComponents().back() = {0, 0}; registry.getComponents().back() = { "./assets/R-TypeSheet/r-typesheet18.gif", 10, 20}; registry.getComponents().back() = {2.0F, 5.0F, 30.5F, 25.2F}; - registry.getComponents().back() = Types::Player(true); registry.addEntity(); registry.getComponents().back() = {0, 0}; registry.getComponents().back() = {10, 10}; @@ -57,7 +63,6 @@ static void initRaylib() registry.addEntity(); registry.getComponents().back() = Types::SoundEffect("assets/Audio/Sounds/yes.ogg"); - std::cout << "init4" << std::endl; registry.addEntity(); registry.getComponents().back() = Types::MusicStream("assets/Audio/Musics/Title.mp3"); @@ -74,18 +79,14 @@ static void destroyRaylib() SceneManager::SceneManager() : _currentScene(Scene::MAIN_GAME), _stop(false) { - std::cout << "start" << std::endl; + std::cout << "CONSTRUCTEUR" << std::endl; auto &director = Systems::SystemManagersDirector::getInstance(); for (auto systems : Systems::getSystemsGroups()) { - std::cout << "loop" << std::endl; director.addSystemManager(systems); } - std::cout << "before" << std::endl; initRaylib(); - std::cout << "after" << std::endl; while (!_stop && !WindowShouldClose()) { - std::cout << "main loop" << std::endl; BeginDrawing(); ClearBackground(RAYWHITE); auto scene = _scenes.at(_currentScene); @@ -97,14 +98,18 @@ SceneManager::SceneManager() : _currentScene(Scene::MAIN_GAME), _stop(false) destroyRaylib(); } -SceneManager &SceneManager::getInstance() -{ - return _instance; -} void SceneManager::changeScene(Scene scene) { + std::cout << "changeScene "; + std::cout << static_cast(scene) << " " << static_cast(_currentScene) << std::endl; _currentScene = scene; + Registry::getInstance().clear(); + Registry ®istry = Registry::getInstance(); + registry.addEntity(); + registry.getComponents().back() = {0, 0}; + registry.getComponents().back() = {10, 10}; + registry.getComponents().back() = Types::Player(true); } void SceneManager::stop() diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index 308c363b..5b315a08 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -11,9 +11,13 @@ #include #include #include +#include + +#include enum Scene { MAIN_GAME, + MENU, SCENE_MAX }; @@ -34,11 +38,9 @@ class SceneManager { Scene _currentScene; bool _stop; - const std::array, 1> _scenes = { - std::vector{EVENTS, GAME, DISPLAY} + static std::optional _instance; + const std::array, 2> _scenes = { + std::vector{EVENTS, GAME, DISPLAY}, + std::vector{EVENTS, DISPLAY} }; - - // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) - static SceneManager _instance; - // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) }; diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index 13eea733..a841a735 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -5,16 +5,15 @@ ** EventsSystems implementation */ -#include - #include "EventsSystems.hpp" #include "Registry.hpp" #include "CustomTypes.hpp" +#include "SceneManager.hpp" + namespace Systems { void EventsSystems::playerMovement(std::size_t /*unused*/) { - std::cout << "movement" << std::endl; Registry::components arrPosition = Registry::getInstance().getComponents(); @@ -34,9 +33,13 @@ namespace Systems { positionIt->value().x -= 1; } if (IsKeyDown(KEY_UP)) { + std::cout << "UP" << std::endl; + SceneManager::getInstance().changeScene(Scene::MENU); positionIt->value().y -= 1; } if (IsKeyDown(KEY_DOWN)) { + std::cout << "DOWN" << std::endl; + SceneManager::getInstance().changeScene(Scene::MAIN_GAME); positionIt->value().y += 1; } } diff --git a/src/ECS/Registry.cpp b/src/ECS/Registry.cpp index d523652d..8d31b10b 100644 --- a/src/ECS/Registry.cpp +++ b/src/ECS/Registry.cpp @@ -31,6 +31,13 @@ void Registry::removeEntity(std::size_t id) } } +void Registry::clear() +{ + _data.clear(); + _addComponentPlaceFunctions.clear(); + _removeComponentFunctions.clear(); +} + Registry::Registry() : _entitiesNb(0) { } diff --git a/src/ECS/Registry.hpp b/src/ECS/Registry.hpp index 583db235..15bdfd75 100644 --- a/src/ECS/Registry.hpp +++ b/src/ECS/Registry.hpp @@ -36,6 +36,8 @@ class Registry { void removeEntity(std::size_t /*id*/); + void clear(); + Registry &operator=(const Registry &) = delete; Registry(const Registry &) = delete; void operator=(const Registry &&) = delete; diff --git a/src/main_client.cpp b/src/main_client.cpp index 93f282df..7d1aebe3 100644 --- a/src/main_client.cpp +++ b/src/main_client.cpp @@ -5,6 +5,9 @@ ** main */ +#include "SceneManager.hpp" + int main() { + SceneManager::getInstance(); } From 99371f8e253cc08796027f57a6d9c6508ca8c621 Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Wed, 27 Sep 2023 18:46:18 +0200 Subject: [PATCH 04/14] ECS: Add functional SceneManager MAJOR --- src/Client/SceneManager.cpp | 70 +++++-------------- src/Client/SceneManager.hpp | 29 ++++---- src/Client/Systems/ClientSystems.cpp | 9 ++- src/Client/Systems/ClientSystems.hpp | 7 +- src/Client/Systems/Events/EventsSystems.cpp | 17 ++--- src/Client/Systems/Events/EventsSystems.hpp | 6 +- src/Client/Systems/Graphic/GraphicSystems.cpp | 8 +-- src/Client/Systems/Graphic/GraphicSystems.hpp | 6 +- src/ECS/Systems/Managers/SystemManager.cpp | 3 +- src/ECS/Systems/Systems.cpp | 8 +-- src/ECS/Systems/Systems.hpp | 2 +- 11 files changed, 59 insertions(+), 106 deletions(-) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index b0d25cfc..b811278d 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -5,29 +5,32 @@ ** SceneManager implementation */ +#include "SceneManager.hpp" #include "raylib.h" #include "ClientSystems.hpp" -#include "SceneManager.hpp" -#include "SystemManagersDirector.hpp" - -#include #include "Registry.hpp" -#include "CustomTypes.hpp" +#include "SystemManagersDirector.hpp" -constexpr int screenWidth = 1920; +constexpr int screenWidth = 1920; constexpr int screenHeight = 1080; -constexpr int fps = 60; +constexpr int fps = 60; -std::optional SceneManager::_instance = std::nullopt; +// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) +bool SceneManager::_init = false; +SceneManager SceneManager::_instance = SceneManager(); +// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) SceneManager &SceneManager::getInstance() { - if (_instance == std::nullopt) { - std::cout << "SCENE CONTRUCTION" << std::endl; - const std::optional a = SceneManager(); - _instance = a; + if (!_init) { + _init = true; + _instance.init(); } - return _instance.value(); + return _instance; +} + +SceneManager::SceneManager() : _currentScene(Scene::MAIN_GAME), _stop(false) +{ } static void initRaylib() @@ -39,36 +42,6 @@ static void initRaylib() "rectangles"); SetTargetFPS(fps); InitAudioDevice(); - Registry ®istry = Registry::getInstance(); - registry.addEntity(); - Registry::components res = registry.getComponents(); - res.back() = {10, 20}; - registry.getComponents().back() = Types::Player(true); - registry.getComponents().back() = {0, 0}; - registry.getComponents().back() = { - "./assets/R-TypeSheet/r-typesheet18.gif", - 10, - 20}; - registry.getComponents().back() = {2.0F, 5.0F, 30.5F, 25.2F}; - registry.addEntity(); - registry.getComponents().back() = {0, 0}; - registry.getComponents().back() = {10, 10}; - registry.addEntity(); - registry.getComponents().back() = {40, 40}; - registry.getComponents().back() = { - "Player", - BLACK, - LoadFont("assets/Fonts/soliden/SolidenTrial-Black.otf"), - 5.5}; - registry.addEntity(); - registry.getComponents().back() = - Types::SoundEffect("assets/Audio/Sounds/yes.ogg"); - registry.addEntity(); - registry.getComponents().back() = - Types::MusicStream("assets/Audio/Musics/Title.mp3"); - if (registry.getComponents().back().has_value()) { - registry.getComponents().back().value().needToPlay = true; - } } static void destroyRaylib() @@ -77,9 +50,8 @@ static void destroyRaylib() CloseWindow(); } -SceneManager::SceneManager() : _currentScene(Scene::MAIN_GAME), _stop(false) +void SceneManager::init() { - std::cout << "CONSTRUCTEUR" << std::endl; auto &director = Systems::SystemManagersDirector::getInstance(); for (auto systems : Systems::getSystemsGroups()) { @@ -98,18 +70,10 @@ SceneManager::SceneManager() : _currentScene(Scene::MAIN_GAME), _stop(false) destroyRaylib(); } - void SceneManager::changeScene(Scene scene) { - std::cout << "changeScene "; - std::cout << static_cast(scene) << " " << static_cast(_currentScene) << std::endl; _currentScene = scene; Registry::getInstance().clear(); - Registry ®istry = Registry::getInstance(); - registry.addEntity(); - registry.getComponents().back() = {0, 0}; - registry.getComponents().back() = {10, 10}; - registry.getComponents().back() = Types::Player(true); } void SceneManager::stop() diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index 5b315a08..46e0d344 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -7,40 +7,37 @@ #pragma once -#include -#include #include #include +#include #include +#include #include -enum Scene { - MAIN_GAME, - MENU, - SCENE_MAX -}; +enum Scene { MAIN_GAME, MENU, SCENE_MAX }; -enum SystemManagers { - GAME, - EVENTS, - DISPLAY, - MANAGER_MAX -}; +enum SystemManagers { GAME, EVENTS, DISPLAY, MANAGER_MAX }; class SceneManager { public: static SceneManager &getInstance(); void changeScene(Scene scene); void stop(); + private: SceneManager(); + void init(); Scene _currentScene; bool _stop; - static std::optional _instance; const std::array, 2> _scenes = { - std::vector{EVENTS, GAME, DISPLAY}, - std::vector{EVENTS, DISPLAY} + std::vector {EVENTS, GAME, DISPLAY}, + std::vector {EVENTS, DISPLAY} }; + + // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) + static bool _init; + static SceneManager _instance; + // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) }; diff --git a/src/Client/Systems/ClientSystems.cpp b/src/Client/Systems/ClientSystems.cpp index 6db152ad..21e89c3b 100644 --- a/src/Client/Systems/ClientSystems.cpp +++ b/src/Client/Systems/ClientSystems.cpp @@ -7,14 +7,13 @@ #include "ClientSystems.hpp" - namespace Systems { - const std::array>, 3> getSystemsGroups() + std::array>, 3> + getSystemsGroups() { return { getECSSystems(), EventsSystems::getEventSystems(), - GraphicSystems::getGraphicsSystems() - }; + GraphicSystems::getGraphicsSystems()}; } -} +} // namespace Systems diff --git a/src/Client/Systems/ClientSystems.hpp b/src/Client/Systems/ClientSystems.hpp index 2ee87afd..dd79b9e0 100644 --- a/src/Client/Systems/ClientSystems.hpp +++ b/src/Client/Systems/ClientSystems.hpp @@ -8,10 +8,11 @@ #pragma once #include -#include "Systems.hpp" -#include "GraphicSystems.hpp" #include "EventsSystems.hpp" +#include "GraphicSystems.hpp" +#include "Systems.hpp" namespace Systems { - const std::array>, 3> getSystemsGroups(); + std::array>, 3> + getSystemsGroups(); } // namespace Systems diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index a841a735..776e9e6b 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -6,10 +6,8 @@ */ #include "EventsSystems.hpp" -#include "Registry.hpp" #include "CustomTypes.hpp" - -#include "SceneManager.hpp" +#include "Registry.hpp" namespace Systems { void EventsSystems::playerMovement(std::size_t /*unused*/) @@ -33,13 +31,9 @@ namespace Systems { positionIt->value().x -= 1; } if (IsKeyDown(KEY_UP)) { - std::cout << "UP" << std::endl; - SceneManager::getInstance().changeScene(Scene::MENU); positionIt->value().y -= 1; } if (IsKeyDown(KEY_DOWN)) { - std::cout << "DOWN" << std::endl; - SceneManager::getInstance().changeScene(Scene::MAIN_GAME); positionIt->value().y += 1; } } @@ -47,10 +41,9 @@ namespace Systems { playerIt++; } } - const std::vector> EventsSystems::getEventSystems() + std::vector> + EventsSystems::getEventSystems() { - return { - playerMovement - }; + return {playerMovement}; } -} +} // namespace Systems diff --git a/src/Client/Systems/Events/EventsSystems.hpp b/src/Client/Systems/Events/EventsSystems.hpp index 0cb5fe57..457731ac 100644 --- a/src/Client/Systems/Events/EventsSystems.hpp +++ b/src/Client/Systems/Events/EventsSystems.hpp @@ -8,12 +8,12 @@ #pragma once #include -#include #include +#include namespace Systems { namespace EventsSystems { void playerMovement(std::size_t /*unused*/); - const std::vector> getEventSystems(); + std::vector> getEventSystems(); } // namespace EventsSystems -} +} // namespace Systems diff --git a/src/Client/Systems/Graphic/GraphicSystems.cpp b/src/Client/Systems/Graphic/GraphicSystems.cpp index f54c7fe6..dabc83ee 100644 --- a/src/Client/Systems/Graphic/GraphicSystems.cpp +++ b/src/Client/Systems/Graphic/GraphicSystems.cpp @@ -5,8 +5,8 @@ ** GraphicSystems */ -#include "raylib.h" #include "GraphicSystems.hpp" +#include "raylib.h" #include "CustomTypes.hpp" #include "Registry.hpp" @@ -206,14 +206,14 @@ namespace Systems { textIt++; } } - const std::vector> GraphicSystems::getGraphicsSystems() + std::vector> + GraphicSystems::getGraphicsSystems() { return { rectRenderer, spriteRenderer, textRenderer, musicPlayer, - soundEffectPlayer - }; + soundEffectPlayer}; } } // namespace Systems diff --git a/src/Client/Systems/Graphic/GraphicSystems.hpp b/src/Client/Systems/Graphic/GraphicSystems.hpp index 7240b7ce..3de515c8 100644 --- a/src/Client/Systems/Graphic/GraphicSystems.hpp +++ b/src/Client/Systems/Graphic/GraphicSystems.hpp @@ -8,8 +8,8 @@ #pragma once #include -#include #include +#include namespace Systems { namespace GraphicSystems { @@ -18,6 +18,6 @@ namespace Systems { void textRenderer(std::size_t /*unused*/); void soundEffectPlayer(std::size_t /*unused*/); void musicPlayer(std::size_t /*unused*/); - const std::vector> getGraphicsSystems(); + std::vector> getGraphicsSystems(); } // namespace GraphicSystems -} +} // namespace Systems diff --git a/src/ECS/Systems/Managers/SystemManager.cpp b/src/ECS/Systems/Managers/SystemManager.cpp index 5b02d75b..df21c3b4 100644 --- a/src/ECS/Systems/Managers/SystemManager.cpp +++ b/src/ECS/Systems/Managers/SystemManager.cpp @@ -11,7 +11,8 @@ namespace Systems { SystemManager::SystemManager( std::vector> systems) : _systems(std::move(systems)) - {} + { + } void SystemManager::updateSystems() { diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index 73b96837..425bb3a0 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -7,7 +7,6 @@ #include "Systems.hpp" #include -#include #include "CustomTypes.hpp" #include "Registry.hpp" @@ -53,9 +52,8 @@ namespace Systems { } } - const std::vector> getECSSystems() { - return { - windowCollision - }; + std::vector> getECSSystems() + { + return {windowCollision}; } } // namespace Systems diff --git a/src/ECS/Systems/Systems.hpp b/src/ECS/Systems/Systems.hpp index 264f4c3d..07b8ff5c 100644 --- a/src/ECS/Systems/Systems.hpp +++ b/src/ECS/Systems/Systems.hpp @@ -13,5 +13,5 @@ namespace Systems { void windowCollision(std::size_t); - const std::vector> getECSSystems(); + std::vector> getECSSystems(); } // namespace Systems From 666f2fbe7c057b683fdc594419e415936431f6c7 Mon Sep 17 00:00:00 2001 From: Github Actions Date: Wed, 27 Sep 2023 16:48:07 +0000 Subject: [PATCH 05/14] FORMAT-AUTO: automatic format on pull request #28 --- src/Client/SceneManager.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index 46e0d344..74eabac4 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -33,7 +33,7 @@ class SceneManager { bool _stop; const std::array, 2> _scenes = { std::vector {EVENTS, GAME, DISPLAY}, - std::vector {EVENTS, DISPLAY} + std::vector {EVENTS, DISPLAY } }; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) From 417a5def9c6c64c047cf568b6ad005da7f8092b1 Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Wed, 27 Sep 2023 18:54:40 +0200 Subject: [PATCH 06/14] ECS: Fix missing attribute reset in Registry::clear PATCH --- src/Client/SceneManager.hpp | 2 -- src/ECS/Registry.cpp | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index 46e0d344..13f827cd 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -13,8 +13,6 @@ #include #include -#include - enum Scene { MAIN_GAME, MENU, SCENE_MAX }; enum SystemManagers { GAME, EVENTS, DISPLAY, MANAGER_MAX }; diff --git a/src/ECS/Registry.cpp b/src/ECS/Registry.cpp index 8d31b10b..faa078bd 100644 --- a/src/ECS/Registry.cpp +++ b/src/ECS/Registry.cpp @@ -36,6 +36,7 @@ void Registry::clear() _data.clear(); _addComponentPlaceFunctions.clear(); _removeComponentFunctions.clear(); + _entitiesNb = 0; } Registry::Registry() : _entitiesNb(0) From 0d82b2a37783c600aa51921052014d4f51df9e6a Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Wed, 27 Sep 2023 18:56:28 +0200 Subject: [PATCH 07/14] ECS: Format PATCH --- src/Client/SceneManager.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index 64b4f290..13f827cd 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -31,7 +31,7 @@ class SceneManager { bool _stop; const std::array, 2> _scenes = { std::vector {EVENTS, GAME, DISPLAY}, - std::vector {EVENTS, DISPLAY } + std::vector {EVENTS, DISPLAY} }; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) From d24d67683067af02a46326a629c546d2bc3d33db Mon Sep 17 00:00:00 2001 From: Github Actions Date: Wed, 27 Sep 2023 16:56:30 +0000 Subject: [PATCH 08/14] FORMAT-AUTO: automatic format on pull request #28 --- src/Client/SceneManager.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index 13f827cd..64b4f290 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -31,7 +31,7 @@ class SceneManager { bool _stop; const std::array, 2> _scenes = { std::vector {EVENTS, GAME, DISPLAY}, - std::vector {EVENTS, DISPLAY} + std::vector {EVENTS, DISPLAY } }; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) From 126d3ff31877e8f1de6615ea6a10c197c866c3ea Mon Sep 17 00:00:00 2001 From: guillaume abel <91884836+guillaumeAbel@users.noreply.github.com> Date: Wed, 27 Sep 2023 19:03:45 +0200 Subject: [PATCH 09/14] Update src/Client/SceneManager.cpp Co-authored-by: Xavier Mitault --- src/Client/SceneManager.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index b811278d..20498090 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -38,8 +38,7 @@ static void initRaylib() InitWindow( screenWidth, screenHeight, - "raylib [textures] examples - texture source and destination " - "rectangles"); + "R-Bus"); SetTargetFPS(fps); InitAudioDevice(); } From 941a096f65ac52bfb27c7ad23c2ad135803f7f16 Mon Sep 17 00:00:00 2001 From: Github Actions Date: Wed, 27 Sep 2023 17:04:28 +0000 Subject: [PATCH 10/14] FORMAT-AUTO: automatic format on pull request #28 --- src/Client/SceneManager.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index 20498090..c4c3d036 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -35,10 +35,7 @@ SceneManager::SceneManager() : _currentScene(Scene::MAIN_GAME), _stop(false) static void initRaylib() { - InitWindow( - screenWidth, - screenHeight, - "R-Bus"); + InitWindow(screenWidth, screenHeight, "R-Bus"); SetTargetFPS(fps); InitAudioDevice(); } From 4ee12930b1742adfe5e2b2dcba52f27e1ede9b9b Mon Sep 17 00:00:00 2001 From: guillaume abel Date: Wed, 27 Sep 2023 20:49:43 +0200 Subject: [PATCH 11/14] ECS: Add run method + throw handle and print MINOR --- src/Client/SceneManager.cpp | 62 ++++++++++++++++++++++--------------- src/Client/SceneManager.hpp | 4 ++- src/main_client.cpp | 6 +++- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index b811278d..d3380a02 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -20,11 +20,32 @@ bool SceneManager::_init = false; SceneManager SceneManager::_instance = SceneManager(); // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) +static void initRaylib() +{ + InitWindow( + screenWidth, + screenHeight, + "raylib [textures] examples - texture source and destination " + "rectangles"); + SetTargetFPS(fps); + InitAudioDevice(); +} + +static void initSystemManagers() +{ + auto &director = Systems::SystemManagersDirector::getInstance(); + + for (auto systems : Systems::getSystemsGroups()) { + director.addSystemManager(systems); + } + initRaylib(); +} + SceneManager &SceneManager::getInstance() { if (!_init) { _init = true; - _instance.init(); + initSystemManagers(); } return _instance; } @@ -33,41 +54,32 @@ SceneManager::SceneManager() : _currentScene(Scene::MAIN_GAME), _stop(false) { } -static void initRaylib() -{ - InitWindow( - screenWidth, - screenHeight, - "raylib [textures] examples - texture source and destination " - "rectangles"); - SetTargetFPS(fps); - InitAudioDevice(); -} - static void destroyRaylib() { CloseAudioDevice(); CloseWindow(); } -void SceneManager::init() +int SceneManager::run() { auto &director = Systems::SystemManagersDirector::getInstance(); - for (auto systems : Systems::getSystemsGroups()) { - director.addSystemManager(systems); - } - initRaylib(); - while (!_stop && !WindowShouldClose()) { - BeginDrawing(); - ClearBackground(RAYWHITE); - auto scene = _scenes.at(_currentScene); - for (auto &systemManager : scene) { - director.getSystemManager(systemManager).updateSystems(); + try { + while (!_stop && !WindowShouldClose()) { + BeginDrawing(); + ClearBackground(RAYWHITE); + auto scene = _scenes.at(_currentScene); + for (auto &systemManager : scene) { + director.getSystemManager(systemManager).updateSystems(); + } + EndDrawing(); } - EndDrawing(); + destroyRaylib(); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + return ReturnValue::ERROR; } - destroyRaylib(); + return ReturnValue::OK; } void SceneManager::changeScene(Scene scene) diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index 13f827cd..d99328be 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -13,6 +13,8 @@ #include #include +enum ReturnValue { OK = 0, ERROR = 84 }; + enum Scene { MAIN_GAME, MENU, SCENE_MAX }; enum SystemManagers { GAME, EVENTS, DISPLAY, MANAGER_MAX }; @@ -20,12 +22,12 @@ enum SystemManagers { GAME, EVENTS, DISPLAY, MANAGER_MAX }; class SceneManager { public: static SceneManager &getInstance(); + int run(); void changeScene(Scene scene); void stop(); private: SceneManager(); - void init(); Scene _currentScene; bool _stop; diff --git a/src/main_client.cpp b/src/main_client.cpp index 7d1aebe3..161985de 100644 --- a/src/main_client.cpp +++ b/src/main_client.cpp @@ -9,5 +9,9 @@ int main() { - SceneManager::getInstance(); + SceneManager &sceneManager = SceneManager::getInstance(); + + int res = sceneManager.run(); + + return res; } From 8c4c6ae28c8c3c900a055741fc9223bd5dc11406 Mon Sep 17 00:00:00 2001 From: Kitetsu Date: Wed, 27 Sep 2023 21:45:29 +0200 Subject: [PATCH 12/14] ECS: Add custom sparse array to stock the ids MINOR --- src/ECS/Registry.cpp | 3 +++ src/ECS/Registry.hpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/ECS/Registry.cpp b/src/ECS/Registry.cpp index faa078bd..3ded85ed 100644 --- a/src/ECS/Registry.cpp +++ b/src/ECS/Registry.cpp @@ -41,4 +41,7 @@ void Registry::clear() Registry::Registry() : _entitiesNb(0) { + for (std::size_t i = 0; i < MAX; i++) { + addCustomSparseArray(); + } } diff --git a/src/ECS/Registry.hpp b/src/ECS/Registry.hpp index 15bdfd75..15cc1d78 100644 --- a/src/ECS/Registry.hpp +++ b/src/ECS/Registry.hpp @@ -14,10 +14,18 @@ #include #include #include +#include #include #include "raylib.h" #include "SparseArray.hpp" +enum CustomIndex { + PLAYER, + BULLET, + ENNEMY, + MAX +}; + class Registry { public: template @@ -38,6 +46,23 @@ class Registry { void clear(); + template + components getCustomSparseArray(std::size_t id) + { + + if (id > _customSparseArrays.size()) { + throw std::runtime_error("ID not in "); + } + try { + components castedComponent = std::any_cast>( + _customSparseArrays[id]); + + return castedComponent; + } catch (const std::bad_any_cast& e) { + throw std::runtime_error("Bad any cast"); + } + } + Registry &operator=(const Registry &) = delete; Registry(const Registry &) = delete; void operator=(const Registry &&) = delete; @@ -46,6 +71,15 @@ class Registry { private: Registry(); + template + std::size_t addCustomSparseArray() + { + std::size_t id = _customSparseArrays.size(); + + _customSparseArrays.push_back(SparseArray()); + return (id); + } + template void checkAddSparseArray() { @@ -90,5 +124,6 @@ class Registry { std::vector> _removeComponentFunctions; std::unordered_map _data; + std::vector _customSparseArrays; std::size_t _entitiesNb; }; From f4e776d8ded615ae7c155af98addb7ad3e7834b2 Mon Sep 17 00:00:00 2001 From: Kitetsu Date: Wed, 27 Sep 2023 22:34:43 +0200 Subject: [PATCH 13/14] CLIENT-GAME/ECS : Add final implementation of custom sparse array MAJOR --- src/Client/SceneManager.cpp | 22 +++++++++++ src/Client/SceneManager.hpp | 2 +- src/Client/Systems/Events/EventsSystems.cpp | 23 +++++------ src/ECS/ECSCustomTypes.hpp | 4 -- src/ECS/Registry.cpp | 7 +++- src/ECS/Registry.hpp | 19 ++++------ src/ECS/Systems/Systems.cpp | 42 ++++++++++----------- 7 files changed, 65 insertions(+), 54 deletions(-) diff --git a/src/Client/SceneManager.cpp b/src/Client/SceneManager.cpp index 2eef4f5d..47aa8647 100644 --- a/src/Client/SceneManager.cpp +++ b/src/Client/SceneManager.cpp @@ -11,10 +11,16 @@ #include "Registry.hpp" #include "SystemManagersDirector.hpp" +// to suppr +#include "CustomTypes.hpp" + constexpr int screenWidth = 1920; constexpr int screenHeight = 1080; constexpr int fps = 60; +// to suppr +constexpr int playerData = 10; + // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) bool SceneManager::_init = false; SceneManager SceneManager::_instance = SceneManager(); @@ -25,6 +31,22 @@ static void initRaylib() InitWindow(screenWidth, screenHeight, "R-Bus"); SetTargetFPS(fps); InitAudioDevice(); + Registry::getInstance().addEntity(); + Registry::getInstance().getComponents().back() = { + playerData, + playerData}; + Registry::getInstance().getComponents().back() = { + playerData, + playerData}; + Registry::getInstance().getComponents().back() = { + playerData, + playerData}; + SparseArray &playerId = + Registry::getInstance().getCustomSparseArray( + CustomIndex::PLAYER); + playerId.add(); + playerId.back() = + std::optional(Registry::getInstance().getEntitiesNb() - 1); } static void initSystemManagers() diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index 44a6978a..d99328be 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -33,7 +33,7 @@ class SceneManager { bool _stop; const std::array, 2> _scenes = { std::vector {EVENTS, GAME, DISPLAY}, - std::vector {EVENTS, DISPLAY } + std::vector {EVENTS, DISPLAY} }; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index 776e9e6b..481f4a2d 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -15,30 +15,25 @@ namespace Systems { Registry::components arrPosition = Registry::getInstance().getComponents(); - Registry::components arrPlayer = - Registry::getInstance().getComponents(); + SparseArray &playerId = + Registry::getInstance().getCustomSparseArray( + CustomIndex::PLAYER); - auto positionIt = arrPosition.begin(); - auto playerIt = arrPlayer.begin(); - - while (positionIt != arrPosition.end() && playerIt != arrPlayer.end()) { - if (playerIt->has_value() && positionIt->has_value() - && playerIt->value().isMine) { + for (std::optional id : playerId) { + if (id.has_value() && arrPosition[id.value()].has_value()) { if (IsKeyDown(KEY_RIGHT)) { - positionIt->value().x += 1; + arrPosition[id.value()].value().x += 1; } if (IsKeyDown(KEY_LEFT)) { - positionIt->value().x -= 1; + arrPosition[id.value()].value().x -= 1; } if (IsKeyDown(KEY_UP)) { - positionIt->value().y -= 1; + arrPosition[id.value()].value().y -= 1; } if (IsKeyDown(KEY_DOWN)) { - positionIt->value().y += 1; + arrPosition[id.value()].value().y += 1; } } - positionIt++; - playerIt++; } } std::vector> diff --git a/src/ECS/ECSCustomTypes.hpp b/src/ECS/ECSCustomTypes.hpp index d5b8e86d..7c15c4a5 100644 --- a/src/ECS/ECSCustomTypes.hpp +++ b/src/ECS/ECSCustomTypes.hpp @@ -26,8 +26,4 @@ namespace Types { float y; }; - struct Player { - bool isMine; - }; - } // namespace Types diff --git a/src/ECS/Registry.cpp b/src/ECS/Registry.cpp index 3ded85ed..6c381858 100644 --- a/src/ECS/Registry.cpp +++ b/src/ECS/Registry.cpp @@ -39,9 +39,14 @@ void Registry::clear() _entitiesNb = 0; } +std::size_t Registry::getEntitiesNb() +{ + return (_entitiesNb); +} + Registry::Registry() : _entitiesNb(0) { for (std::size_t i = 0; i < MAX; i++) { - addCustomSparseArray(); + addCustomSparseArray(); } } diff --git a/src/ECS/Registry.hpp b/src/ECS/Registry.hpp index 15cc1d78..e5310062 100644 --- a/src/ECS/Registry.hpp +++ b/src/ECS/Registry.hpp @@ -11,20 +11,15 @@ #include #include #include +#include #include #include #include -#include #include #include "raylib.h" #include "SparseArray.hpp" -enum CustomIndex { - PLAYER, - BULLET, - ENNEMY, - MAX -}; +enum CustomIndex { PLAYER, BULLET, ENNEMY, MAX }; class Registry { public: @@ -49,20 +44,22 @@ class Registry { template components getCustomSparseArray(std::size_t id) { - if (id > _customSparseArrays.size()) { throw std::runtime_error("ID not in "); } try { - components castedComponent = std::any_cast>( - _customSparseArrays[id]); + components castedComponent = + std::any_cast>( + _customSparseArrays[id]); return castedComponent; - } catch (const std::bad_any_cast& e) { + } catch (const std::bad_any_cast &e) { throw std::runtime_error("Bad any cast"); } } + std::size_t getEntitiesNb(); + Registry &operator=(const Registry &) = delete; Registry(const Registry &) = delete; void operator=(const Registry &&) = delete; diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index 425bb3a0..77898526 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -17,38 +17,34 @@ namespace Systems { Registry::getInstance().getComponents(); Registry::components arrCollisionRect = Registry::getInstance().getComponents(); - Registry::components arrPlayer = - Registry::getInstance().getComponents(); - const float maxPercent = 100.0F; - auto positionIt = arrPosition.begin(); - auto collisionIt = arrCollisionRect.begin(); - auto playerIt = arrPlayer.begin(); + SparseArray &playerId = + Registry::getInstance().getCustomSparseArray( + CustomIndex::PLAYER); - while (playerIt != arrPlayer.end() && positionIt != arrPosition.end() - && collisionIt != arrCollisionRect.end()) { - if (playerIt->has_value() && positionIt->has_value() - && collisionIt->has_value()) { - if (positionIt->value().x < 0) { - positionIt->value().x = 0; + const float maxPercent = 100.0F; + for (std::optional id : playerId) { + if (id.has_value() && arrPosition[id.value()].has_value() + && arrCollisionRect[id.value()].has_value()) { + if (arrPosition[id.value()].value().x < 0) { + arrPosition[id.value()].value().x = 0; } - if (positionIt->value().y < 0) { - positionIt->value().y = 0; + if (arrPosition[id.value()].value().y < 0) { + arrPosition[id.value()].value().y = 0; } - if (positionIt->value().x + collisionIt->value().width + if (arrPosition[id.value()].value().x + + arrCollisionRect[id.value()].value().width > maxPercent) { - positionIt->value().x = - maxPercent - collisionIt->value().width; + arrPosition[id.value()].value().x = + maxPercent - arrCollisionRect[id.value()].value().width; } - if (positionIt->value().y + collisionIt->value().height + if (arrPosition[id.value()].value().y + + arrCollisionRect[id.value()].value().height > maxPercent) { - positionIt->value().y = - maxPercent - collisionIt->value().height; + arrPosition[id.value()].value().y = maxPercent + - arrCollisionRect[id.value()].value().height; } } - positionIt++; - collisionIt++; - playerIt++; } } From 2dc0562f9a6dc1f8dae7e22774f462aeaf90f73e Mon Sep 17 00:00:00 2001 From: Github Actions Date: Wed, 27 Sep 2023 20:38:16 +0000 Subject: [PATCH 14/14] FORMAT-AUTO: automatic format on pull request #29 --- src/Client/SceneManager.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client/SceneManager.hpp b/src/Client/SceneManager.hpp index d99328be..44a6978a 100644 --- a/src/Client/SceneManager.hpp +++ b/src/Client/SceneManager.hpp @@ -33,7 +33,7 @@ class SceneManager { bool _stop; const std::array, 2> _scenes = { std::vector {EVENTS, GAME, DISPLAY}, - std::vector {EVENTS, DISPLAY} + std::vector {EVENTS, DISPLAY } }; // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)