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. |
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.
-
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.
-
fromJSON(json) → {StateMachine}
-
Restores this instance from the given JSON object.
Parameters:
Name Type Description json
Object The JSON object.
-
get(id) → {State}
-
Returns the state for the given ID.
Parameters:
Name Type Description id
String The ID of the state.
-
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.
-
remove(id) → {StateMachine}
-
Removes a state via its ID from the state machine.
Parameters:
Name Type Description id
String The ID of the state.
-
resolveReferences(entities) → {StateMachine}
-
Restores UUIDs with references to GameEntity objects.
Parameters:
Name Type Description entities
Map.<String, GameEntity> Maps game entities to UUIDs.
-
revert() → {StateMachine}
-
Returns to the previous state.
-
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.