Class: TurnEngine

Class: TurnEngine

TurnEngine

new TurnEngine(isServer)

Represents an turn based game engine. It's like a normal ECS with Action and Turn.

Parameters:
Name Type Argument Default Description
isServer Boolean <optional>
false

Whether if it's a server or not

Source:
See:

Extends

Members

isServer :Boolean

A boolean contains whether if it's a server or not.

Type:
  • Boolean
Inherited From:
Source:

players :Array

An arraying holding all the player Entity in the game.

Type:
  • Array
Source:
See:

systems :Array

Array of all the System in the Engine. You shouldn't edit this array directly, use Engine#addSystem and Engine#removeSystem instead.

Type:
  • Array
Inherited From:
Source:

turns :Array

An array holding all the Turn used in the game.

Type:
  • Array
Source:
See:

Methods

addEntity(entity)

Adds an Entity to the Engine.

Parameters:
Name Type Description
entity Entity

An Entity to add

Inherited From:
Source:
Fires:

addSystem(key, system)

Adds the System to the Engine. The System will be triggered when the Engine#update is called. This will trigger System#add.

Parameters:
Name Type Description
key String

The System's string key

system System

The System to add

Inherited From:
Source:

c(key, constructor)

Registers a Component type to the Engine.

Parameters:
Name Type Description
key String

Component's string key.

constructor function

Component's constructor.

Inherited From:
Source:

createAction(name, entity, player, options) → {Action}

Creates a new Action using registered constructor.

Parameters:
Name Type Description
name String

The name of the Action.

entity Entity

The entity associated with the action.

player Entity

The player who requested the action.

options Object

The arguments associated with the action.

Inherited From:
Source:
Returns:

The created Action.

Type
Action

createEntity() → {Entity}

Creates an empty Entity and adds to the Engine.

Inherited From:
Source:
Fires:
Returns:

an empty Entity.

Type
Entity

createSystem(key) → {SystemBuilder}

Returns a new SystemBuilder associated the Engine.

Parameters:
Name Type Description
key String

The System's string key to use.

Inherited From:
Source:
Returns:

A new SystemBuilder

Type
SystemBuilder

defineAction(name, constructor)

Defines an Action type to the Engine.

Parameters:
Name Type Description
name String

The name of the Action.

constructor function

The constructor of the Action.

Inherited From:
Source:

deserialize(serialized)

Deserializes the Engine object and places on provided engine. The provided Engine object should be clean; it shouldn't contain any Entity object. But it can contain pre-defined Components and Systems. Note that systems won't be included in serialized object, so user should load the systems on their own.

Parameters:
Name Type Description
serialized Object

Engine object

Inherited From:
Source:

e()

Executes Entity related functions by its arguments.

If no argument is provided, it returns new Entity. If a number is provided, it returns a Entity with that ID. If an object is provided, it returns new Entity populated with the template. Otherwise, it returns an array of Entity having provided list components.

Inherited From:
Source:

emit(event)

Emits the event. This will trigger all the listener with that event type. You can supply listener function's arguments by appending arguments after the 'event' parameter.

Parameters:
Name Type Description
event String

The name of the event.

args...

Arguments to supply to listeners.

Inherited From:
Source:

getActionConstructor(name) → {function}

Returns the Action's construtor with specified name.

Parameters:
Name Type Description
name String

The name of the Action.

Inherited From:
Source:
Returns:

The constructor of the Action.

Type
function

getComponentBit(key)

Returns Component type's unique ID in the Engine. This unique ID is used in BitSet's bit position. This will call Engine#registerComponent if needed.

Parameters:
Name Type Description
key String

Component's string key.

Inherited From:
Source:
Returns:

the Component's unique ID.

getComponentConstructor(key)

Returns Component's constructor registered in the Engine/

Parameters:
Name Type Description
key String

Component's string key.

Inherited From:
Source:
Returns:

the Component's constructor.

getComponentGroup(entities) → {ComponentGroup}

Searches ComponentGroup by its array.

Parameters:
Name Type Description
entities Array

An array of ComponentGroup

Inherited From:
Source:
Returns:

The ComponentGroup linked with the array

Type
ComponentGroup

getComponentName(key)

Returns Component type's key by its unique ID.

Parameters:
Name Type Description
key Number

Component's unique ID

Inherited From:
Source:
Returns:

the Component's string key.

getComponentsBitSet(components) → {BitSet}

Returns a BitSet holding combination of Components type's unique ID.

Parameters:
Name Type Description
components Array

An array holding Component keys.

Inherited From:
Source:
Returns:

A BitSet holding combination of Components.

Type
BitSet

getEntities() → {Array}

Returns an array of Entity. It shouldn't be edited since the Engine uses it directly.

Inherited From:
Source:
Returns:

An array holding Entity

Type
Array

getEntitiesFor(componentGroup) → {Array}

Returns an array having Entity matching ComponentGroup's criteria. The array will keep updated, so you can call this method only once and check its contents if needed. It's an alias function to Engine#registerComponentGroup

Parameters:
Name Type Description
componentGroup ComponentGroup

The ComponentGroup

Inherited From:
Source:
See:
Fires:
Returns:

An array holding Entity matching its criteria

Type
Array

getEntity(id) → {Entity}

Returns an Entity with given ID. This will return null if it can't be found.

Parameters:
Name Type Description
id Number

An ID to find the Entity

Inherited From:
Source:
Returns:

The entity with given ID

Type
Entity

getSystem(key) → {System}

Returns the System registered to the Engine.

Parameters:
Name Type Description
key String

The System's string key

Inherited From:
Source:
Returns:

The system registered to the Engine

Type
System

getTurn() → {Turn}

Returns current Turn object. It'll call TurnEngine#nextTurn if the game hasn't started yet.

Source:
Returns:

The current Turn

Type
Turn

listeners(event) → {Array}

Returns the array of listeners with specified event type.

Parameters:
Name Type Description
event String

The name of the event.

Inherited From:
Source:
Returns:

An array holding event listeners.

Type
Array

nextTurn() → {Turn}

Finishes the current Turn and starts next Turn.

Source:
Fires:
Throws:

If there are no players in the game

Returns:

The current Turn

Type
Turn

obtainEntityId() → {Number}

Returns a new Entity ID to use. This changes its value each time it's called.

Inherited From:
Source:
See:
Returns:

An integer that is used for Entity's ID

Type
Number

on(event, listener)

Registers a listener.

Parameters:
Name Type Description
event String

The name of the event.

listener function

The function to be triggered.

Inherited From:
Source:

once(event, listener)

Registers a one-time listener. The listener will be removed after being triggered once.

Parameters:
Name Type Description
event String

The name of the event.

listener function

The function to be triggered.

Inherited From:
Source:

registerComponent(key, constructor)

Registers a Component type to the Engine.

Parameters:
Name Type Description
key String

Component's string key.

constructor

{Function] - Component's constructor.

Inherited From:
Source:

registerComponentGroup(componentGroup) → {Array}

Registers the ComponentGroup to the Engine. Engine will check if an Entity matches ComponentGroup's criteria every time when a Entity is added or removed.

Parameters:
Name Type Description
componentGroup ComponentGroup

The ComponentGroup to register

Inherited From:
Source:
See:
Fires:
Returns:

An array holding Entity matching its criteria

Type
Array

removeAllEntities()

Removes all Entity from the Engine.

Inherited From:
Source:
Fires:

removeAllListeners(event)

Removes all the listener with specified event type. If event parameter is not set, it will remove all the listener of all types.

Parameters:
Name Type Argument Description
event String <optional>

The name of the event.

Inherited From:
Source:

removeEntity(entity)

Removes an Entity from the Engine.

Parameters:
Name Type Description
entity Entity

An Entity to remove

Inherited From:
Source:
Fires:

removeListener(event, listener)

Removes the listener.

Parameters:
Name Type Description
event String

The name of the event.

listener function

The function to be removed.

Inherited From:
Source:

removeSystem(key)

Removes the System from the Engine. This will trigger System#remove.

Parameters:
Name Type Description
key String

The System's string key to remove.

Inherited From:
Source:

runAction(action)

Runs the Action. Action shouldn't have run yet if ActionEngine#isServer is true, but it should have run on server and have Action#result if not.

Parameters:
Name Type Description
action Action

The Action to run.

Inherited From:
Source:
Fires:

s()

Executes System related functions by its arguments.

If a key is provided and the Engine doesn't have a system with that key, it returns a SystemBuilder. If a key is provided and the Engine has a system with that key, it returns that system. If a key is provided and a system is provided, it registers the system.

Inherited From:
Source:

<private> sortSystems()

Sorts the System array to match their priority.

Inherited From:
Source:
See:

toJSON() → {Object}

Serializes the Engine object. Note that systems won't be included in serialized object, so user should load the systems on their own.

Inherited From:
Source:
Returns:

serialized Engine object

Type
Object

update()

An update function called by every tick of the game.

Inherited From:
Source:

<private> updateComponentGroup(entity)

Updates the Entity to find if it matches ComponentGroups' criteria.

Parameters:
Name Type Description
entity Entity

the Entity to update

Inherited From:
Source:
See:

Events

action

This event is fired when the action executes.

Properties:
Name Type Description
0 Turn

The current Turn.

1 Action

The Action object.

Source:

entityAdded

This event is fired when a Entity has added to Engine.

Type:
Inherited From:
Source:

entityRemoved

This event is fired when a Entity has removed from Engine.

Type:
Inherited From:
Source:

gameInit

This event is fired when the game starts.

Type:
Source:

preAction

This event is fired before the action executes.

Properties:
Name Type Description
1 Action

The Action object.

Inherited From:
Source:

sequenceNext

This event is fired when the sequence changes.

Type:
Source:

turnNext

This event is fired when the turn changes.

Type:
Source: