mirror of
https://github.com/BlackMATov/ecs.hpp.git
synced 2025-12-12 18:16:14 +07:00
3.0 KiB
3.0 KiB
ecs.hpp
Installation
ecs.hpp is a header-only library. All you need to do is copy the headers files from headers directory into your project and include them:
#include "ecs.hpp/ecs.hpp"
Also, you can add the root repository directory to your cmake project:
add_subdirectory(external/ecs.hpp)
target_link_libraries(your_project_target ecs.hpp)
Basic usage
struct position_component {
float x;
float y;
position_component(float nx, float ny)
: x(nx), y(ny) {}
};
struct velocity_component {
float dx;
float dy;
velocity_component(float ndx, float ndy)
: dx(ndx), dy(ndy) {}
};
class movement_system : public ecs_hpp::system {
public:
void process(ecs_hpp::registry& owner) override {
owner.for_joined_components<
position_component,
velocity_component
>([](const ecs_hpp::entity& e, position_component& p, const velocity_component& v) {
p.x += v.dx;
p.y += v.dy;
});
}
};
class gravity_system : public ecs_hpp::system {
public:
gravity_system(float gravity)
: gravity_(gravity) {}
void process(ecs_hpp::registry& owner) override {
owner.for_each_component<
velocity_component
>([this](const ecs_hpp::entity& e, velocity_component& v) {
v.dx += gravity_;
v.dy += gravity_;
});
}
private:
float gravity_;
};
ecs_hpp::registry world;
world.add_system<movement_system>(0);
world.add_system<gravity_system>(1, 9.8f);
auto entity_one = world.create_entity();
world.assign_component<position_component>(entity_one, 4.f, 2.f);
world.assign_component<velocity_component>(entity_one, 10.f, 20.f);
auto entity_two = world.create_entity();
entity_two.assign_component<position_component>(4.f, 2.f);
entity_two.assign_component<velocity_component>(10.f, 20.f);
world.process_all_systems();
API
coming soon!