mirror of
https://github.com/BlackMATov/ecs.hpp.git
synced 2025-12-15 11:53:51 +07:00
update readme example
This commit is contained in:
68
README.md
68
README.md
@@ -41,61 +41,71 @@ target_link_libraries(your_project_target ecs.hpp)
|
|||||||
## Basic usage
|
## Basic usage
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
struct position_component {
|
|
||||||
float x;
|
#include <ecs.hpp/ecs.hpp>
|
||||||
float y;
|
namespace ecs = ecs_hpp;
|
||||||
position_component(float nx, float ny)
|
|
||||||
: x(nx), y(ny) {}
|
struct movable {};
|
||||||
|
struct disabled {};
|
||||||
|
|
||||||
|
struct position {
|
||||||
|
float x{};
|
||||||
|
float y{};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct velocity_component {
|
struct velocity {
|
||||||
float dx;
|
float dx{};
|
||||||
float dy;
|
float dy{};
|
||||||
velocity_component(float ndx, float ndy)
|
|
||||||
: dx(ndx), dy(ndy) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class movement_system : public ecs_hpp::system {
|
class movement_system : public ecs::system {
|
||||||
public:
|
public:
|
||||||
void process(ecs_hpp::registry& owner) override {
|
void process(ecs::registry& owner) override {
|
||||||
owner.for_joined_components<
|
owner.for_joined_components<
|
||||||
position_component,
|
position,
|
||||||
velocity_component
|
velocity
|
||||||
>([](const ecs_hpp::entity& e, position_component& p, const velocity_component& v) {
|
>([](ecs::entity, position& p, const velocity& v) {
|
||||||
p.x += v.dx;
|
p.x += v.dx;
|
||||||
p.y += v.dy;
|
p.y += v.dy;
|
||||||
});
|
}, ecs::require<movable>{}, ecs::filter<disabled>{});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class gravity_system : public ecs_hpp::system {
|
class gravity_system : public ecs::system {
|
||||||
public:
|
public:
|
||||||
gravity_system(float gravity)
|
gravity_system(float gravity)
|
||||||
: gravity_(gravity) {}
|
: gravity_(gravity) {}
|
||||||
|
|
||||||
void process(ecs_hpp::registry& owner) override {
|
void process(ecs::registry& owner) override {
|
||||||
owner.for_each_component<
|
owner.for_each_component<
|
||||||
velocity_component
|
velocity
|
||||||
>([this](const ecs_hpp::entity& e, velocity_component& v) {
|
>([this](ecs::entity e, velocity& v) {
|
||||||
v.dx += gravity_;
|
v.dx += gravity_;
|
||||||
v.dy += gravity_;
|
v.dy += gravity_;
|
||||||
});
|
}, ecs::filter<disabled>{});
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
float gravity_;
|
float gravity_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
ecs_hpp::registry world;
|
ecs::registry world;
|
||||||
world.add_system<movement_system>(0);
|
|
||||||
world.add_system<gravity_system>(1, 9.8f);
|
ecs::registry_filler(world)
|
||||||
|
.system<movement_system>(0)
|
||||||
|
.system<gravity_system>(1, 9.8f);
|
||||||
|
|
||||||
auto entity_one = world.create_entity();
|
auto entity_one = world.create_entity();
|
||||||
world.assign_component<position_component>(entity_one, 4.f, 2.f);
|
ecs::entity_filler(entity_one)
|
||||||
world.assign_component<velocity_component>(entity_one, 10.f, 20.f);
|
.component<movable>()
|
||||||
|
.component<position>(4.f, 2.f)
|
||||||
|
.component<velocity>(10.f, 20.f);
|
||||||
|
|
||||||
auto entity_two = world.create_entity();
|
auto entity_two = world.create_entity();
|
||||||
entity_two.assign_component<position_component>(4.f, 2.f);
|
ecs::entity_filler(entity_two)
|
||||||
entity_two.assign_component<velocity_component>(10.f, 20.f);
|
.component<movable>()
|
||||||
|
.component<disabled>()
|
||||||
|
.component<position>(4.f, 2.f)
|
||||||
|
.component<velocity>(10.f, 20.f);
|
||||||
|
|
||||||
world.process_all_systems();
|
world.process_all_systems();
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1571,61 +1571,67 @@ TEST_CASE("registry") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("example") {
|
TEST_CASE("example") {
|
||||||
struct position_component {
|
struct movable {};
|
||||||
float x;
|
struct disabled {};
|
||||||
float y;
|
|
||||||
position_component(float nx, float ny)
|
struct position {
|
||||||
: x(nx), y(ny) {}
|
float x{};
|
||||||
|
float y{};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct velocity_component {
|
struct velocity {
|
||||||
float dx;
|
float dx{};
|
||||||
float dy;
|
float dy{};
|
||||||
velocity_component(float ndx, float ndy)
|
|
||||||
: dx(ndx), dy(ndy) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class movement_system : public ecs_hpp::system {
|
class movement_system : public ecs::system {
|
||||||
public:
|
public:
|
||||||
void process(ecs_hpp::registry& owner) override {
|
void process(ecs::registry& owner) override {
|
||||||
owner.for_joined_components<
|
owner.for_joined_components<
|
||||||
position_component,
|
position,
|
||||||
velocity_component
|
velocity
|
||||||
>([](const ecs_hpp::entity&, position_component& p, const velocity_component& v) {
|
>([](ecs::entity, position& p, const velocity& v) {
|
||||||
p.x += v.dx;
|
p.x += v.dx;
|
||||||
p.y += v.dy;
|
p.y += v.dy;
|
||||||
});
|
}, ecs::require<movable>{}, ecs::filter<disabled>{});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class gravity_system : public ecs_hpp::system {
|
class gravity_system : public ecs::system {
|
||||||
public:
|
public:
|
||||||
gravity_system(float gravity)
|
gravity_system(float gravity)
|
||||||
: gravity_(gravity) {}
|
: gravity_(gravity) {}
|
||||||
|
|
||||||
void process(ecs_hpp::registry& owner) override {
|
void process(ecs::registry& owner) override {
|
||||||
owner.for_each_component<
|
owner.for_each_component<
|
||||||
velocity_component
|
velocity
|
||||||
>([this](const ecs_hpp::entity&, velocity_component& v) {
|
>([this](ecs::entity, velocity& v) {
|
||||||
v.dx += gravity_;
|
v.dx += gravity_;
|
||||||
v.dy += gravity_;
|
v.dy += gravity_;
|
||||||
});
|
}, ecs::filter<disabled>{});
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
float gravity_;
|
float gravity_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
ecs_hpp::registry world;
|
ecs::registry world;
|
||||||
world.add_system<movement_system>(0);
|
|
||||||
world.add_system<gravity_system>(1, 9.8f);
|
ecs::registry_filler(world)
|
||||||
|
.system<movement_system>(0)
|
||||||
|
.system<gravity_system>(1, 9.8f);
|
||||||
|
|
||||||
auto entity_one = world.create_entity();
|
auto entity_one = world.create_entity();
|
||||||
world.assign_component<position_component>(entity_one, 4.f, 2.f);
|
ecs::entity_filler(entity_one)
|
||||||
world.assign_component<velocity_component>(entity_one, 10.f, 20.f);
|
.component<movable>()
|
||||||
|
.component<position>(4.f, 2.f)
|
||||||
|
.component<velocity>(10.f, 20.f);
|
||||||
|
|
||||||
auto entity_two = world.create_entity();
|
auto entity_two = world.create_entity();
|
||||||
entity_two.assign_component<position_component>(4.f, 2.f);
|
ecs::entity_filler(entity_two)
|
||||||
entity_two.assign_component<velocity_component>(10.f, 20.f);
|
.component<movable>()
|
||||||
|
.component<disabled>()
|
||||||
|
.component<position>(4.f, 2.f)
|
||||||
|
.component<velocity>(10.f, 20.f);
|
||||||
|
|
||||||
world.process_all_systems();
|
world.process_all_systems();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user