StateMachine(owner)

Finite state machine (FSM) for implementing State-driven agent design.

new StateMachine(owner)

Constructs a new state machine with the given values.

Parameters:
Name Type Default Description
owner GameEntity null

The owner of this state machine.

Author:

Members

nullable currentState :State

The current state of the game entity.

Default Value:
  • null

nullable globalState :State

This state logic is called every time the state machine is updated.

Default Value:
  • null

nullable owner :GameEntity

The game entity that owns this state machine.

Default Value:
  • null

nullable previousState :State

The previous state of the game entity.

Default Value:
  • null

states :Map.<String, State>

A map with all states of the state machine.

Methods

add(id, state) → {StateMachine}

Adds a new state with the given ID to the state machine.

Parameters:
Name Type Description
id String

The ID of the state.

state State

The state.

Returns:
StateMachine -

A reference to this state machine.

changeTo(id) → {StateMachine}

Performs a state change to the state defined by its ID.

Parameters:
Name Type Description
id String

The ID of the state.

Returns:
StateMachine -

A reference to this state machine.

fromJSON(json) → {StateMachine}

Restores this instance from the given JSON object.

Parameters:
Name Type Description
json Object

The JSON object.

Returns:
StateMachine -

A reference to this state machine.

get(id) → {State}

Returns the state for the given ID.

Parameters:
Name Type Description
id String

The ID of the state.

Returns:
State -

The state for the given ID.

handleMessage(telegram) → {Boolean}

Tries to dispatch the massage to the current or global state and returns true if the message was processed successfully.

Parameters:
Name Type Description
telegram Telegram

The telegram with the message data.

Returns:
Boolean -

Whether the message was processed or not.

in() → {Boolean}

Returns true if this FSM is in the given state.

Returns:
Boolean -

Whether this FSM is in the given state or not.

registerType(type, constructor) → {StateMachine}

Registers a custom type for deserialization. When calling StateMachine#fromJSON the state machine is able to pick the correct constructor in order to create custom states.

Parameters:
Name Type Description
type String

The name of the state type.

constructor function

The constructor function.

Returns:
StateMachine -

A reference to this state machine.

remove(id) → {StateMachine}

Removes a state via its ID from the state machine.

Parameters:
Name Type Description
id String

The ID of the state.

Returns:
StateMachine -

A reference to this state machine.

resolveReferences(entities) → {StateMachine}

Restores UUIDs with references to GameEntity objects.

Parameters:
Name Type Description
entities Map.<String, GameEntity>

Maps game entities to UUIDs.

Returns:
StateMachine -

A reference to this state machine.

revert() → {StateMachine}

Returns to the previous state.

Returns:
StateMachine -

A reference to this state machine.

toJSON() → {Object}

Transforms this instance into a JSON object.

Returns:
Object -

The JSON object.

update() → {StateMachine}

Updates the internal state of the FSM. Usually called by GameEntity#update.

Returns:
StateMachine -

A reference to this state machine.