use bevy::prelude::*;
use pyri_state::{
debug::log_flush::LogFlushPlugin,
extra::{bevy_state::BevyStatePlugin, react::ReactPlugin},
prelude::*,
schedule::{
apply_flush::ApplyFlushPlugin, detect_change::DetectChangePlugin,
flush_message::FlushMessagePlugin, resolve_state::ResolveStatePlugin,
},
setup::RegisterState,
};
fn main() -> AppExit {
App::new()
.add_plugins((DefaultPlugins, StatePlugin))
.add_state::<BasicState>()
.add_state::<RawState>()
.add_state::<CustomState>()
.run()
}
#[derive(State, Reflect, Clone, PartialEq, Eq)]
#[reflect(Resource)]
struct BasicState;
#[derive(State, Reflect)]
#[state(no_defaults)]
#[reflect(Resource)]
struct RawState;
#[derive(State, Reflect, Clone, PartialEq, Eq, Hash, Debug)]
#[state(
// Disable default plugins: detect_change, flush_message, apply_flush.
no_defaults,
// Trigger a flush on any state change (requires PartialEq, Eq).
detect_change,
// Write a message on flush (requires Clone).
flush_message,
// Log on flush (requires Debug).
log_flush,
// Include a `BevyState<Self>` wrapper (requires StateMut, Clone, PartialEq, Eq, Hash, Debug).
// (see `ecosystem_compatibility` example for more information)
bevy_state,
// Enable reaction components such as `DespawnOnExitState<Self>` (requires Eq).
react,
// Clone the next state into the current state on flush (requires Clone).
apply_flush,
// Swap out the default `NextStateBuffer<Self>` for another `NextState` type.
// (see `custom_next_state` example for more information)
next(NextStateStack<Self>),
// Run this state's on-flush hooks after the listed states.
after(BasicState, RawState),
// Run this state's on-flush hooks before the listed states.
before(CustomState),
)]
#[reflect(Resource)]
struct DerivedState;
#[derive(Resource, Reflect, Clone, PartialEq, Eq, Hash, Debug)]
#[reflect(Resource)]
struct CustomState;
impl State for CustomState {
type Next = NextStateBuffer<Self>;
}
impl RegisterState for CustomState {
fn register_state(app: &mut App) {
app.add_plugins((
ResolveStatePlugin::<Self>::default()
.after::<BasicState>()
.after::<RawState>()
.after::<DerivedState>(),
DetectChangePlugin::<Self>::default(),
FlushMessagePlugin::<Self>::default(),
LogFlushPlugin::<Self>::default(),
BevyStatePlugin::<Self>::default(),
ReactPlugin::<Self>::default(),
ApplyFlushPlugin::<Self>::default(),
));
}
}