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

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

  • Always

    Always 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(
    id: String,
    animation: object3d.Animation,
    is_looping: Bool,
  )
}

Constructors

An animation state machine

pub opaque type StateMachine(ctx)

The current state of a running state machine

pub type StateMachineState {
  Playing(state: String, elapsed: Float)
  Blending(
    from: String,
    to: String,
    blend_progress: Float,
    blend_duration: Float,
  )
}

Constructors

  • Playing(state: String, elapsed: Float)

    Playing a single state

  • Blending(
      from: String,
      to: String,
      blend_progress: Float,
      blend_duration: Float,
    )

    Blending between two states

Transition between two states

pub type Transition(ctx) {
  Transition(
    from: String,
    to: String,
    condition: Condition(ctx),
    blend_duration: Float,
  )
}

Constructors

  • Transition(
      from: String,
      to: String,
      condition: Condition(ctx),
      blend_duration: Float,
    )

Values

pub fn add_state(
  machine: StateMachine(ctx),
  id: String,
  animation: object3d.Animation,
  looping looping: Bool,
) -> StateMachine(ctx)

Add a state to the state machine

pub fn add_transition(
  machine: StateMachine(ctx),
  from from: String,
  to to: String,
  condition condition: Condition(ctx),
  blend_duration blend_duration: Float,
) -> StateMachine(ctx)

Add a transition between two states

pub fn blend_progress(
  machine: StateMachine(ctx),
) -> option.Option(Float)

Get blend progress (0.0 to 1.0) if blending

pub fn current_state(machine: StateMachine(ctx)) -> String

Get the current state ID

pub fn get_current_animation(
  machine: StateMachine(ctx),
) -> AnimationOutput

Get the current animation(s) to play Returns a single animation or blend information

pub fn get_state(
  machine: StateMachine(ctx),
  id: String,
) -> Result(State, Nil)

Get a state by ID

pub fn is_blending(machine: StateMachine(ctx)) -> Bool

Check if currently blending

pub fn new(initial_state: String) -> StateMachine(ctx)

Create a new state machine with a starting state

pub fn set_default_blend(
  machine: StateMachine(ctx),
  duration: Float,
) -> StateMachine(ctx)

Set the default blend duration for transitions

pub fn state_count(machine: StateMachine(ctx)) -> Int

Get the number of states

pub fn state_ids(machine: StateMachine(ctx)) -> List(String)

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(ctx)) -> Int

Get the number of transitions

pub fn transition_to(
  machine: StateMachine(ctx),
  target: String,
  blend_duration: option.Option(Float),
) -> StateMachine(ctx)

Manually trigger a transition to a specific state

pub fn update(
  machine: StateMachine(ctx),
  context: ctx,
  delta_time: Float,
) -> #(StateMachine(ctx), Bool)

Update the state machine (should be called every frame) Returns updated state machine and whether a transition occurred

Search Document