spearwolf / ecs

an entity component system for javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@spearwolf/ecs

An entity component system for javascript.

What does a Component look like?

@Component({
  name: 'foo'
})
class Foo {

  hello() => {
    console.log('moin moin!');
  };

  connectToEntity(entity) {
    entity.on('hello', this);
  }

  disconnectFromEntity(entity) {
    entity.off(this);
  }

}

@Component({
  name: 'bar'
})
class Bar {

  initialize(message) {
    this.message = message;
  }

  sayHello = () => {
    console.log(this.message);
  }

  connectToEntity(entity) {
    entity.on('hello', this.sayHello);
  }

  disconnectFromEntity(entity) {
    entity.off(this.sayHello);
  }

  // destroy()

}

What is an Entity and how does it play together?

// first, you need an entrypoint
// .. where you can register your components
const ecs = new ECS([ Foo, Bar ]);

// then you can create an entity and attach some components to it
const entity = ecs.createEntity([
  Foo,
  [Bar, "hej!"], // our Bar component needs some configuration
]);

// as a result you will get an object with 'foo' and 'bar' properties
// both properties are pointing to their component instances

entity.foo // is an instance of Foo
entity.bar // is an instance of Bar

entity.id // each entity has an unique id!

// now we can play with our entity ..

entity.bar.sayHello() // => 'hej!'

entity.emit('hello') // emit the event 'action'

// => 'moin moin!'
// => 'hej!'

About

an entity component system for javascript

License:Apache License 2.0


Languages

Language:JavaScript 100.0%