game_state_machine 1.0.0

A stack-based state machine with update functions.
Documentation
  • Coverage
  • 100%
    18 out of 18 items documented0 out of 12 items with examples
  • Size
  • Source code size: 18.95 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.59 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • AnneKitsune/game_state_machine
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • AnneKitsune

Game State Machine

Support an Open Source Developer! :hearts:
Become a patron

Read the documentation.

Features

  • State trait that is simple to implement.
  • Generic stack-based state machine, for all your needs.
  • State update functions.
  • State pause and unpause.

Usage

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())
}

Maintainer Information

  • Maintainer: Jojolepro
  • Contact: jojolepro [at] jojolepro [dot] com
  • Website: jojolepro.com
  • Patreon: patreon