decs 1.0.0

@nogc D Entity Component System


To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

/ Works in progress /

decs

decs is my attempt to build a @nogc D language Entity Component System. I'm not really an game dev, im trying to learn how an ECS works so some portions of this code can be inefficients or bug prone.

It's inspired by the following projects :

https://github.com/miguelmartin75/anax => C++ ECS https://github.com/jzhu98/star-entity => D ECS https://github.com/claudemr/entitysysd => D ECS

How to build

You have to use dub to build the project.

Add this project as a dependency to your dub.json:

"dependencies": {
    "decs": "~>1.0.0"
}

How to use

Entity Manager


import decs;

void main()
{
    auto em = new EntityManager();

    while(game)
    {
        em.update(dt);
    }
    
    // !important!
    em.dispose();
}

Entities

You can create an entity, kill it, invalidate it or activate it (still need some work here).

auto entity = em.createEntity();

// Notifies all systems that a new entity is alive (check system section to understand this part)
entity.activate();

// Entity is still alive but will be invalid in current scope
entity.invalidate();

// Poor entity :<
entity.kill();

Components

An component must be represented as a struct.

struct PositionComponent
{
    float x, y, z;
}

You can add a component to an entity :

// add
entity.add!PositionComponent(1, 2, 3);

// update (its handled as a pointer)
entity.get!PositionComponent().y = 50;

// check
if(entity.has!PositionComponent())
{
    // ...
}

Systems

An system must be a class that inherits decs.System.

class MovementSystem : System
{
    public void update(in float delta)
    {

    }
}

There is 2 ways to query entities from your system :

  • 1 => Using entity manager entities!(components...) function
  • 2 => Using the components filter mixin mixin ComponentsFilter!(components...);

If you provide an components filter, when an entity is activated, it will be automatically added to the systems whose filters match her components.

class MovementSystem : System
{
    // option 2
    mixin ComponentsFilter!(PositionComponent);

    public void update(in float deltaTime)
    {
        // option 1
        auto entities = this.m_entityManager.entities!(PositionComponent);

        // option 2 (should be more efficient)
        foreach(entity; this.m_entities)
        {

        }
    }
}

System's update method will be called automatically by the entity manager in the main loop. If you want to handle the update manually, you can set the System.UpdatePolicy to manual :

class MovementSystem : System
{
    public this()
    {
        super(System.UpdatePolicy.Manual);
    }
}

// ...

while(game)
{
    em.update(dt);

    // you have to call it manually (before or after the em.update call)
    myMovementSystem.update(dt);
}

Events

wip

TODOs

  • Add @component and @event UDAs
  • Add the possibility to defines systems execution order when using em.update(dt)
  • Add anexample project
  • Improve this readme
Authors:
aldo
Dependencies:
dnogc
Versions:
1.0.0 2017-Oct-23
~master 2017-Nov-08
Show all 2 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 0 downloads total