tiramisu/state_machine
Types
Output from the state machine
pub type AnimationOutput {
None
Single(object3d.Animation)
Blend(
from: object3d.Animation,
to: object3d.Animation,
factor: Float,
)
}
Constructors
-
NoneNo animation
-
Single(object3d.Animation)Play a single animation
-
Blend( from: object3d.Animation, to: object3d.Animation, factor: Float, )Blend between two animations
Condition for transitioning between states
The generic parameter ctx allows you to pass context (like GameContext) to custom conditions
pub type Condition(ctx) {
Always
AfterDuration(Float)
Custom(fn(ctx) -> Bool)
}
Constructors
-
AlwaysAlways transition (immediate)
-
AfterDuration(Float)Transition after a duration (seconds)
-
Custom(fn(ctx) -> Bool)Custom condition function that receives context
An animation state with its configuration
pub type State(state) {
State(
id: state,
animation: object3d.Animation,
is_looping: Bool,
)
}
Constructors
-
State(id: state, animation: object3d.Animation, is_looping: Bool)
An animation state machine
pub opaque type StateMachine(state, ctx)
The current state of a running state machine
pub type StateMachineState(state) {
Playing(state: state, elapsed: Float)
Blending(
from: state,
to: state,
blend_progress: Float,
blend_duration: Float,
)
}
Constructors
-
Playing(state: state, elapsed: Float)Playing a single state
-
Blending( from: state, to: state, blend_progress: Float, blend_duration: Float, )Blending between two states
Values
pub fn add_state(
machine: StateMachine(state, ctx),
id: state,
animation: object3d.Animation,
looping looping: Bool,
) -> StateMachine(state, ctx)
Add a state to the state machine
pub fn add_transition(
machine: StateMachine(state, ctx),
from from: state,
to to: state,
condition condition: Condition(ctx),
blend_duration blend_duration: Float,
) -> StateMachine(state, ctx)
Add a transition between two states
pub fn blend_progress(
machine: StateMachine(state, ctx),
) -> option.Option(Float)
Get blend progress (0.0 to 1.0) if blending
pub fn current_state(machine: StateMachine(state, ctx)) -> state
Get the current state ID
pub fn get_current_animation(
machine: StateMachine(state, ctx),
) -> AnimationOutput
Get the current animation(s) to play Returns a single animation or blend information
pub fn get_state(
machine: StateMachine(state, ctx),
id: state,
) -> Result(State(state), Nil)
Get a state by ID
pub fn is_blending(machine: StateMachine(state, ctx)) -> Bool
Check if currently blending
pub fn new(initial_state: state) -> StateMachine(state, ctx)
Create a new state machine with a starting state
pub fn set_default_blend(
machine: StateMachine(state, ctx),
duration: Float,
) -> StateMachine(state, ctx)
Set the default blend duration for transitions
pub fn state_count(machine: StateMachine(state, ctx)) -> Int
Get the number of states
pub fn state_ids(
machine: StateMachine(state, ctx),
) -> List(state)
Get all state IDs
pub fn to_playback(
output: AnimationOutput,
) -> option.Option(object3d.AnimationPlayback)
Convert AnimationOutput to AnimationPlayback for use with Model3D
pub fn transition_count(machine: StateMachine(state, ctx)) -> Int
Get the number of transitions
pub fn transition_to(
machine: StateMachine(state, ctx),
target: state,
blend_duration: option.Option(Float),
) -> StateMachine(state, ctx)
Manually trigger a transition to a specific state
pub fn update(
machine: StateMachine(state, ctx),
context: ctx,
delta_time: Float,
) -> #(StateMachine(state, ctx), Bool)
Update the state machine (should be called every frame) Returns updated state machine and whether a transition occurred