Skip to content
A Haxe Unity3D-like entity-component system.
Haxe
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
simpleECS
.gitignore
LICENSE
README.md

README.md

simpleECS

A Haxe Unity3D-like entity-component system.

Usage

  • Add simpleECS to your classpaths.
  • That's it, quite simple.

Wait, where is my GameObject/Actor class?!

Well, I decided to call it Entity, after all you're not necessarily using this lib to make games.

Code samples

Your components should extend the Component class:

package;
import simpleECS.Component;
import simpleECS.Entity;

class OneComponent extends Component 
{
    public function new(owner:Entity) 
    {
        super(owner);
    }
    public function oneComponentMethod():Void
    {
        trace("This is oneComponentMethod.");
    }   
    public function checkOtherComponent():Void
    {
        if (owner.getComponent(OtherComponent) != null)
            trace("Yay, we have OtherComponent!");
        else
            trace("We don't have OtherComponent.");
    }
    /*ALWAYS override this method so you can properly destroy your components.*/
    override public function destroy():Void
    {
        if (_isDestroyed) //preventing multiple destroy calls.
            return;
            
        _isDestroyed = true;
        //your component destruction code goes here...
        super.destroy();
        trace("OneComponent destroyed.");
    }
}

class OtherComponent extends Component 
{
    public function new(owner:Entity) 
    {
        super(owner);
    }   
    public function otherComponentMethod():Void
    {
        trace("This is otherComponentMethod");
    }
    /*ALWAYS override this method so you can properly destroy your components.*/
    override public function destroy():Void
    {
        if (_isDestroyed) //preventing multiple destroy calls.
            return;
            
        _isDestroyed = true;
        //your component destruction code goes here...
        super.destroy();
        trace("OtherComponent destroyed.");
    }
}

Then, somewhere else you can build your entities:

var ent = new Entity();
ent.addComponent(new OneComponent(ent)).addComponent(new OtherComponent(ent));

var oneComp:OneComponent = ent.getComponent(OneComponent);
oneComp.oneComponentMethod();

var otherComp:OtherComponent = ent.getComponent(OtherComponent);
otherComp.otherComponentMethod();

ent.removeComponent(OtherComponent);
trace("ent.numComponents: " + ent.numComponents + "\n"); //ent.numComponents: 1
You can’t perform that action at this time.