#[macro_export]
macro_rules! game_state {
($state:ident$(, [$($collection:ident),*])?) => {
LoadingState::new($state::Loading)
$($(.load_collection::<$collection>())*)?
.continue_to_state($state::Loaded)
};
(inner: pub enum $state:ident {$($tt:tt)*} $(, [$($collection:ident),*])?) => {
::paste::paste! {
#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash, SubStates)]
#[source([< Loading $state >] = [ < Loading $state > ]::Loaded)]
pub enum $state {$($tt)*}
}
};
(pub enum $state:ident {$($tt:tt)*} $(, [$($collection:ident),*])?) => {
::paste::paste! {
#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash, States)]
pub enum [< Loading $state >] {
#[default]
Loading,
Loaded
}
}
$crate::game_state! { inner: pub enum $state { $($tt)* } }
::paste::paste! {
pub trait [< $state Ext >] {
fn [< init_ $state:snake >](&mut self) -> &mut Self;
}
impl [< $state Ext >] for bevy::prelude::App {
fn [< init_ $state:snake >](&mut self) -> &mut Self {
self
.add_sub_state::<$state>()
.init_state::< [< Loading $state >] >()
.add_loading_state($crate::game_state!([< Loading $state >]$(, [$($collection),*])? ))
}
}
}
};
(source: $source_state:ident = $source_variant:expr, pub enum $state:ident {$($tt:tt)*} $(, [$($collection:ident),*])?) => {
::paste::paste! {
#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash, SubStates)]
#[source($source_state = $source_variant)]
pub enum [< Loading $state >] {
#[default]
Loading,
Loaded
}
}
$crate::game_state! { inner: pub enum $state { $($tt)* } }
::paste::paste! {
pub trait [< $state Ext >] {
fn [< init_ $state:snake >](&mut self) -> &mut Self;
fn [< add_in_ $state:snake _systems>]<M>(
&mut self,
schedule: impl ::bevy::ecs::schedule::ScheduleLabel,
systems: impl ::bevy::prelude::IntoScheduleConfigs<::bevy::ecs::system::ScheduleSystem, M>,
) -> &mut Self;
fn [< add_on_enter_ $state:snake _systems >]<M>(
&mut self,
systems: impl ::bevy::prelude::IntoScheduleConfigs<::bevy::ecs::system::ScheduleSystem, M>,
) -> &mut Self;
fn [< add_on_exit_ $state:snake _systems >]<M>(
&mut self,
systems: impl ::bevy::prelude::IntoScheduleConfigs<::bevy::ecs::system::ScheduleSystem, M>,
) -> &mut Self;
}
impl [< $state Ext >] for bevy::prelude::App {
fn [< init_ $state:snake >](&mut self) -> &mut Self {
self
.add_sub_state::<$state>()
.add_sub_state::< [< Loading $state >] >()
.add_loading_state($crate::game_state!([< Loading $state >]$(, [$($collection),*])? ))
}
fn [< add_in_ $state:snake _systems>]<M>(
&mut self,
schedule: impl ::bevy::ecs::schedule::ScheduleLabel,
systems: impl ::bevy::prelude::IntoScheduleConfigs<::bevy::ecs::system::ScheduleSystem, M>,
) -> &mut Self {
use $crate::state::AppStateExt;
self.add_in_state_systems(schedule, systems, [< Loading $state >]::Loaded)
}
fn [< add_on_enter_ $state:snake _systems >]<M>(
&mut self,
systems: impl ::bevy::prelude::IntoScheduleConfigs<::bevy::ecs::system::ScheduleSystem, M>,
) -> &mut Self {
use $crate::state::AppStateExt;
self.add_on_enter_state_systems(systems, [< Loading $state >]::Loaded)
}
fn [< add_on_exit_ $state:snake _systems >]<M>(
&mut self,
systems: impl ::bevy::prelude::IntoScheduleConfigs<::bevy::ecs::system::ScheduleSystem, M>,
) -> &mut Self {
use $crate::state::AppStateExt;
self.add_on_exit_state_systems(systems, [< Loading $state >]::Loaded)
}
}
}
};
}
pub use crate::game_state;