StateMachineComponent
A state machine that can be attached to entities as a component.
StateMachineComponent extends StateMachine with the ability to be attached to entities as a component. It provides direct access to the parent entity and automatically manages its lifecycle as part of the entity.
This is useful when you want to add state machine behavior to existing entities without subclassing them.
Example usage:
// Define states for a player entity
enum PlayerState {
IDLE;
RUNNING;
JUMPING;
}
// Create a state machine component for the player
var playerStateMachine = new StateMachineComponent<PlayerState, Player>();
playerStateMachine.set(IDLE, new IdleState());
playerStateMachine.set(RUNNING, new RunningState());
playerStateMachine.set(JUMPING, new JumpingState());
// Attach to player entity
player.component(playerStateMachine);
// Alternatively, on entity fields marked as component, you can just write `StateMachine`
// as it will be automatically replaced by `StateMachineComponent` at compile time
public var machine:StateMachine<PlayerState, Player>;
// States can access the entity
class IdleState extends State {
override function update(delta:Float) {
var player = cast(machine, StateMachineComponent<PlayerState, Player>).entity;
if (player.velocity.x != 0) {
machine.state = RUNNING;
}
}
}
Instance Members
The entity this state machine is attached to. Set automatically when added as a component.
new(): Void
Private Members
Name | Type |
---|---|
entity |
Entity |
getEntity(): Entity
Returns |
---|
Entity |
Metadata
Name | Parameters |
---|---|
:build |
ceramic.macros.ComponentMacro.build() |
:autoBuild |
ceramic.macros.ComponentMacro.build() |
:build |
tracker.macros.ObservableMacro.build() |
:autoBuild |
tracker.macros.ObservableMacro.build() |
:build |
ceramic.macros.EntityMacro.buildForCompletion() |
:autoBuild |
ceramic.macros.EntityMacro.buildForCompletion() |
:build |
tracker.macros.EventsMacro.build() |
:autoBuild |
tracker.macros.EventsMacro.build() |