2025-08-24 09:42:18 -04:00
|
|
|
//bevy system signatures often violate these rules
|
|
|
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
|
|
|
|
|
|
use avian3d::prelude::*;
|
|
|
|
|
use bevy::prelude::*;
|
|
|
|
|
|
|
|
|
|
pub struct StatePlugin;
|
|
|
|
|
|
|
|
|
|
impl Plugin for StatePlugin {
|
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
app.init_state::<AppState>()
|
|
|
|
|
.enable_state_scoped_entities::<AppState>()
|
|
|
|
|
.add_sub_state::<GameState>()
|
2025-09-01 12:08:39 -04:00
|
|
|
.add_sub_state::<BasePhaseState>()
|
2025-08-24 09:42:18 -04:00
|
|
|
.enable_state_scoped_entities::<GameState>()
|
2025-09-01 12:08:39 -04:00
|
|
|
.add_computed_state::<Phase>()
|
2025-08-24 09:42:18 -04:00
|
|
|
.configure_sets(
|
|
|
|
|
FixedUpdate,
|
|
|
|
|
GameSystemSet::LoadingTick.run_if(in_state(GameState::Loading)),
|
|
|
|
|
)
|
|
|
|
|
.configure_sets(
|
|
|
|
|
FixedUpdate,
|
|
|
|
|
(
|
|
|
|
|
// these all run before physics, which is in FixedPostUpdate by default
|
|
|
|
|
GameSystemSet::PreTick,
|
2025-08-25 08:31:33 -04:00
|
|
|
(
|
|
|
|
|
GameSystemSet::PrePlan.run_if(in_state(Phase::Plan)),
|
|
|
|
|
GameSystemSet::PreExecute.run_if(in_state(Phase::Execute)),
|
|
|
|
|
),
|
2025-08-24 09:42:18 -04:00
|
|
|
GameSystemSet::Tick,
|
2025-08-25 08:31:33 -04:00
|
|
|
(
|
|
|
|
|
GameSystemSet::Plan.run_if(in_state(Phase::Plan)),
|
|
|
|
|
GameSystemSet::Execute.run_if(in_state(Phase::Execute)),
|
|
|
|
|
),
|
2025-08-26 08:16:40 -04:00
|
|
|
GameSystemSet::StatusEffects,
|
|
|
|
|
GameSystemSet::DealDamage,
|
2025-08-24 09:42:18 -04:00
|
|
|
GameSystemSet::PostTick,
|
2025-08-25 08:31:33 -04:00
|
|
|
(
|
|
|
|
|
GameSystemSet::PostPlan.run_if(in_state(Phase::Plan)),
|
|
|
|
|
GameSystemSet::PostExecute.run_if(in_state(Phase::Execute)),
|
|
|
|
|
),
|
2025-08-24 09:42:18 -04:00
|
|
|
)
|
|
|
|
|
.run_if(in_state(GameState::Running))
|
|
|
|
|
.chain(),
|
|
|
|
|
)
|
|
|
|
|
.configure_sets(
|
|
|
|
|
Update,
|
|
|
|
|
(
|
|
|
|
|
GameSystemSet::UpdateInputs,
|
2025-08-24 14:46:57 -04:00
|
|
|
GameSystemSet::UI,
|
2025-08-24 09:42:18 -04:00
|
|
|
(
|
|
|
|
|
GameSystemSet::PreUpdate,
|
|
|
|
|
GameSystemSet::Update,
|
|
|
|
|
GameSystemSet::PostUpdate,
|
|
|
|
|
)
|
2025-08-24 14:46:57 -04:00
|
|
|
.run_if(in_state(GameState::Running))
|
2025-08-24 09:42:18 -04:00
|
|
|
.chain(),
|
|
|
|
|
)
|
|
|
|
|
.chain(),
|
|
|
|
|
)
|
|
|
|
|
.add_systems(OnEnter(GameState::Running), start_physics)
|
|
|
|
|
.add_systems(OnExit(GameState::Running), stop_physics);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(States, Default, Debug, Hash, PartialEq, Eq, Clone)]
|
|
|
|
|
pub enum AppState {
|
|
|
|
|
#[default]
|
|
|
|
|
Setup,
|
|
|
|
|
Menu,
|
|
|
|
|
Game,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, SubStates, Default)]
|
|
|
|
|
#[source(AppState = AppState::Game)]
|
|
|
|
|
pub enum GameState {
|
|
|
|
|
Loading,
|
|
|
|
|
#[default]
|
|
|
|
|
Running,
|
|
|
|
|
Paused,
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 12:08:39 -04:00
|
|
|
/// this is what you should set to change the state. do not read this for scheduling
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, SubStates)]
|
|
|
|
|
#[source(AppState = AppState::Game)]
|
|
|
|
|
pub enum BasePhaseState {
|
|
|
|
|
Execute { phase: u32 },
|
|
|
|
|
Plan { phase: u32 },
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for BasePhaseState {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::Plan { phase: 0 }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BasePhaseState {
|
|
|
|
|
pub fn phase_count(&self) -> u32 {
|
|
|
|
|
match self {
|
|
|
|
|
BasePhaseState::Execute { phase } => *phase,
|
|
|
|
|
BasePhaseState::Plan { phase } => *phase,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// this is what you should read, as it takes into account the game state
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
2025-08-25 08:31:33 -04:00
|
|
|
pub enum Phase {
|
|
|
|
|
Execute,
|
|
|
|
|
Plan,
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 12:08:39 -04:00
|
|
|
impl ComputedStates for Phase {
|
|
|
|
|
type SourceStates = (BasePhaseState, GameState);
|
|
|
|
|
|
|
|
|
|
fn compute(sources: Self::SourceStates) -> Option<Self> {
|
|
|
|
|
match sources {
|
|
|
|
|
(_, GameState::Loading) | (_, GameState::Paused) => None,
|
|
|
|
|
(BasePhaseState::Execute { .. }, GameState::Running) => Some(Self::Execute),
|
|
|
|
|
(BasePhaseState::Plan { .. }, GameState::Running) => Some(Self::Plan),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 09:42:18 -04:00
|
|
|
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
|
|
|
|
|
pub enum GameSystemSet {
|
2025-08-25 08:31:33 -04:00
|
|
|
// fixed update - contains all gameplay logic. does not run when game is paused
|
|
|
|
|
LoadingTick,
|
|
|
|
|
PreTick,
|
|
|
|
|
PreExecute,
|
|
|
|
|
PrePlan,
|
|
|
|
|
Tick,
|
|
|
|
|
Execute,
|
|
|
|
|
Plan,
|
2025-08-26 08:16:40 -04:00
|
|
|
StatusEffects,
|
|
|
|
|
DealDamage,
|
2025-08-25 08:31:33 -04:00
|
|
|
PostTick,
|
|
|
|
|
PostExecute,
|
|
|
|
|
PostPlan,
|
2025-08-24 09:42:18 -04:00
|
|
|
// runs even if game is not loaded/active
|
|
|
|
|
UpdateInputs,
|
2025-08-24 14:46:57 -04:00
|
|
|
UI,
|
|
|
|
|
// update - cosmetic only. does not run while game is paused
|
2025-08-24 09:42:18 -04:00
|
|
|
PreUpdate,
|
|
|
|
|
Update,
|
|
|
|
|
PostUpdate,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn start_physics(mut time: ResMut<Time<Physics>>) {
|
|
|
|
|
time.unpause();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stop_physics(mut time: ResMut<Time<Physics>>) {
|
|
|
|
|
time.pause();
|
|
|
|
|
}
|