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

@ -14,20 +14,42 @@ use bevy::{
use bevy_egui::PrimaryEguiContext;
use building::{BuildingTypes, PlaceBuilding};
use camera::ControlledCamera;
use inhabitant::Inhabitant;
use inhabitant_coranth::SpawnCoranth;
use physics::{Ground, PhysicsLayers};
use resource_graveyard::PlaceGraveyard;
use state::{AppState, GameSystemSet};
use state::{AppState, BasePhaseState, GameSystemSet, Phase};
pub struct LevelPlugin;
impl Plugin for LevelPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(AppState::Game), setup)
.add_systems(FixedUpdate, spawn_enemies.in_set(GameSystemSet::Execute));
app.init_resource::<ExecuteTimer>()
.add_systems(OnEnter(AppState::Game), setup)
.add_systems(
OnTransition {
exited: Phase::Execute,
entered: Phase::Plan,
},
on_plan,
)
.add_systems(
OnTransition {
exited: Phase::Plan,
entered: Phase::Execute,
},
on_execute,
)
.add_systems(
FixedUpdate,
(spawn_enemies, tick_execute_timer).in_set(GameSystemSet::Execute),
);
}
}
#[derive(Resource, Default, Deref, DerefMut)]
pub struct ExecuteTimer(pub Option<Timer>);
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
@ -123,3 +145,30 @@ fn spawn_enemies(
}
}
}
fn on_execute(mut timer: ResMut<ExecuteTimer>) {
timer.0 = Some(Timer::from_seconds(5., TimerMode::Once));
}
fn tick_execute_timer(
mut execute_timer: ResMut<ExecuteTimer>,
time: Res<Time>,
curr_phase: Res<State<BasePhaseState>>,
mut next_phase: ResMut<NextState<BasePhaseState>>,
) {
if let Some(timer) = &mut execute_timer.0 {
timer.tick(time.delta());
if timer.finished() {
execute_timer.0 = None;
next_phase.set(BasePhaseState::Plan {
phase: curr_phase.get().phase_count() + 1,
});
}
}
}
fn on_plan(query: Query<Entity, With<Inhabitant>>, mut commands: Commands) {
for entity in query.iter() {
commands.entity(entity).despawn();
}
}