Add an instance of a system to the game.
Example:
var scriptSystem = require('script-system');
game.addSystem(new scriptSystem());
Returns true
if added, and false
if there was a problem adding the system.
Add an instance of a component factory to the game.
Example:
var positionComponent = require('position-component');
game.addComponent('position', positionComponent);
Returns true
if added, and false
if there was a problem adding the component.
Create a new game object with the specified ID string.
If template is supplied, components will be created with the object and added automatically.
See Instantiating Objects for more information.
Example:
game.createObject('object id', {'position': {'x': 0, 'y': 0}});
Returns true
if the object was created, otherwise returns false
if there was an error.
Get an object instance by its ID string.
Example:
game.createObject('object id');
var obj = game.getObject('object id');
Returns the object instance if it was found, otherwise returns undefined
.
An object instance is a map of component type to component ID, for example:
{
id: "object id",
position: 1, //this object's `position` component has ID 1
size: 3 //this object's `size` component has ID 3
}
Removes an object and its components from the game entirely.
Example:
game.createObject('object id');
game.removeObject('object id');
game.getObject('object id'); //returns undefined
Add a new component of type componentType
to an existing object with ID objectID
.
Example:
game.createObject('object id');
game.addComponentToObject('object id', 'position', {'x': 0, 'y': 0});
Returns true
if the component was added, otherwise returns false
if there was an error;
Get all component instances of the specified type.
Example:
var positions = game.getComponentInstances('position');
for (var i = 0; i < positions.length; ++i) {
console.log(positions[i].x, positions[i].y);
}
Returns an array of component instances, or an empty array if none exist.
Get a particular component instance belonging to an object with ID objectID
.
Example:
game.createObject('Player', {'position': {'x': 0, 'y': 5}});
var position = game.getComponentInstance('Player', 'position');
console.log(position.x, position.y); //outputs '0 5'
Returns the component instance, or undefined
if it is not found.
Get all objects that have a particular type of component.
Example:
var renderableObjects = game.getObjectsWithComponent('renderable');
for (var i = 0; i < renderableObjects.length; ++i) {
//render each renderable object here
}
The game engine emits the following events:
Emitted when a new component instance is created.
The event listeners are called with the component type (string) and the component instance (object) as parameters:
this.engine.on('componentCreated', function (type, instance) {
//...
});
Emitted when the update tick begins.
Emitted when all systems have finished updating.
Emitted when the render tick begins.
Emitted when all systems have finished rendering.