Crates.io | rsanim |
lib.rs | rsanim |
version | 0.3.0 |
source | src |
created_at | 2023-01-14 21:04:08.079708 |
updated_at | 2023-01-17 23:35:14.387544 |
description | A basic state machine for managing sprite animations |
homepage | |
repository | https://github.com/ironpeak/rsanim |
max_upload_size | |
id | 759038 |
size | 170,451 |
A basic state machine for managing sprite animations.
Example usage:
use rsanim::*;
use std::collections::HashMap;
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
enum Animation {
Idle,
Run,
}
#[derive(Clone, Debug, PartialEq)]
struct Params {
pub speed: f32,
}
let mut state_machine = StateMachine::new(
Animation::Idle,
HashMap::from([
(
Animation::Idle,
State {
duration: 0.5,
repeat: true,
},
),
(
Animation::Run,
State {
duration: 1.0,
repeat: true,
},
),
]),
vec![
Transition {
start_state: TransitionStartState::Node(Animation::Idle),
end_state: TransitionEndState::Node(Animation::Run),
trigger: TransitionTrigger::Condition(Box::new(|x: &Params| x.speed > 0.0)),
},
Transition {
start_state: TransitionStartState::Node(Animation::Run),
end_state: TransitionEndState::Node(Animation::Idle),
trigger: TransitionTrigger::Condition(Box::new(|x: &Params| x.speed <= 0.0)),
},
],
Params { speed: 0.0 },
)
.unwrap();
let animator = Animator::new(
state_machine,
HashMap::from([
(
Animation::Idle,
vec![
Frame {
value: 0,
progress: 0.00,
},
Frame {
value: 1,
progress: 0.33,
},
Frame {
value: 2,
progress: 0.67,
},
],
),
(
Animation::Run,
vec![
Frame {
value: 0,
progress: 0.00,
},
Frame {
value: 1,
progress: 0.33,
},
Frame {
value: 2,
progress: 0.67,
},
],
),
]),
)
.unwrap();
Update the state machine's elapsed time:
let delta_time = 0.1;
animator.update(delta_time);
Update the state machine's parameters that are used to determine conditional transitions:
animator.update_parameters(&|x| {
x.speed = 1.0;
});
See examples/bevy.rs
.