GameEngine

GameEngine

The GameEngine contains the game logic. Extend this class to implement game mechanics. The GameEngine derived instance runs once on the server, where the final decisions are always taken, and one instance will run on each client as well, where the client emulates what it expects to be happening on the server.

The game engine's logic must listen to user inputs and act on these inputs to change the game state. For example, the game engine listens to controller/keyboard inputs to infer movement for the player/ship/first-person. The game engine listens to clicks, button-presses to infer firing, etc..

Note that the game engine runs on both the server and on the clients - but the server decisions always have the final say, and therefore clients must resolve server updates which conflict with client-side predictions.

Constructor

new GameEngine(options)

Create a game engine instance. This needs to happen once on the server, and once on each client.

Parameters:
Name Type Description
options Object

options object

Properties
Name Type Description
traceLevel Number

the trace level.

Source:

Members

(static) worldSettings :Object

The worldSettings defines the game world constants, such as width, height, depth, etc. such that all other classes can reference these values.

Source:

playerId :String

client's player ID, as a string. If running on the client, this is set at runtime by the clientEngine

Source:

Methods

addObjectToWorld(object) → {Object}

Add object to the game world. On the client side, the object may not be created, if the server copy of this object is already in the game world. This could happen when the client is using delayed-input, and the RTT is very low.

Parameters:
Name Type Description
object Object

the object.

Source:
Returns:

the final object.

Type
Object

getPlayerGameOverResult() → {Object}

Decide whether the player game is over by returning an Object, need to be implemented

Source:
Returns:

truthful if the game is over for the player and the object is returned as GameOver data

Type
Object

isOwnedByPlayer(object) → {Boolean}

Check if a given object is owned by the player on this client

Parameters:
Name Type Description
object Object

the game object to check

Source:
Returns:

true if the game object is owned by the player on this client

Type
Boolean

on(eventName, eventHandler)

Register a handler for an event

Parameters:
Name Type Description
eventName String

name of the event

eventHandler function

handler function

Source:

once(eventName, eventHandler)

Register a handler for an event, called just once (if at all)

Parameters:
Name Type Description
eventName String

name of the event

eventHandler function

handler function

Source:

processInput(inputDesc, playerId, isServer)

Override this function to implement input handling. This method will be called on the specific client where the input was received, and will also be called on the server when the input reaches the server. The client does not call this method directly, rather the client calls ClientEngine#sendInput so that the input is sent to both server and client, and so that the input is delayed artificially if so configured.

The input is described by a short string, and is given an index. The index is used internally to keep track of inputs which have already been applied on the client during synchronization. The input is also associated with the ID of a player.

Parameters:
Name Type Description
inputDesc Object

input descriptor object

Properties
Name Type Description
input String

describe the input (e.g. "up", "down", "fire")

messageIndex Number

input identifier

step Number

the step on which this input occurred

playerId Number

the player ID

isServer Boolean

indicate if this function is being called on the server side

Source:

registerClasses(serializer)

Register Game Object Classes

Parameters:
Name Type Description
serializer Serializer

the serializer

Source:
Example
registerClasses(serializer) {
  serializer.registerClass(Paddle);
  serializer.registerClass(Ball);
}

removeListener(eventName, eventHandler)

Remove a handler

Parameters:
Name Type Description
eventName String

name of the event

eventHandler function

handler function

Source:

removeObjectFromWorld(objectId)

Remove an object from the game world.

Parameters:
Name Type Description
objectId Object | String

the object or object ID

Source:

start()

Start the game. This method runs on both server and client. Extending the start method is useful for setting up the game's worldSettings attribute, and registering methods on the event handler.

Source:

step(isReenact, t, dt, physicsOnly)

Single game step.

Parameters:
Name Type Description
isReenact Boolean

is this step a re-enactment of the past.

t Number

the current time (optional)

dt Number

elapsed time since last step was called. (optional)

physicsOnly Boolean

do a physics step only, no game logic

Source:

Events

client__postStep

Marks the end of a game step on the client

Source:

client__preStep

Marks the beginning of a game step on the client

Source:

client__processInput

An input needs to be handled. This event is emitted on the client only, just before the general processInput event.

Parameters:
Name Type Description
input Object

input descriptor object

Properties
Name Type Description
input String

describe the input (e.g. "up", "down", "fire")

messageIndex Number

input identifier

options Object

the object which was passed as SendInput's InputOptions parameter

step Number

input execution step

playerId Number

the player ID

Source:

client__roomUpdate

Client moved from one room to another

Parameters:
Name Type Description
playerId Number

the player ID

from String

the room from which the client came

to String

the room to which the client went

Source:

client__slowFrameRate

Report slow frame rate on the browser. The browser did not achieve a reasonable frame rate

Source:

client__stepReset

Client reset the world step

Parameters:
Name Type Description
resetDesc Object

sync from the server

oldStep Number

the old step count

newStep Number

the new step count

Source:

client__syncReceived

Client received a sync from the server

Parameters:
Name Type Description
sync Object

sync from the server

syncEvents Array

array of events in the sync

maxStepCount Number

highest step in the sync

Source:

objectAdded

An object has been added to the world

Parameters:
Name Type Description
obj Object

the new object

Source:

objectDestroyed

An object has been removed from the world

Parameters:
Name Type Description
obj Object

the object

Source:

playerDisconnected

A player has left

Parameters:
Name Type Description
joinTime Number

epoch of join time

disconnectTime Number

epoch of disconnect time

playerDesc Object

player descriptor

Properties
Name Type Description
playerId String

the player ID

Source:

playerJoined

A player has joined

Parameters:
Name Type Description
joinTime Number

epoch of join time

playerDesc Object

player descriptor

Properties
Name Type Description
playerId String

the player ID

Source:

postStep

Marks the end of a game step

Parameters:
Name Type Description
stepNumber Number

the step number

isReenact Boolean

is this step a re-enactment

Source:

preStep

Marks the beginning of a new game step

Parameters:
Name Type Description
stepNumber Number

the step number

isReenact Boolean

is this step a re-enactment

Source:

processInput

An input needs to be handled. Emitted just before the GameEngine method processInput is invoked.

Parameters:
Name Type Description
input Object

input descriptor object

Properties
Name Type Description
input String

describe the input (e.g. "up", "down", "fire")

messageIndex Number

input identifier

options Object

the object which was passed as SendInput's InputOptions parameter

step Number

input execution step

playerId Number

the player ID

Source:

server__inputReceived

User input received on the server

Parameters:
Name Type Description
input Object

input descriptor

Properties
Name Type Description
data Object

input descriptor

playerId String

player that sent the input

Source:

server__playerDisconnected

A player has left on the server

Parameters:
Name Type Description
joinTime Number

epoch of join time

disconnectTime Number

epoch of disconnect time

playerDesc Object

player descriptor

Properties
Name Type Description
playerId String

the player ID

Source:

server__playerJoined

A player has joined on the server

Parameters:
Name Type Description
joinTime Number

epoch of join time

playerDesc Object

player descriptor

Properties
Name Type Description
playerId String

the player ID

Source:

server__postStep

Marks the end of a game step on the server

Parameters:
Name Type Description
stepNumber Number

the step number

Source:

server__preStep

Marks the beginning of a game step on the server

Parameters:
Name Type Description
stepNumber Number

the step number

Source:

server__processInput

An input needs to be handled. This event is emitted on the server only, just before the general processInput event.

Parameters:
Name Type Description
input Object

input descriptor object

Properties
Name Type Description
input String

describe the input (e.g. "up", "down", "fire")

messageIndex Number

input identifier

options Object

the object which was passed as SendInput's InputOptions parameter

step Number

input execution step

playerId Number

the player ID

Source:

server__roomUpdate

Client moved from one room to another

Parameters:
Name Type Description
playerId Number

the player ID

from String

the room from which the client came

to String

the room to which the client went

Source:

start

server has started

Parameters:
Name Type Description
timestamp Number

UTC epoch of start time

Source:

syncReceived

A synchronization update arrived from the server

Parameters:
Name Type Description
sync Object

the synchronization object

Source: