mirror of
https://github.com/BlackMATov/ecs.hpp.git
synced 2025-12-16 22:19:21 +07:00
dummy systems
This commit is contained in:
44
ecs.hpp
44
ecs.hpp
@@ -13,6 +13,7 @@
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
@@ -31,6 +32,7 @@
|
|||||||
namespace ecs_hpp
|
namespace ecs_hpp
|
||||||
{
|
{
|
||||||
class entity;
|
class entity;
|
||||||
|
class system;
|
||||||
class registry;
|
class registry;
|
||||||
|
|
||||||
using family_id = std::uint16_t;
|
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
|
// registry
|
||||||
@@ -285,8 +302,8 @@ namespace ecs_hpp
|
|||||||
{
|
{
|
||||||
class registry final {
|
class registry final {
|
||||||
public:
|
public:
|
||||||
registry();
|
registry() = default;
|
||||||
~registry() noexcept;
|
~registry() noexcept = default;
|
||||||
|
|
||||||
entity create_entity();
|
entity create_entity();
|
||||||
bool destroy_entity(const entity& ent);
|
bool destroy_entity(const entity& ent);
|
||||||
@@ -332,6 +349,10 @@ namespace ecs_hpp
|
|||||||
void for_joined_components(F&& f);
|
void for_joined_components(F&& f);
|
||||||
template < typename... Ts, typename F >
|
template < typename... Ts, typename F >
|
||||||
void for_joined_components(F&& f) const;
|
void for_joined_components(F&& f) const;
|
||||||
|
|
||||||
|
template < typename T, typename... Args >
|
||||||
|
void add_system(Args&&... args);
|
||||||
|
void process_systems();
|
||||||
private:
|
private:
|
||||||
template < typename T >
|
template < typename T >
|
||||||
detail::component_storage<T>* find_storage_() noexcept;
|
detail::component_storage<T>* find_storage_() noexcept;
|
||||||
@@ -370,6 +391,9 @@ namespace ecs_hpp
|
|||||||
|
|
||||||
using storage_uptr = std::unique_ptr<detail::component_storage_base>;
|
using storage_uptr = std::unique_ptr<detail::component_storage_base>;
|
||||||
std::unordered_map<family_id, storage_uptr> storages_;
|
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
|
namespace ecs_hpp
|
||||||
{
|
{
|
||||||
inline registry::registry() = default;
|
|
||||||
inline registry::~registry() noexcept = default;
|
|
||||||
|
|
||||||
inline entity registry::create_entity() {
|
inline entity registry::create_entity() {
|
||||||
std::lock_guard<std::mutex> guard(mutex_);
|
std::lock_guard<std::mutex> guard(mutex_);
|
||||||
assert(last_entity_id_ < std::numeric_limits<entity_id>::max());
|
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));
|
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 >
|
template < typename T >
|
||||||
detail::component_storage<T>* registry::find_storage_() noexcept {
|
detail::component_storage<T>* registry::find_storage_() noexcept {
|
||||||
const auto family = detail::type_family<T>::id();
|
const auto family = detail::type_family<T>::id();
|
||||||
|
|||||||
@@ -318,7 +318,6 @@ TEST_CASE("registry") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("for_joined_components") {
|
SECTION("for_joined_components") {
|
||||||
{
|
{
|
||||||
ecs::registry w;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user