Skip to content

Commit

Permalink
feat(ecs): working on ecs module
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed Sep 11, 2019
1 parent 655dead commit 271acbd
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 2 deletions.
5 changes: 3 additions & 2 deletions modules/ecs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
55 changes: 55 additions & 0 deletions modules/ecs/antara/gaming/ecs/antara.ecs.system.manager.tests.cpp
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 &registry, 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);
}
}
}
44 changes: 44 additions & 0 deletions modules/ecs/antara/gaming/ecs/system.manager.cpp
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 &registry, 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));
}
}
89 changes: 89 additions & 0 deletions modules/ecs/antara/gaming/ecs/system.manager.hpp
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 &registry, 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()));
}
}

0 comments on commit 271acbd

Please sign in to comment.