-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
modules/ecs/antara/gaming/ecs/antara.ecs.system.manager.tests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <doctest/doctest.h> | ||
#include "antara/gaming/ecs/system.hpp" | ||
#include "antara/gaming/ecs/system.manager.hpp" | ||
|
||
class logic_concrete_system : public antara::gaming::ecs::logic_update_system<logic_concrete_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<logic_concrete_system>(); | ||
CHECK_EQ(manager.nb_systems(), 1u); | ||
CHECK_EQ(manager.nb_systems(logic_concrete_system::get_system_type()), 1u); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<size_t>(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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <memory> | ||
#include <entt/entity/entity.hpp> | ||
#include <entt/signal/dispatcher.hpp> | ||
#include "antara/gaming/ecs/base.system.hpp" | ||
|
||
namespace antara::gaming::ecs | ||
{ | ||
class system_manager | ||
{ | ||
public: | ||
//! Public typedefs | ||
using system_ptr = std::unique_ptr<base_system>; | ||
using system_array = std::vector<system_ptr>; | ||
using system_registry = std::array<system_array, system_type::size>; | ||
|
||
//! 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<typename TSystem, typename ... TSystemArgs> | ||
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<typename TSystem, typename... TSystemArgs> | ||
TSystem &system_manager::create_system(TSystemArgs &&... args) noexcept | ||
{ | ||
auto creator = [this](auto &&... args_) { | ||
return std::make_unique<TSystem>(this->entity_registry_, | ||
this->dispatcher_, | ||
std::forward<decltype(args_)>(args_)...); | ||
}; | ||
system_ptr sys = creator(std::forward<TSystemArgs>(args)...); | ||
return static_cast<TSystem &>(add_system_(std::move(sys), TSystem::get_system_type())); | ||
} | ||
} |