From 271acbdcc1d8adabcdeac56edee7f74e3033e96b Mon Sep 17 00:00:00 2001 From: milerius Date: Wed, 11 Sep 2019 21:07:31 +0200 Subject: [PATCH] feat(ecs): working on ecs module --- modules/ecs/CMakeLists.txt | 5 +- .../ecs/antara.ecs.system.manager.tests.cpp | 55 ++++++++++++ .../ecs/antara/gaming/ecs/system.manager.cpp | 44 +++++++++ .../ecs/antara/gaming/ecs/system.manager.hpp | 89 +++++++++++++++++++ 4 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 modules/ecs/antara/gaming/ecs/antara.ecs.system.manager.tests.cpp create mode 100644 modules/ecs/antara/gaming/ecs/system.manager.cpp create mode 100644 modules/ecs/antara/gaming/ecs/system.manager.hpp diff --git a/modules/ecs/CMakeLists.txt b/modules/ecs/CMakeLists.txt index 19569044..1f8eab62 100644 --- a/modules/ecs/CMakeLists.txt +++ b/modules/ecs/CMakeLists.txt @@ -1,6 +1,6 @@ ## shared sources between the module and his unit tests add_library(antara_ecs_shared_sources INTERFACE) -target_sources(antara_ecs_shared_sources INTERFACE antara/gaming/ecs/base.system.cpp) +target_sources(antara_ecs_shared_sources INTERFACE antara/gaming/ecs/base.system.cpp antara/gaming/ecs/system.manager.cpp) target_include_directories(antara_ecs_shared_sources INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(antara_ecs_shared_sources INTERFACE EnTT strong_type) add_library(antara::ecs_shared_sources ALIAS antara_ecs_shared_sources) @@ -15,7 +15,8 @@ add_library(antara::ecs ALIAS antara_ecs) add_executable(antara_ecs_tests) target_sources(antara_ecs_tests PUBLIC antara/gaming/ecs/antara.ecs.tests.cpp - antara/gaming/ecs/antara.ecs.system.tests.cpp) + antara/gaming/ecs/antara.ecs.system.tests.cpp + antara/gaming/ecs/antara.ecs.system.manager.tests.cpp) target_link_libraries(antara_ecs_tests PRIVATE doctest PUBLIC antara::ecs_shared_sources) set_target_properties(antara_ecs_tests PROPERTIES diff --git a/modules/ecs/antara/gaming/ecs/antara.ecs.system.manager.tests.cpp b/modules/ecs/antara/gaming/ecs/antara.ecs.system.manager.tests.cpp new file mode 100644 index 00000000..a1e5c5ee --- /dev/null +++ b/modules/ecs/antara/gaming/ecs/antara.ecs.system.manager.tests.cpp @@ -0,0 +1,55 @@ +/****************************************************************************** + * Copyright © 2013-2019 The Komodo Platform Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * Komodo Platform software, including this file may be copied, modified, * + * propagated or distributed except according to the terms contained in the * + * LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include +#include "antara/gaming/ecs/system.hpp" +#include "antara/gaming/ecs/system.manager.hpp" + +class logic_concrete_system : public antara::gaming::ecs::logic_update_system +{ +public: + logic_concrete_system(entt::registry ®istry, entt::dispatcher &dispatcher) : system(registry, dispatcher) + { + + } + + logic_concrete_system() = default; + + void update() noexcept final + { + + } + + ~logic_concrete_system() noexcept final = default; +}; + +namespace antara::gaming::ecs::tests +{ + TEST_SUITE ("system manager test suite") + { + entt::registry registry; + entt::dispatcher dispatcher; + system_manager manager{registry, dispatcher}; + + TEST_CASE ("add system") + { + CHECK_EQ(manager.nb_systems(), 0u); + manager.create_system(); + CHECK_EQ(manager.nb_systems(), 1u); + CHECK_EQ(manager.nb_systems(logic_concrete_system::get_system_type()), 1u); + } + } +} \ No newline at end of file diff --git a/modules/ecs/antara/gaming/ecs/system.manager.cpp b/modules/ecs/antara/gaming/ecs/system.manager.cpp new file mode 100644 index 00000000..3189741c --- /dev/null +++ b/modules/ecs/antara/gaming/ecs/system.manager.cpp @@ -0,0 +1,44 @@ +/****************************************************************************** + * Copyright © 2013-2019 The Komodo Platform Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * Komodo Platform software, including this file may be copied, modified, * + * propagated or distributed except according to the terms contained in the * + * LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "antara/gaming/ecs/system.manager.hpp" + +namespace antara::gaming::ecs +{ + system_manager::system_manager(entt::registry ®istry, entt::dispatcher &dispatcher) noexcept : entity_registry_( + registry), dispatcher_(dispatcher) + { + + } + + std::size_t system_manager::nb_systems(system_type sys_type) const noexcept + { + return systems_[sys_type].size(); + } + + std::size_t system_manager::nb_systems() const noexcept + { + return std::accumulate(begin(systems_), end(systems_), static_cast(0u), + [](size_t accumulator, auto &&vec) { + return accumulator + vec.size(); + }); + } + + base_system &system_manager::add_system_(system_manager::system_ptr &&system, system_type sys_type) noexcept + { + return *systems_[sys_type].emplace_back(std::move(system)); + } +} \ No newline at end of file diff --git a/modules/ecs/antara/gaming/ecs/system.manager.hpp b/modules/ecs/antara/gaming/ecs/system.manager.hpp new file mode 100644 index 00000000..c0c24a56 --- /dev/null +++ b/modules/ecs/antara/gaming/ecs/system.manager.hpp @@ -0,0 +1,89 @@ +/****************************************************************************** + * Copyright © 2013-2019 The Komodo Platform Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * Komodo Platform software, including this file may be copied, modified, * + * propagated or distributed except according to the terms contained in the * + * LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#pragma once + +#include +#include +#include +#include "antara/gaming/ecs/base.system.hpp" + +namespace antara::gaming::ecs +{ + class system_manager + { + public: + //! Public typedefs + using system_ptr = std::unique_ptr; + using system_array = std::vector; + using system_registry = std::array; + + //! Constructors + + /** + * \note Basic constructor + * \param dispatcher The dispatcher is provided to the system when it is created. + * \param registry The entity_registry is provided to the system when it is created. + */ + explicit system_manager(entt::registry ®istry, entt::dispatcher &dispatcher) noexcept; + + /** + * \return number of systems + */ + [[nodiscard]] std::size_t nb_systems() const noexcept; + + /** + * \param sys_type represent the type of systems. + * \return number of systems of a specific type. + */ + [[nodiscard]] std::size_t nb_systems(system_type sys_type) const noexcept; + + /** + * \note This function allow you to create a system with the given argument + * \note This function is a factory + * \tparam TSystem represents the type of system to create + * \tparam TSystemArgs represents the arguments needed to construct the system to create + * \return Returns a reference to the created system + */ + template + TSystem &create_system(TSystemArgs &&...args) noexcept; + + private: + //! Private member functions + base_system &add_system_(system_ptr &&system, system_type sys_type) noexcept; + + //! Private data members + entt::registry &entity_registry_; + entt::dispatcher &dispatcher_; + system_registry systems_{{}}; + }; +} + +//! Implementation +namespace antara::gaming::ecs +{ + template + TSystem &system_manager::create_system(TSystemArgs &&... args) noexcept + { + auto creator = [this](auto &&... args_) { + return std::make_unique(this->entity_registry_, + this->dispatcher_, + std::forward(args_)...); + }; + system_ptr sys = creator(std::forward(args)...); + return static_cast(add_system_(std::move(sys), TSystem::get_system_type())); + } +} \ No newline at end of file