Crate pyri_state

source ·
Expand description

pyri_state is a bevy_state alternative offering flexible change detection & scheduling.

NOTE: This crate is incompatible with the bevy/bevy_state feature, so make sure it’s disabled.

§Overview

  1. The current state is a Resource or Component that implements State.
  2. The next state is stored in a NextStateBuffer resource by default.
  3. A state flush is triggered by the TriggerStateFlush resource and handled in the StateFlush schedule.
  4. State flush hooks are organized into ResolveStateSet system sets.
  5. Tools are provided for state setup, access, pattern-matching, debugging, and more.

§Getting started

Import the prelude to bring traits and common types into scope:

use pyri_state::prelude::*;

Define your own State type using the derive macro:

#[derive(State, Clone, PartialEq, Eq, Default)]
struct Level(pub usize);

Add StatePlugin and initialize your state type:

app.add_plugins(StatePlugin).init_state::<Level>();

Add update systems with StatePattern::on_update:

app.add_systems(Update, (
    Level::ANY.on_update(update_level_timer),
    Level(10).on_update(update_boss_health_bar),
    state!(Level(4..=6)).on_update(spawn_enemy_waves),
));

Add flush hooks with other StatePattern methods:

app.add_systems(StateFlush, (
    // Short-hand for `on_exit` followed by `on_enter`.
    Level::ANY.on_edge(despawn_old_level, spawn_new_level),
    Level(10).on_enter(play_boss_music),
    state!(Level(4 | 7 | 10)).on_enter(save_checkpoint),
    Level::with(|x| x.0 < 4).on_enter(spawn_tutorial_popup),
));

Modules§

  • System parameters to access current and next states.
  • State debugging tools.
  • Extra tools behind feature flags.
  • NextState trait, extension traits and types.
  • State pattern-matching tools.
  • Re-exported traits and common types.
  • State flush scheduling types and functions.
  • State setup and configuration tools.
  • State trait and extension traits.

Macros§