dummy systems

This commit is contained in:
2018-12-26 18:36:41 +07:00
parent 7460832dc6
commit f77becd269
2 changed files with 73 additions and 6 deletions

44
ecs.hpp
View File

@@ -13,6 +13,7 @@
#include <tuple>
#include <mutex>
#include <memory>
#include <vector>
#include <limits>
#include <utility>
#include <exception>
@@ -31,6 +32,7 @@
namespace ecs_hpp
{
class entity;
class system;
class registry;
using family_id = std::uint16_t;
@@ -275,6 +277,21 @@ namespace std
};
}
// -----------------------------------------------------------------------------
//
// system
//
// -----------------------------------------------------------------------------
namespace ecs_hpp
{
class system {
public:
virtual ~system() = default;
virtual void process(registry& owner) = 0;
};
}
// -----------------------------------------------------------------------------
//
// registry
@@ -285,8 +302,8 @@ namespace ecs_hpp
{
class registry final {
public:
registry();
~registry() noexcept;
registry() = default;
~registry() noexcept = default;
entity create_entity();
bool destroy_entity(const entity& ent);
@@ -332,6 +349,10 @@ namespace ecs_hpp
void for_joined_components(F&& f);
template < typename... Ts, typename F >
void for_joined_components(F&& f) const;
template < typename T, typename... Args >
void add_system(Args&&... args);
void process_systems();
private:
template < typename T >
detail::component_storage<T>* find_storage_() noexcept;
@@ -370,6 +391,9 @@ namespace ecs_hpp
using storage_uptr = std::unique_ptr<detail::component_storage_base>;
std::unordered_map<family_id, storage_uptr> storages_;
using system_uptr = std::unique_ptr<system>;
std::vector<system_uptr> systems_;
};
}
@@ -483,9 +507,6 @@ namespace ecs_hpp
namespace ecs_hpp
{
inline registry::registry() = default;
inline registry::~registry() noexcept = default;
inline entity registry::create_entity() {
std::lock_guard<std::mutex> guard(mutex_);
assert(last_entity_id_ < std::numeric_limits<entity_id>::max());
@@ -628,6 +649,19 @@ namespace ecs_hpp
for_joined_components_impl_<Ts...>(std::forward<F>(f));
}
template < typename T, typename... Args >
void registry::add_system(Args&&... args) {
std::lock_guard<std::mutex> guard(mutex_);
systems_.emplace_back(
std::make_unique<T>(std::forward<Args>(args)...));
}
inline void registry::process_systems() {
for ( auto& s : systems_ ) {
s->process(*this);
}
}
template < typename T >
detail::component_storage<T>* registry::find_storage_() noexcept {
const auto family = detail::type_family<T>::id();

View File

@@ -318,7 +318,6 @@ TEST_CASE("registry") {
}
}
}
SECTION("for_joined_components") {
{
ecs::registry w;
@@ -365,4 +364,38 @@ TEST_CASE("registry") {
}
}
}
SECTION("systems") {
{
class movement_system : public ecs::system {
public:
void process(ecs::registry& owner) override {
owner.for_joined_components<position_c, velocity_c>([](
ecs::entity e, position_c& p, const velocity_c& v)
{
p.x += v.x;
p.y += v.y;
});
}
};
ecs::registry w;
w.add_system<movement_system>();
auto e1 = w.create_entity();
auto e2 = w.create_entity();
e1.assign_component<position_c>(1, 2);
e1.assign_component<velocity_c>(3, 4);
e2.assign_component<position_c>(5, 6);
e2.assign_component<velocity_c>(7, 8);
w.process_systems();
REQUIRE(e1.get_component<position_c>().x == 1 + 3);
REQUIRE(e1.get_component<position_c>().y == 2 + 4);
REQUIRE(e2.get_component<position_c>().x == 5 + 7);
REQUIRE(e2.get_component<position_c>().y == 6 + 8);
}
}
}