Skip to content

robertlong/hecs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HECS

HECS Entity Component System

An experimental ECS written in Javascript.

THIS PROJECT IS NO LONGER MAINTAINED

I have transfered the npm package to a new project. The last maintained version of hecs is 0.1.1. I am currently maintaining a new ECS project ECSY and encourage users to move to it.

Goals

  • Performance
    • As little memory allocation and garbage collection as possible
    • Fast iterators
  • Predictability
    • Systems run in the order they are registered
    • Events are processed in the game loop with the rest of the system's logic

Roadmap

  • Custom Schedulers
    • Specify dependencies between systems
    • Enable systems to run in parallel
  • Parallelism
    • Using transferable objects in WebWorkers
    • Using SharedArrayBuffer
  • WASM Integration
    • Rust API that can be used to write high performance systems in WASM

Getting Started

npm install -S [email protected]
import { World, System, EntityId, Read, Write } from "hecs";

const world = new World();

class Position {
  constructor(x) {
    this.x = x;
  }
}

class Velocity {
  constructor(v) {
    this.v = v;
  }
}

world.registerComponent(Position);
world.registerComponent(Velocity);

class VelocitySystem extends System {
  setup() {
    return {
      entities: this.world.createQuery(Write(Position), Read(Velocity))
    };
  }

  update() {
    for (const [position, velocity] of this.ctx.entities) {
      position.x += velocity.v;
    }
  }
}

class LoggingSystem extends System {
  setup() {
    return {
      entities: this.world.createQuery(EntityId, Read(Position))
    };
  }

  update() {
    for (const [entityId, position] of this.ctx.entities) {
      console.log(`Entity ${entityId} has position: ${position.x}`);
    }
  }
}

world.registerSystem(new VelocitySystem());
world.registerSystem(new LoggingSystem());

const entity1 = world.createEntity();
world.addComponent(entity1, new Position(0));
world.addComponent(entity1, new Velocity(1));

const entity2 = world.createEntity();
world.addComponent(entity2, new Position(10));
world.addComponent(entity2, new Velocity(0.5));

function update() {
  world.update();
  requestAnimationFrame(update);
}

requestAnimationFrame(update);

Credits

  • API heavily inspired by Specs

About

An experimental ECS written in Javascript.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published