Skip to content
Chun-hao Hu edited this page May 15, 2013 · 4 revisions

addSystem(system)

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.

addComponent(id, component)

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.

createObject(id, [template])

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.

getObject(id)

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
}

removeObject(id)

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

addComponentToObject(objectID, componentType, options)

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;

getComponentInstances(componentType)

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.

getComponentInstance(objectID, componentType)

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.

getObjectsWithComponent(componentType)

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
}

Events

The game engine emits the following events:

componentCreated

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) {
  //...
});

updateBegin

Emitted when the update tick begins.

updateEnd

Emitted when all systems have finished updating.

renderBegin

Emitted when the render tick begins.

renderEnd

Emitted when all systems have finished rendering.