Hash of component definitions. Also aliased to comps
.
var comp = { name: 'foo' }
ecs.createComponent(comp)
ecs.components['foo'] === comp // true
ecs.comps['foo'] // same
Creates a new component from a definition object.
The definition must have a name
; all other properties are optional.
Returns the component name, to make it easy to grab when the component
is being require
d from a module.
var comp = {
name: 'some-unique-string',
state: {},
order: 99,
multi: false,
onAdd: (id, state) => { },
onRemove: (id, state) => { },
system: (dt, states) => { },
renderSystem: (dt, states) => { },
}
var name = ecs.createComponent( comp )
// name == 'some-unique-string'
Note the multi
flag - for components where this is true, a given
entity can have multiple state objects for that component.
For multi-components, APIs that would normally return a state object
(like getState
) will instead return an array of them.
Creates a new entity id (currently just an incrementing integer).
Optionally takes a list of component names to add to the entity (with default state data).
var id1 = ecs.createEntity()
var id2 = ecs.createEntity([ 'some-component', 'other-component' ])
Deletes the component definition with the given name. First removes the component from all entities that have it.
Note: This API shouldn't be necessary in most real-world usage - you should set up all your components during init and then leave them be. But it's useful if, say, you receive an ECS from another library and you need to replace its components.
ecs.deleteComponent( 'some-component' )
Makes a hasComponent
-like accessor function bound to a given component.
The accessor is much faster than hasComponent
.
ecs.createComponent({
name: 'foo',
})
var hasFoo = ecs.getComponentAccessor('foo')
// ...
ecs.addComponent(id, 'foo')
hasFoo(id) // true
Get the component state for a given entity.
It will automatically have an __id
property for the entity id.
ecs.createComponent({
name: 'foo',
state: { val: 0 }
})
ecs.addComponent(id, 'foo')
ecs.getState(id, 'foo').val // 0
ecs.getState(id, 'foo').__id // equals id
Makes a getState
-like accessor bound to a given component.
The accessor is faster than getState
, so you may want to create
an accessor for any component you'll be accessing a lot.
ecs.createComponent({
name: 'size',
state: { val: 0 }
})
var getEntitySize = ecs.getStateAccessor('size')
// ...
ecs.addComponent(id, 'size', { val:123 })
getEntitySize(id).val // 123
Get an array of state objects for every entity with the given component.
Each one will have an __id
property for the entity id it refers to.
Don't add or remove elements from the returned list!
var arr = ecs.getStatesList('foo')
// returns something shaped like:
// [
// {__id:0, x:1},
// {__id:7, x:2},
// ]
Checks if an entity has a component.
ecs.addComponent(id, 'foo')
ecs.hasComponent(id, 'foo') // true
Removes one particular instance of a multi-component. To avoid breaking loops, the relevant state object will get nulled immediately, and spliced from the states array later when safe (after the current tick/render/animationFrame).
// where component 'foo' is a multi-component
ecs.getState(id, 'foo') // [ state1, state2, state3 ]
ecs.removeMultiComponent(id, 'foo', 1)
ecs.getState(id, 'foo') // [ state1, null, state3 ]
// one JS event loop later...
ecs.getState(id, 'foo') // [ state1, state3 ]
Functions exactly like tick
, but calls renderSystem
functions.
this effectively gives you a second set of systems that are
called with separate timing, in case you want to
tick and render in separate loops
(which you should!).
ecs.createComponent({
name: foo,
order: 5,
renderSystem: function(dt, states) {
// states is the same array you'd get from #getStatesList()
}
})
ecs.render(1000/60)
Tells the ECS that a game tick has occurred, causing component
system
functions to get called.
The optional parameter simply gets passed to the system functions. It's meant to be a timestep, but can be used (or not used) as you like.
If components have an order
property, they'll get called in that order
(lowest to highest). Component order defaults to 99
.
ecs.createComponent({
name: foo,
order: 1,
system: function(dt, states) {
// states is the same array you'd get from #getStatesList()
states.forEach(state => {
console.log('Entity ID: ', state.__id)
})
}
})
ecs.tick(30) // triggers log statements
Generated using TypeDoc
Adds a component to an entity, optionally initializing the state object.