mirror of
https://github.com/BlackMATov/ecs.hpp.git
synced 2025-12-16 22:19:21 +07:00
96
README.md
96
README.md
@@ -41,73 +41,113 @@ target_link_libraries(your_project_target ecs.hpp)
|
|||||||
## Basic usage
|
## Basic usage
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
|
||||||
#include <ecs.hpp/ecs.hpp>
|
#include <ecs.hpp/ecs.hpp>
|
||||||
namespace ecs = ecs_hpp;
|
namespace ecs = ecs_hpp;
|
||||||
|
|
||||||
|
// events
|
||||||
|
|
||||||
|
struct update_event {
|
||||||
|
float dt{};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct render_event {
|
||||||
|
std::string camera;
|
||||||
|
};
|
||||||
|
|
||||||
|
// components
|
||||||
|
|
||||||
struct movable {};
|
struct movable {};
|
||||||
struct disabled {};
|
struct disabled {};
|
||||||
|
|
||||||
|
struct sprite {
|
||||||
|
std::string name;
|
||||||
|
};
|
||||||
|
|
||||||
struct position {
|
struct position {
|
||||||
float x{};
|
float x{};
|
||||||
float y{};
|
float y{};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct velocity {
|
struct velocity {
|
||||||
float dx{};
|
float x{};
|
||||||
float dy{};
|
float y{};
|
||||||
};
|
};
|
||||||
|
|
||||||
class movement_system : public ecs::system {
|
// systems
|
||||||
public:
|
|
||||||
void process(ecs::registry& owner) override {
|
|
||||||
owner.for_joined_components<
|
|
||||||
position,
|
|
||||||
velocity
|
|
||||||
>([](ecs::entity, position& p, const velocity& v) {
|
|
||||||
p.x += v.dx;
|
|
||||||
p.y += v.dy;
|
|
||||||
}, ecs::exists<movable>{} && !ecs::exists<disabled>{});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class gravity_system : public ecs::system {
|
class gravity_system : public ecs::system<update_event> {
|
||||||
public:
|
public:
|
||||||
gravity_system(float gravity)
|
gravity_system(float gravity)
|
||||||
: gravity_(gravity) {}
|
: gravity_(gravity) {}
|
||||||
|
|
||||||
void process(ecs::registry& owner) override {
|
void process(ecs::registry& world, const update_event& evt) override {
|
||||||
owner.for_each_component<
|
world.for_each_component<velocity>(
|
||||||
velocity
|
[this, &evt](ecs::entity, velocity& vel) {
|
||||||
>([this](ecs::entity e, velocity& v) {
|
vel.x += gravity_ * evt.dt;
|
||||||
v.dx += gravity_;
|
vel.y += gravity_ * evt.dt;
|
||||||
v.dy += gravity_;
|
}, ecs::exists<movable>{} && !ecs::exists<disabled>{});
|
||||||
}, !ecs::exists<disabled>{});
|
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
float gravity_{};
|
float gravity_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class movement_system : public ecs::system<update_event> {
|
||||||
|
public:
|
||||||
|
void process(ecs::registry& world, const update_event& evt) override {
|
||||||
|
world.for_joined_components<position, velocity>(
|
||||||
|
[&evt](ecs::entity, position& pos, const velocity& vel) {
|
||||||
|
pos.x += vel.x * evt.dt;
|
||||||
|
pos.y += vel.y * evt.dt;
|
||||||
|
}, ecs::exists<movable>{} && !ecs::exists<disabled>{});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class render_system : public ecs::system<render_event> {
|
||||||
|
public:
|
||||||
|
void process(ecs::registry& world, const render_event& evt) override {
|
||||||
|
world.for_joined_components<sprite, position>(
|
||||||
|
[&evt](ecs::entity, const sprite& s, const position& p) {
|
||||||
|
std::cout << "Render sprite:" << std::endl;
|
||||||
|
std::cout << "--> pos: " << p.x << "," << p.y << std::endl;
|
||||||
|
std::cout << "--> sprite: " << s.name << std::endl;
|
||||||
|
std::cout << "--> camera: " << evt.camera << std::endl;
|
||||||
|
}, !ecs::exists<disabled>{});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// world
|
||||||
|
|
||||||
ecs::registry world;
|
ecs::registry world;
|
||||||
|
|
||||||
ecs::registry_filler(world)
|
struct physics_feature {};
|
||||||
.system<movement_system>(0)
|
world.assign_feature<physics_feature>()
|
||||||
.system<gravity_system>(1, 9.8f);
|
.add_system<movement_system>()
|
||||||
|
.add_system<gravity_system>(9.8f);
|
||||||
|
|
||||||
|
struct rendering_feature {};
|
||||||
|
world.assign_feature<rendering_feature>()
|
||||||
|
.add_system<render_system>();
|
||||||
|
|
||||||
|
// entities
|
||||||
|
|
||||||
auto entity_one = world.create_entity();
|
auto entity_one = world.create_entity();
|
||||||
ecs::entity_filler(entity_one)
|
ecs::entity_filler(entity_one)
|
||||||
.component<movable>()
|
.component<movable>()
|
||||||
|
.component<sprite>("ship")
|
||||||
.component<position>(4.f, 2.f)
|
.component<position>(4.f, 2.f)
|
||||||
.component<velocity>(10.f, 20.f);
|
.component<velocity>(10.f, 20.f);
|
||||||
|
|
||||||
auto entity_two = world.create_entity();
|
auto entity_two = world.create_entity();
|
||||||
ecs::entity_filler(entity_two)
|
ecs::entity_filler(entity_two)
|
||||||
.component<movable>()
|
.component<movable>()
|
||||||
.component<disabled>()
|
.component<sprite>("player")
|
||||||
.component<position>(4.f, 2.f)
|
.component<position>(4.f, 2.f)
|
||||||
.component<velocity>(10.f, 20.f);
|
.component<velocity>(10.f, 20.f);
|
||||||
|
|
||||||
world.process_all_systems();
|
// processing
|
||||||
|
|
||||||
|
world.process_event(update_event{0.1f});
|
||||||
|
world.process_event(render_event{"main"});
|
||||||
```
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|||||||
@@ -39,7 +39,14 @@ namespace ecs_hpp
|
|||||||
|
|
||||||
class prototype;
|
class prototype;
|
||||||
|
|
||||||
|
template < typename E >
|
||||||
|
class after;
|
||||||
|
template < typename E >
|
||||||
|
class before;
|
||||||
|
|
||||||
|
template < typename... Es >
|
||||||
class system;
|
class system;
|
||||||
|
class feature;
|
||||||
class registry;
|
class registry;
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
@@ -55,16 +62,18 @@ namespace ecs_hpp
|
|||||||
class option_conj;
|
class option_conj;
|
||||||
template < typename... Ts >
|
template < typename... Ts >
|
||||||
class option_disj;
|
class option_disj;
|
||||||
|
class option_bool;
|
||||||
|
|
||||||
|
template < typename... Ts >
|
||||||
|
class aspect;
|
||||||
|
|
||||||
class entity_filler;
|
class entity_filler;
|
||||||
class registry_filler;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace ecs_hpp
|
namespace ecs_hpp
|
||||||
{
|
{
|
||||||
using family_id = std::uint16_t;
|
using family_id = std::uint16_t;
|
||||||
using entity_id = std::uint32_t;
|
using entity_id = std::uint32_t;
|
||||||
using priority_t = std::int32_t;
|
|
||||||
|
|
||||||
constexpr std::size_t entity_id_index_bits = 22u;
|
constexpr std::size_t entity_id_index_bits = 22u;
|
||||||
constexpr std::size_t entity_id_version_bits = 10u;
|
constexpr std::size_t entity_id_version_bits = 10u;
|
||||||
@@ -281,8 +290,20 @@ namespace ecs_hpp
|
|||||||
incremental_locker() = default;
|
incremental_locker() = default;
|
||||||
~incremental_locker() noexcept = default;
|
~incremental_locker() noexcept = default;
|
||||||
|
|
||||||
incremental_locker(const incremental_locker&) = delete;
|
incremental_locker(incremental_locker&& other) noexcept = default;
|
||||||
incremental_locker& operator=(const incremental_locker&) = delete;
|
incremental_locker(const incremental_locker& other) noexcept = default;
|
||||||
|
|
||||||
|
incremental_locker& operator=(incremental_locker&& other) noexcept {
|
||||||
|
assert(!is_locked());
|
||||||
|
(void)other;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
incremental_locker& operator=(const incremental_locker& other) noexcept {
|
||||||
|
assert(!is_locked());
|
||||||
|
(void)other;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
void lock() noexcept {
|
void lock() noexcept {
|
||||||
++lock_count_;
|
++lock_count_;
|
||||||
@@ -1220,6 +1241,27 @@ namespace ecs_hpp
|
|||||||
void swap(prototype& l, prototype& r) noexcept;
|
void swap(prototype& l, prototype& r) noexcept;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// triggers
|
||||||
|
//
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace ecs_hpp
|
||||||
|
{
|
||||||
|
template < typename E >
|
||||||
|
class after {
|
||||||
|
public:
|
||||||
|
const E& event;
|
||||||
|
};
|
||||||
|
|
||||||
|
template < typename E >
|
||||||
|
class before {
|
||||||
|
public:
|
||||||
|
const E& event;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// system
|
// system
|
||||||
@@ -1228,10 +1270,63 @@ namespace ecs_hpp
|
|||||||
|
|
||||||
namespace ecs_hpp
|
namespace ecs_hpp
|
||||||
{
|
{
|
||||||
class system {
|
template <>
|
||||||
|
class system<> {
|
||||||
public:
|
public:
|
||||||
virtual ~system() = default;
|
virtual ~system() = default;
|
||||||
virtual void process(registry& owner) = 0;
|
};
|
||||||
|
|
||||||
|
template < typename E >
|
||||||
|
class system<E>
|
||||||
|
: public virtual system<> {
|
||||||
|
public:
|
||||||
|
virtual void process(registry& owner, const E& event) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
template < typename E, typename... Es>
|
||||||
|
class system<E, Es...>
|
||||||
|
: public system<E>
|
||||||
|
, public system<Es...> {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// feature
|
||||||
|
//
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace ecs_hpp
|
||||||
|
{
|
||||||
|
class feature final {
|
||||||
|
public:
|
||||||
|
feature() = default;
|
||||||
|
|
||||||
|
feature(const feature&) = delete;
|
||||||
|
feature& operator=(const feature&) = delete;
|
||||||
|
|
||||||
|
feature(feature&&) noexcept = default;
|
||||||
|
feature& operator=(feature&&) noexcept = default;
|
||||||
|
|
||||||
|
feature& enable() & noexcept;
|
||||||
|
feature&& enable() && noexcept;
|
||||||
|
|
||||||
|
feature& disable() & noexcept;
|
||||||
|
feature&& disable() && noexcept;
|
||||||
|
|
||||||
|
bool is_enabled() const noexcept;
|
||||||
|
bool is_disabled() const noexcept;
|
||||||
|
|
||||||
|
template < typename T, typename... Args >
|
||||||
|
feature& add_system(Args&&... args) &;
|
||||||
|
template < typename T, typename... Args >
|
||||||
|
feature&& add_system(Args&&... args) &&;
|
||||||
|
|
||||||
|
template < typename Event >
|
||||||
|
feature& process_event(registry& owner, const Event& event);
|
||||||
|
private:
|
||||||
|
bool disabled_{false};
|
||||||
|
std::vector<std::unique_ptr<system<>>> systems_;
|
||||||
|
mutable detail::incremental_locker systems_locker_;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1292,6 +1387,9 @@ namespace ecs_hpp
|
|||||||
registry(const registry& other) = delete;
|
registry(const registry& other) = delete;
|
||||||
registry& operator=(const registry& other) = delete;
|
registry& operator=(const registry& other) = delete;
|
||||||
|
|
||||||
|
registry(registry&& other) noexcept = default;
|
||||||
|
registry& operator=(registry&& other) noexcept = default;
|
||||||
|
|
||||||
entity wrap_entity(const const_uentity& ent) noexcept;
|
entity wrap_entity(const const_uentity& ent) noexcept;
|
||||||
const_entity wrap_entity(const const_uentity& ent) const noexcept;
|
const_entity wrap_entity(const const_uentity& ent) const noexcept;
|
||||||
|
|
||||||
@@ -1364,13 +1462,23 @@ namespace ecs_hpp
|
|||||||
template < typename... Ts, typename F, typename... Opts >
|
template < typename... Ts, typename F, typename... Opts >
|
||||||
void for_joined_components(F&& f, Opts&&... opts) const;
|
void for_joined_components(F&& f, Opts&&... opts) const;
|
||||||
|
|
||||||
template < typename T, typename... Args >
|
template < typename Tag, typename... Args >
|
||||||
void add_system(priority_t priority, Args&&... args);
|
feature& assign_feature(Args&&... args);
|
||||||
|
|
||||||
void process_all_systems();
|
template < typename Tag, typename... Args >
|
||||||
void process_systems_above(priority_t min);
|
feature& ensure_feature(Args&&... args);
|
||||||
void process_systems_below(priority_t max);
|
|
||||||
void process_systems_in_range(priority_t min, priority_t max);
|
template < typename Tag >
|
||||||
|
bool has_feature() const noexcept;
|
||||||
|
|
||||||
|
template < typename Tag >
|
||||||
|
feature& get_feature();
|
||||||
|
|
||||||
|
template < typename Tag >
|
||||||
|
const feature& get_feature() const;
|
||||||
|
|
||||||
|
template < typename Event >
|
||||||
|
registry& process_event(const Event& event);
|
||||||
|
|
||||||
struct memory_usage_info {
|
struct memory_usage_info {
|
||||||
std::size_t entities{0u};
|
std::size_t entities{0u};
|
||||||
@@ -1390,6 +1498,18 @@ namespace ecs_hpp
|
|||||||
template < typename T >
|
template < typename T >
|
||||||
detail::component_storage<T>& get_or_create_storage_();
|
detail::component_storage<T>& get_or_create_storage_();
|
||||||
|
|
||||||
|
template < typename F, typename... Opts >
|
||||||
|
void for_joined_components_impl_(
|
||||||
|
std::index_sequence<>,
|
||||||
|
F&& f,
|
||||||
|
Opts&&... opts);
|
||||||
|
|
||||||
|
template < typename F, typename... Opts >
|
||||||
|
void for_joined_components_impl_(
|
||||||
|
std::index_sequence<>,
|
||||||
|
F&& f,
|
||||||
|
Opts&&... opts) const;
|
||||||
|
|
||||||
template < typename T
|
template < typename T
|
||||||
, typename... Ts
|
, typename... Ts
|
||||||
, typename F
|
, typename F
|
||||||
@@ -1450,14 +1570,15 @@ namespace ecs_hpp
|
|||||||
private:
|
private:
|
||||||
entity_id last_entity_id_{0u};
|
entity_id last_entity_id_{0u};
|
||||||
std::vector<entity_id> free_entity_ids_;
|
std::vector<entity_id> free_entity_ids_;
|
||||||
|
|
||||||
mutable detail::incremental_locker entity_ids_locker_;
|
mutable detail::incremental_locker entity_ids_locker_;
|
||||||
detail::sparse_set<entity_id, detail::entity_id_indexer> entity_ids_;
|
detail::sparse_set<entity_id, detail::entity_id_indexer> entity_ids_;
|
||||||
|
|
||||||
using storage_uptr = std::unique_ptr<detail::component_storage_base>;
|
using storage_uptr = std::unique_ptr<detail::component_storage_base>;
|
||||||
detail::sparse_map<family_id, storage_uptr> storages_;
|
detail::sparse_map<family_id, storage_uptr> storages_;
|
||||||
|
|
||||||
using system_uptr = std::unique_ptr<system>;
|
mutable detail::incremental_locker features_locker_;
|
||||||
std::vector<std::pair<priority_t, system_uptr>> systems_;
|
detail::sparse_map<family_id, feature> features_;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1469,44 +1590,43 @@ namespace ecs_hpp
|
|||||||
|
|
||||||
namespace ecs_hpp
|
namespace ecs_hpp
|
||||||
{
|
{
|
||||||
//
|
namespace detail
|
||||||
// traits
|
{
|
||||||
//
|
template < typename T >
|
||||||
|
struct is_option
|
||||||
|
: std::false_type {};
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
struct option {
|
struct is_option<exists<T>>
|
||||||
static constexpr bool instance = false;
|
: std::true_type {};
|
||||||
};
|
|
||||||
|
template < typename... Ts >
|
||||||
|
struct is_option<exists_any<Ts...>>
|
||||||
|
: std::true_type {};
|
||||||
|
|
||||||
|
template < typename... Ts >
|
||||||
|
struct is_option<exists_all<Ts...>>
|
||||||
|
: std::true_type {};
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
struct option<exists<T>> {
|
struct is_option<option_neg<T>>
|
||||||
static constexpr bool instance = true;
|
: std::true_type {};
|
||||||
};
|
|
||||||
|
|
||||||
template < typename... Ts >
|
template < typename... Ts >
|
||||||
struct option<exists_any<Ts...>> {
|
struct is_option<option_conj<Ts...>>
|
||||||
static constexpr bool instance = true;
|
: std::true_type {};
|
||||||
};
|
|
||||||
|
|
||||||
template < typename... Ts >
|
template < typename... Ts >
|
||||||
struct option<exists_all<Ts...>> {
|
struct is_option<option_disj<Ts...>>
|
||||||
static constexpr bool instance = true;
|
: std::true_type {};
|
||||||
};
|
|
||||||
|
template <>
|
||||||
|
struct is_option<option_bool>
|
||||||
|
: std::true_type {};
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
struct option<option_neg<T>> {
|
inline constexpr bool is_option_v = is_option<T>::value;
|
||||||
static constexpr bool instance = true;
|
}
|
||||||
};
|
|
||||||
|
|
||||||
template < typename... Ts >
|
|
||||||
struct option<option_conj<Ts...>> {
|
|
||||||
static constexpr bool instance = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
template < typename... Ts >
|
|
||||||
struct option<option_disj<Ts...>> {
|
|
||||||
static constexpr bool instance = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// options
|
// options
|
||||||
@@ -1583,31 +1703,95 @@ namespace ecs_hpp
|
|||||||
std::tuple<Ts...> opts_;
|
std::tuple<Ts...> opts_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class option_bool final {
|
||||||
|
public:
|
||||||
|
option_bool(bool b)
|
||||||
|
: bool_(b) {}
|
||||||
|
|
||||||
|
bool operator()(const const_entity& e) const {
|
||||||
|
(void)e;
|
||||||
|
return bool_;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
bool bool_{false};
|
||||||
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// operators
|
// operators
|
||||||
//
|
//
|
||||||
|
|
||||||
template < typename A
|
template < typename A
|
||||||
, typename = std::enable_if_t<option<A>::instance>>
|
, typename = std::enable_if_t<detail::is_option_v<A>> >
|
||||||
option_neg<std::decay_t<A>> operator!(A&& a) {
|
option_neg<std::decay_t<A>> operator!(A&& a) {
|
||||||
return {std::forward<A>(a)};
|
return {std::forward<A>(a)};
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename A, typename B
|
template < typename A, typename B
|
||||||
, typename = std::enable_if_t<option<A>::instance>
|
, typename = std::enable_if_t<detail::is_option_v<A>>
|
||||||
, typename = std::enable_if_t<option<B>::instance> >
|
, typename = std::enable_if_t<detail::is_option_v<B>> >
|
||||||
option_conj<std::decay_t<A>, std::decay_t<B>> operator&&(A&& a, B&& b) {
|
option_conj<std::decay_t<A>, std::decay_t<B>> operator&&(A&& a, B&& b) {
|
||||||
return {std::forward<A>(a), std::forward<B>(b)};
|
return {std::forward<A>(a), std::forward<B>(b)};
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename A, typename B
|
template < typename A, typename B
|
||||||
, typename = std::enable_if_t<option<A>::instance>
|
, typename = std::enable_if_t<detail::is_option_v<A>>
|
||||||
, typename = std::enable_if_t<option<B>::instance> >
|
, typename = std::enable_if_t<detail::is_option_v<B>> >
|
||||||
option_disj<std::decay_t<A>, std::decay_t<B>> operator||(A&& a, B&& b) {
|
option_disj<std::decay_t<A>, std::decay_t<B>> operator||(A&& a, B&& b) {
|
||||||
return {std::forward<A>(a), std::forward<B>(b)};
|
return {std::forward<A>(a), std::forward<B>(b)};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// aspect
|
||||||
|
//
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace ecs_hpp
|
||||||
|
{
|
||||||
|
template < typename... Ts >
|
||||||
|
class aspect {
|
||||||
|
public:
|
||||||
|
static auto to_option() noexcept {
|
||||||
|
return (option_bool{true} && ... && exists<Ts>{});
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool match_entity(const const_entity& e) noexcept {
|
||||||
|
return (... && e.exists_component<Ts>());
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename F, typename... Opts >
|
||||||
|
static void for_each_entity(registry& owner, F&& f, Opts&&... opts) {
|
||||||
|
owner.for_joined_components<Ts...>(
|
||||||
|
[&f](const auto& e, const auto&...){
|
||||||
|
f(e);
|
||||||
|
}, std::forward<Opts>(opts)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename F, typename... Opts >
|
||||||
|
static void for_each_entity(const registry& owner, F&& f, Opts&&... opts) {
|
||||||
|
owner.for_joined_components<Ts...>(
|
||||||
|
[&f](const auto& e, const auto&...){
|
||||||
|
f(e);
|
||||||
|
}, std::forward<Opts>(opts)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename F, typename... Opts >
|
||||||
|
static void for_joined_components(registry& owner, F&& f, Opts&&... opts) {
|
||||||
|
owner.for_joined_components<Ts...>(
|
||||||
|
std::forward<F>(f),
|
||||||
|
std::forward<Opts>(opts)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename F, typename... Opts >
|
||||||
|
static void for_joined_components(const registry& owner, F&& f, Opts&&... opts) {
|
||||||
|
owner.for_joined_components<Ts...>(
|
||||||
|
std::forward<F>(f),
|
||||||
|
std::forward<Opts>(opts)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// fillers
|
// fillers
|
||||||
@@ -1629,22 +1813,6 @@ namespace ecs_hpp
|
|||||||
private:
|
private:
|
||||||
entity& entity_;
|
entity& entity_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class registry_filler final {
|
|
||||||
public:
|
|
||||||
registry_filler(registry& registry) noexcept
|
|
||||||
: registry_(registry) {}
|
|
||||||
|
|
||||||
template < typename T, typename... Args >
|
|
||||||
registry_filler& system(priority_t priority, Args&&... args) {
|
|
||||||
registry_.add_system<T>(
|
|
||||||
priority,
|
|
||||||
std::forward<Args>(args)...);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
registry& registry_;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
@@ -2210,6 +2378,76 @@ namespace ecs_hpp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// feature impl
|
||||||
|
//
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace ecs_hpp
|
||||||
|
{
|
||||||
|
inline feature& feature::enable() & noexcept {
|
||||||
|
disabled_ = false;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline feature&& feature::enable() && noexcept {
|
||||||
|
enable();
|
||||||
|
return std::move(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline feature& feature::disable() & noexcept {
|
||||||
|
disabled_ = true;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline feature&& feature::disable() && noexcept {
|
||||||
|
disable();
|
||||||
|
return std::move(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool feature::is_enabled() const noexcept {
|
||||||
|
return !disabled_;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool feature::is_disabled() const noexcept {
|
||||||
|
return disabled_;
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T, typename... Args >
|
||||||
|
feature& feature::add_system(Args&&... args) & {
|
||||||
|
assert(!systems_locker_.is_locked());
|
||||||
|
systems_.push_back(std::make_unique<T>(std::forward<Args>(args)...));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename T, typename... Args >
|
||||||
|
feature&& feature::add_system(Args&&... args) && {
|
||||||
|
add_system<T>(std::forward<Args>(args)...);
|
||||||
|
return std::move(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Event >
|
||||||
|
feature& feature::process_event(registry& owner, const Event& event) {
|
||||||
|
detail::incremental_lock_guard lock(systems_locker_);
|
||||||
|
|
||||||
|
const auto fire_event = [this, &owner](const auto& event){
|
||||||
|
for ( const auto& base_system : systems_ ) {
|
||||||
|
using system_type = system<std::decay_t<decltype(event)>>;
|
||||||
|
if ( auto event_system = dynamic_cast<system_type*>(base_system.get()) ) {
|
||||||
|
event_system->process(owner, event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fire_event(before<Event>{event});
|
||||||
|
fire_event(event);
|
||||||
|
fire_event(after<Event>{event});
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// registry impl
|
// registry impl
|
||||||
@@ -2591,45 +2829,59 @@ namespace ecs_hpp
|
|||||||
std::forward<Opts>(opts)...);
|
std::forward<Opts>(opts)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename T, typename... Args >
|
template < typename Tag, typename... Args >
|
||||||
void registry::add_system(priority_t priority, Args&&... args) {
|
feature& registry::assign_feature(Args&&... args) {
|
||||||
auto iter = std::upper_bound(
|
const auto feature_id = detail::type_family<Tag>::id();
|
||||||
systems_.begin(), systems_.end(), priority,
|
if ( feature* f = features_.find(feature_id) ) {
|
||||||
[](priority_t pr, const auto& r){
|
return *f = feature{std::forward<Args>(args)...};
|
||||||
return pr < r.first;
|
}
|
||||||
});
|
assert(!features_locker_.is_locked());
|
||||||
systems_.insert(
|
return *features_.insert(feature_id, feature()).first;
|
||||||
iter,
|
|
||||||
std::make_pair(priority, std::make_unique<T>(std::forward<Args>(args)...)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void registry::process_all_systems() {
|
template < typename Tag, typename... Args >
|
||||||
process_systems_in_range(
|
feature& registry::ensure_feature(Args&&... args) {
|
||||||
std::numeric_limits<priority_t>::min(),
|
const auto feature_id = detail::type_family<Tag>::id();
|
||||||
std::numeric_limits<priority_t>::max());
|
if ( feature* f = features_.find(feature_id) ) {
|
||||||
|
return *f;
|
||||||
|
}
|
||||||
|
assert(!features_locker_.is_locked());
|
||||||
|
return *features_.insert(feature_id, feature{std::forward<Args>(args)...}).first;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void registry::process_systems_above(priority_t min) {
|
template < typename Tag >
|
||||||
process_systems_in_range(
|
bool registry::has_feature() const noexcept {
|
||||||
min,
|
const auto feature_id = detail::type_family<Tag>::id();
|
||||||
std::numeric_limits<priority_t>::max());
|
return features_.has(feature_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void registry::process_systems_below(priority_t max) {
|
template < typename Tag >
|
||||||
process_systems_in_range(
|
feature& registry::get_feature() {
|
||||||
std::numeric_limits<priority_t>::min(),
|
const auto feature_id = detail::type_family<Tag>::id();
|
||||||
max);
|
if ( feature* f = features_.find(feature_id) ) {
|
||||||
|
return *f;
|
||||||
|
}
|
||||||
|
throw std::logic_error("ecs_hpp::registry (feature not found)");
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void registry::process_systems_in_range(priority_t min, priority_t max) {
|
template < typename Tag >
|
||||||
const auto first = std::lower_bound(
|
const feature& registry::get_feature() const {
|
||||||
systems_.begin(), systems_.end(), min,
|
const auto feature_id = detail::type_family<Tag>::id();
|
||||||
[](const auto& p, priority_t pr) noexcept {
|
if ( const feature* f = features_.find(feature_id) ) {
|
||||||
return p.first < pr;
|
return *f;
|
||||||
});
|
|
||||||
for ( auto iter = first; iter != systems_.end() && iter->first <= max; ++iter ) {
|
|
||||||
iter->second->process(*this);
|
|
||||||
}
|
}
|
||||||
|
throw std::logic_error("ecs_hpp::registry (feature not found)");
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Event >
|
||||||
|
registry& registry::process_event(const Event& event) {
|
||||||
|
detail::incremental_lock_guard lock(features_locker_);
|
||||||
|
for ( const auto family : features_ ) {
|
||||||
|
if ( feature& f = features_.get(family); f.is_enabled() ) {
|
||||||
|
f.process_event(*this, event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline registry::memory_usage_info registry::memory_usage() const noexcept {
|
inline registry::memory_usage_info registry::memory_usage() const noexcept {
|
||||||
@@ -2683,6 +2935,24 @@ namespace ecs_hpp
|
|||||||
storages_.get(family).get());
|
storages_.get(family).get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename F, typename... Opts >
|
||||||
|
void registry::for_joined_components_impl_(
|
||||||
|
std::index_sequence<>,
|
||||||
|
F&& f,
|
||||||
|
Opts&&... opts)
|
||||||
|
{
|
||||||
|
for_each_entity(std::forward<F>(f), std::forward<Opts>(opts)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename F, typename... Opts >
|
||||||
|
void registry::for_joined_components_impl_(
|
||||||
|
std::index_sequence<>,
|
||||||
|
F&& f,
|
||||||
|
Opts&&... opts) const
|
||||||
|
{
|
||||||
|
for_each_entity(std::forward<F>(f), std::forward<Opts>(opts)...);
|
||||||
|
}
|
||||||
|
|
||||||
template < typename T
|
template < typename T
|
||||||
, typename... Ts
|
, typename... Ts
|
||||||
, typename F
|
, typename F
|
||||||
|
|||||||
121
untests/ecs_readme.cpp
Normal file
121
untests/ecs_readme.cpp
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* This file is part of the "https://github.com/blackmatov/ecs.hpp"
|
||||||
|
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||||
|
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
#include <ecs.hpp/ecs.hpp>
|
||||||
|
namespace ecs = ecs_hpp;
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
TEST_CASE("example") {
|
||||||
|
|
||||||
|
// events
|
||||||
|
|
||||||
|
struct update_event {
|
||||||
|
float dt{};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct render_event {
|
||||||
|
std::string camera;
|
||||||
|
};
|
||||||
|
|
||||||
|
// components
|
||||||
|
|
||||||
|
struct movable {};
|
||||||
|
struct disabled {};
|
||||||
|
|
||||||
|
struct sprite {
|
||||||
|
std::string name;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct position {
|
||||||
|
float x{};
|
||||||
|
float y{};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct velocity {
|
||||||
|
float x{};
|
||||||
|
float y{};
|
||||||
|
};
|
||||||
|
|
||||||
|
// systems
|
||||||
|
|
||||||
|
class gravity_system : public ecs::system<update_event> {
|
||||||
|
public:
|
||||||
|
gravity_system(float gravity)
|
||||||
|
: gravity_(gravity) {}
|
||||||
|
|
||||||
|
void process(ecs::registry& world, const update_event& evt) override {
|
||||||
|
world.for_each_component<velocity>(
|
||||||
|
[this, &evt](ecs::entity, velocity& vel) {
|
||||||
|
vel.x += gravity_ * evt.dt;
|
||||||
|
vel.y += gravity_ * evt.dt;
|
||||||
|
}, ecs::exists<movable>{} && !ecs::exists<disabled>{});
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
float gravity_{};
|
||||||
|
};
|
||||||
|
|
||||||
|
class movement_system : public ecs::system<update_event> {
|
||||||
|
public:
|
||||||
|
void process(ecs::registry& world, const update_event& evt) override {
|
||||||
|
world.for_joined_components<position, velocity>(
|
||||||
|
[&evt](ecs::entity, position& pos, const velocity& vel) {
|
||||||
|
pos.x += vel.x * evt.dt;
|
||||||
|
pos.y += vel.y * evt.dt;
|
||||||
|
}, ecs::exists<movable>{} && !ecs::exists<disabled>{});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class render_system : public ecs::system<render_event> {
|
||||||
|
public:
|
||||||
|
void process(ecs::registry& world, const render_event& evt) override {
|
||||||
|
world.for_joined_components<sprite, position>(
|
||||||
|
[&evt](ecs::entity, const sprite& s, const position& p) {
|
||||||
|
std::cout << "Render sprite:" << std::endl;
|
||||||
|
std::cout << "--> pos: " << p.x << "," << p.y << std::endl;
|
||||||
|
std::cout << "--> sprite: " << s.name << std::endl;
|
||||||
|
std::cout << "--> camera: " << evt.camera << std::endl;
|
||||||
|
}, !ecs::exists<disabled>{});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// world
|
||||||
|
|
||||||
|
ecs::registry world;
|
||||||
|
|
||||||
|
struct physics_feature {};
|
||||||
|
world.assign_feature<physics_feature>()
|
||||||
|
.add_system<movement_system>()
|
||||||
|
.add_system<gravity_system>(9.8f);
|
||||||
|
|
||||||
|
struct rendering_feature {};
|
||||||
|
world.assign_feature<rendering_feature>()
|
||||||
|
.add_system<render_system>();
|
||||||
|
|
||||||
|
// entities
|
||||||
|
|
||||||
|
auto entity_one = world.create_entity();
|
||||||
|
ecs::entity_filler(entity_one)
|
||||||
|
.component<movable>()
|
||||||
|
.component<sprite>("ship")
|
||||||
|
.component<position>(4.f, 2.f)
|
||||||
|
.component<velocity>(10.f, 20.f);
|
||||||
|
|
||||||
|
auto entity_two = world.create_entity();
|
||||||
|
ecs::entity_filler(entity_two)
|
||||||
|
.component<movable>()
|
||||||
|
.component<sprite>("player")
|
||||||
|
.component<position>(4.f, 2.f)
|
||||||
|
.component<velocity>(10.f, 20.f);
|
||||||
|
|
||||||
|
// processing
|
||||||
|
|
||||||
|
world.process_event(update_event{0.1f});
|
||||||
|
world.process_event(render_event{"main"});
|
||||||
|
}
|
||||||
@@ -60,14 +60,19 @@ namespace
|
|||||||
TEST_CASE("detail") {
|
TEST_CASE("detail") {
|
||||||
SECTION("get_type_id") {
|
SECTION("get_type_id") {
|
||||||
using namespace ecs::detail;
|
using namespace ecs::detail;
|
||||||
REQUIRE(type_family<position_c>::id() == 1u);
|
|
||||||
REQUIRE(type_family<position_c>::id() == 1u);
|
|
||||||
|
|
||||||
REQUIRE(type_family<velocity_c>::id() == 2u);
|
const auto p_id = type_family<position_c>::id();
|
||||||
REQUIRE(type_family<velocity_c>::id() == 2u);
|
REQUIRE(p_id == type_family<position_c>::id());
|
||||||
|
|
||||||
REQUIRE(type_family<position_c>::id() == 1u);
|
const auto v_id = type_family<velocity_c>::id();
|
||||||
REQUIRE(type_family<velocity_c>::id() == 2u);
|
REQUIRE(v_id == type_family<velocity_c>::id());
|
||||||
|
|
||||||
|
REQUIRE(type_family<position_c>::id() == type_family<position_c>::id());
|
||||||
|
REQUIRE(type_family<velocity_c>::id() == type_family<velocity_c>::id());
|
||||||
|
REQUIRE_FALSE(type_family<position_c>::id() == type_family<velocity_c>::id());
|
||||||
|
|
||||||
|
REQUIRE(p_id == type_family<position_c>::id());
|
||||||
|
REQUIRE(v_id == type_family<velocity_c>::id());
|
||||||
}
|
}
|
||||||
SECTION("tuple_tail") {
|
SECTION("tuple_tail") {
|
||||||
using namespace ecs::detail;
|
using namespace ecs::detail;
|
||||||
@@ -1185,6 +1190,102 @@ TEST_CASE("registry") {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
SECTION("aspects") {
|
||||||
|
{
|
||||||
|
using empty_aspect = ecs::aspect<>;
|
||||||
|
|
||||||
|
ecs::registry w;
|
||||||
|
|
||||||
|
ecs::entity e1 = w.create_entity();
|
||||||
|
REQUIRE(empty_aspect::match_entity(e1));
|
||||||
|
|
||||||
|
ecs::entity e2 = w.create_entity();
|
||||||
|
e2.assign_component<movable_c>();
|
||||||
|
e2.assign_component<position_c>(1,2);
|
||||||
|
REQUIRE(empty_aspect::match_entity(e2));
|
||||||
|
|
||||||
|
ecs::entity e3 = w.create_entity();
|
||||||
|
e3.assign_component<movable_c>();
|
||||||
|
e3.assign_component<position_c>(1,2);
|
||||||
|
e3.assign_component<velocity_c>(3,4);
|
||||||
|
REQUIRE(empty_aspect::match_entity(e3));
|
||||||
|
|
||||||
|
{
|
||||||
|
ecs::entity_id acc{};
|
||||||
|
empty_aspect::for_each_entity(w, [&acc](ecs::entity e){
|
||||||
|
acc += e.id();
|
||||||
|
}, ecs::exists<movable_c>{});
|
||||||
|
REQUIRE(acc == e2.id() + e3.id());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ecs::entity_id acc{};
|
||||||
|
empty_aspect::for_joined_components(w, [&acc](ecs::entity e){
|
||||||
|
acc += e.id();
|
||||||
|
}, ecs::exists<movable_c>{});
|
||||||
|
REQUIRE(acc == e2.id() + e3.id());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ecs::entity_id acc{};
|
||||||
|
w.for_each_entity([&acc](ecs::entity e){
|
||||||
|
acc += e.id();
|
||||||
|
}, empty_aspect::to_option());
|
||||||
|
REQUIRE(acc == e1.id() + e2.id() + e3.id());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
using movable = ecs::aspect<
|
||||||
|
position_c,
|
||||||
|
velocity_c>;
|
||||||
|
|
||||||
|
ecs::registry w;
|
||||||
|
|
||||||
|
ecs::entity e = w.create_entity();
|
||||||
|
REQUIRE_FALSE(movable::match_entity(e));
|
||||||
|
|
||||||
|
e.assign_component<position_c>(1,2);
|
||||||
|
REQUIRE_FALSE(movable::match_entity(e));
|
||||||
|
|
||||||
|
e.assign_component<velocity_c>(3,4);
|
||||||
|
REQUIRE(movable::match_entity(e));
|
||||||
|
|
||||||
|
ecs::entity e2 = w.create_entity();
|
||||||
|
e2.assign_component<position_c>(1,2);
|
||||||
|
|
||||||
|
movable::for_joined_components(w,
|
||||||
|
[](ecs::entity_id, position_c& p, const velocity_c& v){
|
||||||
|
p.x += v.x;
|
||||||
|
p.y += v.y;
|
||||||
|
});
|
||||||
|
|
||||||
|
movable::for_joined_components(std::as_const(w),
|
||||||
|
[](ecs::entity_id, const position_c& p, const velocity_c& v){
|
||||||
|
const_cast<position_c&>(p).x += v.x;
|
||||||
|
const_cast<position_c&>(p).y += v.y;
|
||||||
|
});
|
||||||
|
|
||||||
|
w.for_each_entity([](ecs::entity e){
|
||||||
|
auto& p = e.get_component<position_c>();
|
||||||
|
const auto& v = e.get_component<velocity_c>();
|
||||||
|
p.x += v.x;
|
||||||
|
p.y += v.y;
|
||||||
|
}, movable::to_option());
|
||||||
|
|
||||||
|
std::as_const(w).for_each_entity([](const ecs::const_entity& e){
|
||||||
|
const auto& p = e.get_component<position_c>();
|
||||||
|
const auto& v = e.get_component<velocity_c>();
|
||||||
|
const_cast<position_c&>(p).x += v.x;
|
||||||
|
const_cast<position_c&>(p).y += v.y;
|
||||||
|
}, movable::to_option());
|
||||||
|
|
||||||
|
REQUIRE(e.get_component<position_c>().x == 1 + 3*4);
|
||||||
|
REQUIRE(e.get_component<position_c>().y == 2 + 4*4);
|
||||||
|
|
||||||
|
REQUIRE(e2.get_component<position_c>().x == 1);
|
||||||
|
REQUIRE(e2.get_component<position_c>().y == 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
SECTION("options") {
|
SECTION("options") {
|
||||||
{
|
{
|
||||||
ecs::registry w;
|
ecs::registry w;
|
||||||
@@ -1367,88 +1468,140 @@ TEST_CASE("registry") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
SECTION("systems") {
|
SECTION("systems") {
|
||||||
{
|
struct update_evt {
|
||||||
class movement_system : public ecs::system {
|
int dt{};
|
||||||
public:
|
|
||||||
void process(ecs::registry& owner) override {
|
|
||||||
owner.for_joined_components<position_c, velocity_c>([](
|
|
||||||
ecs::entity, position_c& p, const velocity_c& v)
|
|
||||||
{
|
|
||||||
p.x += v.x;
|
|
||||||
p.y += v.y;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ecs::registry w;
|
class gravity_system : public ecs::system<update_evt> {
|
||||||
w.add_system<movement_system>(0);
|
|
||||||
|
|
||||||
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_all_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);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
class system_n : public ecs::system {
|
|
||||||
public:
|
public:
|
||||||
system_n(int& i, int n) : i_(i), n_(n) {}
|
gravity_system(int g)
|
||||||
void process(ecs::registry&) override {
|
: g_(g) {}
|
||||||
i_ += n_;
|
|
||||||
|
void process(ecs::registry& owner, const update_evt& evt) override {
|
||||||
|
owner.for_each_component<
|
||||||
|
velocity_c
|
||||||
|
>([this, &evt](ecs::entity, velocity_c& v) {
|
||||||
|
v.x += g_ * evt.dt;
|
||||||
|
v.y += g_ * evt.dt;
|
||||||
|
}, !ecs::exists<disabled_c>{});
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
int& i_;
|
int g_{};
|
||||||
int n_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int i = 0;
|
class movement_system : public ecs::system<update_evt> {
|
||||||
ecs::registry w;
|
public:
|
||||||
w.add_system<system_n>(20, std::ref(i), 2);
|
void process(ecs::registry& owner, const update_evt& evt) override {
|
||||||
w.add_system<system_n>(10, std::ref(i), 1);
|
owner.for_joined_components<position_c, velocity_c>([&evt](
|
||||||
REQUIRE(i == 0);
|
ecs::entity, position_c& p, const velocity_c& v)
|
||||||
w.process_all_systems();
|
{
|
||||||
REQUIRE(i == 3);
|
p.x += v.x * evt.dt;
|
||||||
w.process_systems_below(10);
|
p.y += v.y * evt.dt;
|
||||||
REQUIRE(i == 4);
|
}, !ecs::exists<disabled_c>{});
|
||||||
w.process_systems_above(20);
|
|
||||||
REQUIRE(i == 6);
|
|
||||||
w.process_systems_below(20);
|
|
||||||
REQUIRE(i == 9);
|
|
||||||
w.process_systems_above(10);
|
|
||||||
REQUIRE(i == 12);
|
|
||||||
|
|
||||||
w.process_systems_below(9);
|
|
||||||
w.process_systems_above(21);
|
|
||||||
REQUIRE(i == 12);
|
|
||||||
|
|
||||||
w.process_systems_in_range(0, 9);
|
|
||||||
w.process_systems_in_range(11, 19);
|
|
||||||
w.process_systems_in_range(21, 30);
|
|
||||||
REQUIRE(i == 12);
|
|
||||||
|
|
||||||
w.process_systems_in_range(0, 10);
|
|
||||||
REQUIRE(i == 13);
|
|
||||||
w.process_systems_in_range(10, 19);
|
|
||||||
REQUIRE(i == 14);
|
|
||||||
w.process_systems_in_range(10, 20);
|
|
||||||
REQUIRE(i == 17);
|
|
||||||
w.process_systems_in_range(20, 30);
|
|
||||||
REQUIRE(i == 19);
|
|
||||||
w.process_systems_in_range(10, 20);
|
|
||||||
REQUIRE(i == 22);
|
|
||||||
w.process_systems_in_range(0, 30);
|
|
||||||
REQUIRE(i == 25);
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ecs::registry w;
|
||||||
|
REQUIRE_FALSE(w.has_feature<struct physics>());
|
||||||
|
|
||||||
|
w.assign_feature<struct physics>()
|
||||||
|
.add_system<gravity_system>(9);
|
||||||
|
|
||||||
|
REQUIRE(w.has_feature<struct physics>());
|
||||||
|
|
||||||
|
w.ensure_feature<struct physics>()
|
||||||
|
.add_system<movement_system>();
|
||||||
|
|
||||||
|
REQUIRE(w.has_feature<struct physics>());
|
||||||
|
|
||||||
|
ecs::entity e = w.create_entity();
|
||||||
|
e.assign_component<position_c>(1,2);
|
||||||
|
e.assign_component<velocity_c>(3,4);
|
||||||
|
|
||||||
|
w.get_feature<struct physics>().disable();
|
||||||
|
w.process_event(update_evt{2});
|
||||||
|
|
||||||
|
REQUIRE(e.get_component<position_c>().x == 1);
|
||||||
|
REQUIRE(e.get_component<position_c>().y == 2);
|
||||||
|
|
||||||
|
w.get_feature<struct physics>().enable();
|
||||||
|
w.process_event(update_evt{2});
|
||||||
|
|
||||||
|
REQUIRE(e.get_component<position_c>().x == 1 + (3 + 9 * 2) * 2);
|
||||||
|
REQUIRE(e.get_component<position_c>().y == 2 + (4 + 9 * 2) * 2);
|
||||||
|
}
|
||||||
|
SECTION("recursive_systems") {
|
||||||
|
struct update_evt {
|
||||||
|
int dt{};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct physics_evt {
|
||||||
|
update_evt parent{};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct clear_velocity_evt {
|
||||||
|
};
|
||||||
|
|
||||||
|
class gravity_system : public ecs::system<ecs::before<physics_evt>> {
|
||||||
|
public:
|
||||||
|
gravity_system(int g)
|
||||||
|
: g_(g) {}
|
||||||
|
|
||||||
|
void process(ecs::registry& owner, const ecs::before<physics_evt>& before) override {
|
||||||
|
owner.for_each_component<velocity_c>(
|
||||||
|
[this, &evt = before.event](ecs::entity, velocity_c& v) {
|
||||||
|
v.x += g_ * evt.parent.dt;
|
||||||
|
v.y += g_ * evt.parent.dt;
|
||||||
|
}, !ecs::exists<disabled_c>{});
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
int g_{};
|
||||||
|
};
|
||||||
|
|
||||||
|
class movement_system : public ecs::system<physics_evt> {
|
||||||
|
public:
|
||||||
|
void process(ecs::registry& owner, const physics_evt& evt) override {
|
||||||
|
owner.for_joined_components<position_c, velocity_c>(
|
||||||
|
[&evt](ecs::entity, position_c& p, const velocity_c& v) {
|
||||||
|
p.x += v.x * evt.parent.dt;
|
||||||
|
p.y += v.y * evt.parent.dt;
|
||||||
|
}, !ecs::exists<disabled_c>{});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class physics_system : public ecs::system<update_evt, clear_velocity_evt> {
|
||||||
|
public:
|
||||||
|
void process(ecs::registry& owner, const update_evt& evt) override {
|
||||||
|
owner.process_event(physics_evt{evt});
|
||||||
|
}
|
||||||
|
|
||||||
|
void process(ecs::registry& owner, const clear_velocity_evt& evt) override {
|
||||||
|
(void)evt;
|
||||||
|
owner.remove_all_components<velocity_c>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ecs::registry w;
|
||||||
|
|
||||||
|
w.assign_feature<struct physics>()
|
||||||
|
.add_system<gravity_system>(9)
|
||||||
|
.add_system<movement_system>()
|
||||||
|
.add_system<physics_system>();
|
||||||
|
|
||||||
|
ecs::entity e = w.create_entity();
|
||||||
|
e.assign_component<position_c>(1,2);
|
||||||
|
e.assign_component<velocity_c>(3,4);
|
||||||
|
|
||||||
|
w.process_event(update_evt{2});
|
||||||
|
|
||||||
|
REQUIRE(e.get_component<position_c>().x == 1 + (3 + 9 * 2) * 2);
|
||||||
|
REQUIRE(e.get_component<position_c>().y == 2 + (4 + 9 * 2) * 2);
|
||||||
|
|
||||||
|
REQUIRE(w.component_count<velocity_c>() == 1);
|
||||||
|
|
||||||
|
w.process_event(clear_velocity_evt{});
|
||||||
|
|
||||||
|
REQUIRE(w.component_count<velocity_c>() == 0);
|
||||||
}
|
}
|
||||||
SECTION("fillers") {
|
SECTION("fillers") {
|
||||||
struct component_n {
|
struct component_n {
|
||||||
@@ -1456,10 +1609,12 @@ TEST_CASE("registry") {
|
|||||||
component_n(int ni) : i(ni) {}
|
component_n(int ni) : i(ni) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class system_n : public ecs::system {
|
struct update_evt {};
|
||||||
|
|
||||||
|
class system_n : public ecs::system<update_evt> {
|
||||||
public:
|
public:
|
||||||
system_n(int n) : n_(n) {}
|
system_n(int n) : n_(n) {}
|
||||||
void process(ecs::registry& owner) override {
|
void process(ecs::registry& owner, const update_evt&) override {
|
||||||
owner.for_each_component<component_n>(
|
owner.for_each_component<component_n>(
|
||||||
[this](const ecs::const_entity&, component_n& c) noexcept {
|
[this](const ecs::const_entity&, component_n& c) noexcept {
|
||||||
c.i += n_;
|
c.i += n_;
|
||||||
@@ -1470,9 +1625,9 @@ TEST_CASE("registry") {
|
|||||||
};
|
};
|
||||||
{
|
{
|
||||||
ecs::registry w;
|
ecs::registry w;
|
||||||
ecs::registry_filler(w)
|
w.assign_feature<struct physics>()
|
||||||
.system<system_n>(0, 1)
|
.add_system<system_n>(1)
|
||||||
.system<system_n>(0, 2);
|
.add_system<system_n>(2);
|
||||||
|
|
||||||
ecs::entity e1 = w.create_entity();
|
ecs::entity e1 = w.create_entity();
|
||||||
ecs::entity_filler(e1)
|
ecs::entity_filler(e1)
|
||||||
@@ -1483,7 +1638,7 @@ TEST_CASE("registry") {
|
|||||||
ecs::entity_filler(e2)
|
ecs::entity_filler(e2)
|
||||||
.component<component_n>(2);
|
.component<component_n>(2);
|
||||||
|
|
||||||
w.process_all_systems();
|
w.process_event(update_evt{});
|
||||||
|
|
||||||
REQUIRE(e1.get_component<component_n>().i == 4);
|
REQUIRE(e1.get_component<component_n>().i == 4);
|
||||||
REQUIRE(e2.get_component<component_n>().i == 5);
|
REQUIRE(e2.get_component<component_n>().i == 5);
|
||||||
@@ -1586,69 +1741,3 @@ TEST_CASE("registry") {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("example") {
|
|
||||||
struct movable {};
|
|
||||||
struct disabled {};
|
|
||||||
|
|
||||||
struct position {
|
|
||||||
float x{};
|
|
||||||
float y{};
|
|
||||||
};
|
|
||||||
|
|
||||||
struct velocity {
|
|
||||||
float dx{};
|
|
||||||
float dy{};
|
|
||||||
};
|
|
||||||
|
|
||||||
class movement_system : public ecs::system {
|
|
||||||
public:
|
|
||||||
void process(ecs::registry& owner) override {
|
|
||||||
owner.for_joined_components<
|
|
||||||
position,
|
|
||||||
velocity
|
|
||||||
>([](ecs::entity, position& p, const velocity& v) {
|
|
||||||
p.x += v.dx;
|
|
||||||
p.y += v.dy;
|
|
||||||
}, ecs::exists<movable>{} && !ecs::exists<disabled>{});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class gravity_system : public ecs::system {
|
|
||||||
public:
|
|
||||||
gravity_system(float gravity)
|
|
||||||
: gravity_(gravity) {}
|
|
||||||
|
|
||||||
void process(ecs::registry& owner) override {
|
|
||||||
owner.for_each_component<
|
|
||||||
velocity
|
|
||||||
>([this](ecs::entity, velocity& v) {
|
|
||||||
v.dx += gravity_;
|
|
||||||
v.dy += gravity_;
|
|
||||||
}, !ecs::exists<disabled>{});
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
float gravity_{};
|
|
||||||
};
|
|
||||||
|
|
||||||
ecs::registry world;
|
|
||||||
|
|
||||||
ecs::registry_filler(world)
|
|
||||||
.system<movement_system>(0)
|
|
||||||
.system<gravity_system>(1, 9.8f);
|
|
||||||
|
|
||||||
auto entity_one = world.create_entity();
|
|
||||||
ecs::entity_filler(entity_one)
|
|
||||||
.component<movable>()
|
|
||||||
.component<position>(4.f, 2.f)
|
|
||||||
.component<velocity>(10.f, 20.f);
|
|
||||||
|
|
||||||
auto entity_two = world.create_entity();
|
|
||||||
ecs::entity_filler(entity_two)
|
|
||||||
.component<movable>()
|
|
||||||
.component<disabled>()
|
|
||||||
.component<position>(4.f, 2.f)
|
|
||||||
.component<velocity>(10.f, 20.f);
|
|
||||||
|
|
||||||
world.process_all_systems();
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user