Code Monkey home page Code Monkey logo

ecs's Introduction

ECS: Minimal ECS system

Simple strong typed I mean no pointers ecs system. It’s inspired by fast-ecs.

Definitions

  • Entity: is an id number.
  • Component: data storage POD.
  • System: any logic/fuction/memfun that operate on these components.
  • Archetype: a collection of entities that have the same components, we would call it components-store.
  • Archetypes Manager: a collection of Archetypes.

Implementation

  • Entity: struct storing a number with a pointer to components-store to do useful stuff with it and it isn’t store in the system, so you mostly would do insertion with them.
  • Component: any POD containing data otherwise you are abusing ECS.
  • Archetype: components-store, it’s a vector of SortedTuples. Sorted tuples are std::tuple with types rearranged in descending order to reduce memory difference when using tuple vs struct. this class supports adding new entity–components that Archetype contains–, checking for components existence, and removing/adding a component this method creates a new Archetype and they are intended to be used before ECS creation as we cannot change ECS type.
  • Archetypes Manager: Class ECS contains a sorted tuple of Archetypes and support add new entity–creating new one–, along with running over all entities or all entities in a certain Archetype.

example:

// COMPONENTS
struct Position {
  float x, y;
};

struct Direction {
  float angle;
};

struct Mesh {
  std::string name;
};

// Archetypes
using Physics = ecs::Archetype<Position, Direction>;
using Render = ecs::Archetype<Position, Mesh>;

// example
int main(int, char*[]) {
  auto world = ecs::ECS<Physics, Render>();
  auto e1 = world.Add<Physics>();  // new entity from physics Archetype

  e1.Set<Position>(1.4f, 1.134f);

  auto e2 = world.Add<Render>();
  e2.Set<Position>(200.24f, 100.0f);
  e2.Set<Mesh>("ECS Rendering!");

  // loop over physics entities
  world.Run<Physics>([](auto& e) {
    auto position = e.template Get<Position>();
    auto direction = e.template Get<Direction>();
    std::cout << position.x << std::endl;
    std::cout << position.y << std::endl;
    std::cout << direction.angle << std::endl;
    e.template Set<Position>(128.0231f, 1231.0f);
  });

  return 0;
}

Build

Prerequisites

  • CMake 3.0 or newer
  • working C++17 compiler
  • Compiler defines __PRETTY_FUNCTION__ as constexpr
mkdir build
cd build
cmake ..
make

  • [ ] add unit tests
  • [ ] message passing!

Copyright

See LICENSE file

Copyright (c) 2021 Islam Omar ([email protected])

ecs's People

Contributors

islam0mar avatar

Watchers

 avatar  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.