Coder Social home page Coder Social logo

simpleecs's Introduction

Simple Entity Component System

Simple file implementing an Entity Component System for fast prototyping. Written in C++20.

Functions & Features

  • Entity is just an identifier.
  • Component could be any kind of data.
  • System functions *:
    • Init().
    • Update(double dt).

^* Note custom systems must inherit from ECSSystem to specify the dependencies.

  • Register functions:
    • AddComponents<...Components>(Entity): binds new component/s of the specified type to an existing entity. Cannot add an unregister component.
    • RegisterComponent<Component>(): stores component types.
    • RemoveComponent<Component>(Entity): removes an existing component from an entity. Also removes the entity from all systems using it.
    • GetComponent<Component>(Entity): obtains the component object from an entity. Entity must have a component binded previously.
    • RegisterSystem<System>(): creates a new system. When a system is registered will consume ONLY the entities binded with at least one component marked as dependency in ECSSystem class header.

Usage example:

#include <cstdio>
#include <SimpleECS.h>

struct PositionComponent
{
   float x = 0.0f;
   float y = 0.0f;
   float z = 0.0f;
};

struct NameComponent
{
   std::string name = "";
};

class MySystem : public ECS::ECSSystem<PositionComponent, NameComponent>
{
public:
   MySystem() = default;

   void Init() override
   {
      int i = 0;
      for(auto entity : mEntities)
      {
         mRegister->GetComponent<NameComponent>(entity).name = "Entity_" + std::to_string(i);

         ++i;
      }
   }

   void Update(double dt) override
   {
      for(auto entity : mEntities)
      {
         auto& pos = mRegister->GetComponent<PositionComponent>(entity);
         pos.x += 2.0;

         printf("%s[%f, %f, %f]\n", mRegister->GetComponent<NameComponent>(entity).name.c_str(), pos.x, pos.y, pos.z);
      }
   }

};

int main()
{
   ECS::Register reg{};

   ECS::Entity entity0 = ECS::CreateEntity();
   ECS::Entity entity1 = ECS::CreateEntity();

   reg.RegisterComponent<PositionComponent>();   
   reg.RegisterComponent<NameComponent>();

   reg.AddComponents<PositionComponent, NameComponent>(entity0);
   reg.AddComponents<PositionComponent, NameComponent>(entity1);
   
   auto system = reg.RegisterSystem<MySystem>();

   system->Init();
   for(double i = 0.0; i < 25.0; i += 1.0)
   {
      system->Update(i);
   }


   return 0;
}

simpleecs's People

Contributors

josesegade avatar

Watchers

 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.