Engine

Engine

new Engine()

Source:

This module manages the state of entities, components and systems. The heart of Entropy.

Extends

Members

(private) _entities :FastArray

Source:

Array with entities. Array index corresponds to ID of an entity. First element is empty (equals 0), because entity IDs start from 1. Entity with id property equal 0 is officially not present in the system (it can be for example present in the pool or waiting for addition to system).

Type:
  • FastArray

(private) _entitiesCount :Number

Source:

Current number of entities active.

Type:
  • Number

(private) _entitiesIdsPool :Pool

Source:

When entity is removed, it's ID can be reused by new entities. This pool stores old IDs ready to reuse.

Type:
  • Pool

(private) _entitiesToAdd :FastArray

Source:

Queue of entities ready to be added on next tick.

Type:
  • FastArray

(private) _entitiesToRemove :FastArray

Source:

Queue of entities ready to be removed on next tick.

Type:
  • FastArray

(private) _events :Object

Source:
Inherited From:

Object with registered event listeners. Keys are event names.

Type:
  • Object

(private) _isClearingScheduled :Boolean

Source:

Indicates whether clearing is scheduled.

Type:
  • Boolean

(private) _modifiedEntities :FastArray

Source:

List of modified entities.

When entity is modified it is added to this list. After each frame modifications are applied to every entity on the list.

Type:
  • FastArray

(private) _queries :Array

Source:

Array of queries. Every query that was used is stored here and updated when engine state changes.

Type:
  • Array

(private) _systems :FastArray

Source:

Systems that are processed every tick.

Type:
  • FastArray

(private) _systemsToAdd :FastArray

Source:

Queue of systems ready to be added on next tick.

Type:
  • FastArray

(private) _systemsToRemove :FastArray

Source:

Queue of systems ready to be removed on next tick.

Type:
  • FastArray

(private) _wasClearingPerformed :Boolean

Source:

Indicates whether clearing was performed.

Type:
  • Boolean

Methods

addEntity(entity)

Source:

Adds entity to adding queue. If entity is new (not recycled), adds event listener for modifications.

Parameters:
Name Type Description
entity Entity

entity to add

addSystem(system)

Source:

Adds system to adding queue.

Parameters:
Name Type Description
system System

to add

clear()

Source:

Schedules clearing. Clearing is done on next frame.

emit(event, …argopt)

Source:
Inherited From:

Emits event. All listeners attached to this event name earlier will be called with arguments passed after event name.

Parameters:
Name Type Attributes Description
event String

event name

arg Any <optional>
<repeatable>

multiple arguments, that will be passed to listeners

getEntities(query) → {Object}

Source:

Gets entities matching query criterions.

Parameters:
Name Type Description
query Query

query

Returns:

object with entities and length properties

Type
Object

off(event, fn)

Source:
Inherited From:

Removes listener for event.

Example
const myHandler = (e) => {}
ee.on('myEvent', myHandler) // handler attached
ee.off('myEvent', myHandler) // handler detached
Parameters:
Name Type Description
event String

event name

fn function

listener function attached earlier

on(event, fn, onceopt)

Source:
Inherited From:

Method used to register event listener.

Parameters:
Name Type Attributes Default Description
event String

event name

fn function

event listener

once Boolean <optional>
false

if set to true, listener will be called once, then it will be unregistered

once(event, fn)

Source:
Inherited From:

Same as EventEmitter#on, bu with implicit once parameter set to true.

Parameters:
Name Type Description
event String

event name

fn function

event listener

removeEntity(entity)

Source:

Adds entity to removing queue.

Parameters:
Name Type Description
entity Entity

entity to remove

removeSystem(systemOrType)

Source:

Adds system to removing queue.

Parameters:
Name Type Description
systemOrType String | System

system instance or system type to remove

startResponding()

Source:
Inherited From:

Reenables disabled event emitter.

stopResponding()

Source:
Inherited From:

Disables event emitter so it won't repond to any emitted events.

update(…args)

Source:

Updates the engine:

  • updates systems (calls onUpdate method of every active system)
  • performs clearing, if scheduled
  • applies engine modifications (adding/removing entities/systems, updating queries)
Parameters:
Name Type Attributes Description
args Any <repeatable>

arguments passed to systems onUpdate methods

Fires:

Events

clear

Source:

Engine was cleared.