phase -> execute loop

This commit is contained in:
Jim 2025-09-01 12:08:39 -04:00
parent 83256bfbe5
commit 6c0ac7f66d
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
7 changed files with 273 additions and 35 deletions

View file

@ -12,8 +12,9 @@ impl Plugin for StatePlugin {
app.init_state::<AppState>()
.enable_state_scoped_entities::<AppState>()
.add_sub_state::<GameState>()
.add_sub_state::<BasePhaseState>()
.enable_state_scoped_entities::<GameState>()
.add_sub_state::<Phase>()
.add_computed_state::<Phase>()
.configure_sets(
FixedUpdate,
GameSystemSet::LoadingTick.run_if(in_state(GameState::Loading)),
@ -80,14 +81,48 @@ pub enum GameState {
Paused,
}
#[derive(Debug, Clone, Eq, PartialEq, Hash, SubStates, Default)]
#[source(GameState = GameState::Running)]
/// 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)]
pub enum Phase {
#[default]
Execute,
Plan,
}
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),
}
}
}
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
pub enum GameSystemSet {
// fixed update - contains all gameplay logic. does not run when game is paused