Giter Site home page Giter Site logo

twoecs's Introduction

Two ECS

A single header implementation of ECS. Since there isn’t a strict definition of what an ECS is, this library follows the definition presented in this GDC 2017 talk on the implementation used in Overwatch. By this definition an Entity is just an id (aliased to uint32_t by default), a Component has no functionality, a System holds no data, and a World is a collection of Entities, Systems and Components.

This library is probably best used as a reference for learning, since if you’re implementing a game engine you’ll likely want to implement your own ECS anyways. The library is written in C++11 and does not use RTTI or exceptions.

A minimal example

For the complete API documentation see the docs. For a more complete example see the SDL2 implemementation of a simple particle system.

Components

Any struct that is copy constructible and copy assignable can be used as a component. Components do not need to be registered before they are used.

struct Transform {
    float x, y;
};

struct Velocity {
    float x, y;
};

Systems

class MoveSystem : public two::System {
public:
    void update(two::World *world, float dt) override {
        // Returns all entities that have both a Transform and
        // a Velocity component.
        world->each<Transform, Velocity>([dt](Transform &tf, Velocity &vel) {
            tf.x += vel.x * dt;
            tf.y += vel.y * dt;
        });
    }
    // May also override System::load(World *), System::draw(World *),
    // and System::unload(World *)
};

Alternative to world->each:

void draw(two::World *world) override {
    // Alternative to the callback based world->each
    for (auto entity : world->view<Transform>()) {
         auto &tf = world->unpack<Transform>(entity);
         printf("(%f, %f)\n", tf.x, tf.y);
    }
}

Worlds

Worlds manage systems, entities, components and events. Worlds can be used to load/unload game assets and setup entities and systems.

class MainWorld : public two::World {
public:
    void load() override {
        // This will call System::load(world)
        make_system<MoveSystem>();

        auto entity = make_entity();
        pack(entity, Transform{0.0f, 0.0f}, Velocity{0.1f, 0.2f});
    }

    void update(float dt) override {
        // This may be done in your engine if all systems
        // are updated in the same way.
        for (auto *system : systems()) {
            system->update(this, dt);
        }
    }
    // May also override World::unload()
};

int main() {
    MainWorld world;
    world.load();
    for (int frame = 0; frame < 100000; ++i) {
        // Handle events
        // ...
        // Update world
        world.update(1.0f / 60.0f);
        // Render
        for (auto *system : world.systems()) {
            system->draw(&world);
        }
        world.collect_unused_entities();
    }
    world.destroy_systems();
    world.unload();
    return 0;
}

Events

Events can be used to communicate between systems. Each world has independent event channels. Just like components, any data type can be used as an event.

struct KeyDown {
    int key;
};

int main() {
    two::World world;
    world.bind<KeyDown>([](const KeyDown &event) {
        printf("Key <%d> was pressed\n", event.key);
        return true;
    });
    world.emit(KeyDown{0x20});
    return 0;
}

Performance

benchmarks

Given that the entity system is unlikely to be the bottleneck in an engine, this library prioritizes flexibility in the API over being as fast as possible.

twoecs's People

Contributors

stillwwater avatar

Stargazers

gongjinye avatar Koji D avatar Rui Jiang avatar Vlad avatar Jip Boesenkool avatar  avatar  avatar Webster Sheets avatar Anders Howard avatar Ben Gamble avatar  avatar  avatar Mark Luetke avatar Galli Davide Francesco Maria avatar danielsdesk avatar  avatar  avatar  avatar Joan Andrés avatar  avatar Jaldhar H. Vyas avatar Francisco Esquibel avatar Mikolaj Konarski avatar

Watchers

James Cloos avatar  avatar  avatar

twoecs's Issues

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.