Coder Social home page Coder Social logo

deirhx / ecs.hpp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from blackmatov/ecs.hpp

0.0 1.0 0.0 288 KB

C++17 Entity Component System

Home Page: https://blackmatov.github.io/ecs.hpp

License: MIT License

CMake 0.32% C++ 99.19% Batchfile 0.19% Shell 0.29%

ecs.hpp's Introduction

ecs.hpp

C++17 Entity Component System

travis appveyor codecov language license paypal

Requirements

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

#include <ecs.hpp/ecs.hpp>
namespace ecs = ecs_hpp;

// 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"});

ecs.hpp's People

Contributors

blackmatov avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.