Crates.io | game_state_machine |
lib.rs | game_state_machine |
version | 1.0.0 |
source | src |
created_at | 2021-04-11 17:08:04.811815 |
updated_at | 2021-04-11 17:08:04.811815 |
description | A stack-based state machine with update functions. |
homepage | |
repository | https://github.com/jojolepro/game_state_machine/ |
max_upload_size | |
id | 382070 |
size | 19,313 |
Support an Open Source Developer! :hearts:
Read the documentation.
State
trait that is simple to implement.Add the following to you Cargo.toml file:
game_state_machine = "*"
Use it like so:
use game_state_machine::*;
type StateData = (isize, isize);
pub struct Test;
impl State<StateData> for Test {
fn on_start(&mut self, data: &mut StateData) {
data.0 += data.1;
}
fn on_resume(&mut self, data: &mut StateData) {
self.on_start(data);
}
fn update(&mut self, _data: &mut StateData) -> StateTransition<StateData> {
StateTransition::Push(Box::new(Test))
}
}
fn main() {
let mut sm = StateMachine::<StateData>::default();
let mut state_data = (0, 10);
sm.push(Box::new(Test), &mut state_data);
assert!(state_data.0 == 10);
sm.update(&mut state_data);
assert!(state_data.0 == 20);
sm.stop(&mut state_data);
assert!(state_data.0 == 20);
assert!(!sm.is_running())
}