https://github.com/spearwolf/ecs
an entity component system for javascript
https://github.com/spearwolf/ecs
ecs javascript
Last synced: about 1 year ago
JSON representation
an entity component system for javascript
- Host: GitHub
- URL: https://github.com/spearwolf/ecs
- Owner: spearwolf
- License: apache-2.0
- Created: 2019-02-15T10:42:51.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T21:53:32.000Z (over 3 years ago)
- Last Synced: 2025-01-22T08:13:43.422Z (over 1 year ago)
- Topics: ecs, javascript
- Language: JavaScript
- Homepage:
- Size: 914 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `@spearwolf/ecs`
An [entity component system](https://en.wikipedia.org/wiki/Entity_component_system) for javascript.
## What does a Component look like?
```js
@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?
```js
// 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!'
```