Clean up state/scheduling setup.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nolan Darilek 2023-04-03 09:08:23 -05:00
parent dff59fe9cc
commit 6eddde886e
2 changed files with 87 additions and 92 deletions

View File

@ -383,6 +383,9 @@ pub struct ExplorationPlugin<ExplorationType, State, MapData> {
pub map_data: PhantomData<MapData>,
}
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub struct Exploration;
impl<ExplorationType, State, MapData> Plugin for ExplorationPlugin<ExplorationType, State, MapData>
where
ExplorationType: 'static + Component + Default + Copy + Ord + PartialEq + Into<String>,
@ -398,38 +401,30 @@ where
.register_type::<Mappable>()
.register_type::<Explorable>()
.add_plugin(InputManagerPlugin::<ExplorationAction>::default())
.add_system(
exploration_changed_announcement::<ExplorationType, MapData>
.pipe(error_handler)
.in_base_set(CoreSet::PostUpdate),
);
if config.states.is_empty() {
app.add_systems((
exploration_focus::<State, MapData>,
exploration_type_focus::<ExplorationType, State>.pipe(error_handler),
exploration_type_change::<ExplorationType, State>.pipe(error_handler),
navigate_to_explored::<State, MapData>,
))
.add_system(
exploration_type_changed_announcement::<ExplorationType>
.pipe(error_handler)
.in_base_set(CoreSet::PostUpdate),
);
} else {
.add_systems(
(
exploration_type_change::<ExplorationType, State>.pipe(error_handler),
exploration_type_changed_announcement::<ExplorationType>.pipe(error_handler),
)
.chain()
.in_set(Exploration),
)
.add_systems(
(
exploration_focus::<State, MapData>,
exploration_type_focus::<ExplorationType, State>.pipe(error_handler),
exploration_changed_announcement::<ExplorationType, MapData>
.pipe(error_handler),
)
.chain()
.in_set(Exploration),
)
.add_system(navigate_to_explored::<State, MapData>.in_set(Exploration));
if !config.states.is_empty() {
let states = config.states;
for state in states {
app.add_systems(
(
exploration_focus::<State, MapData>,
exploration_type_focus::<ExplorationType, State>.pipe(error_handler),
exploration_type_change::<ExplorationType, State>.pipe(error_handler),
navigate_to_explored::<State, MapData>,
exploration_type_changed_announcement::<ExplorationType>
.pipe(error_handler),
)
.in_set(OnUpdate(state.clone())),
)
.add_system(cleanup.in_schedule(OnExit(state)));
app.configure_set(Exploration.in_set(OnUpdate(state.clone())))
.add_system(cleanup.in_schedule(OnExit(state)));
}
}
}

View File

@ -82,6 +82,58 @@ impl Default for Speed {
}
}
fn snap(
mut tts: ResMut<Tts>,
mut snap_timers: ResMut<SnapTimers>,
mut query: Query<
(
Entity,
&ActionState<NavigationAction>,
&mut Transform,
&CardinalDirection,
),
With<Player>,
>,
) -> Result<(), Box<dyn Error>> {
for (entity, actions, mut transform, direction) in &mut query {
if snap_timers.contains_key(&entity) {
continue;
} else if actions.just_pressed(NavigationAction::SnapLeft) {
snap_timers.insert(entity, SnapTimer::default());
transform.rotation = Quat::from_rotation_z(match direction {
CardinalDirection::North => PI,
CardinalDirection::East => PI / 2.,
CardinalDirection::South => 0.,
CardinalDirection::West => -PI / 2.,
});
} else if actions.just_pressed(NavigationAction::SnapRight) {
snap_timers.insert(entity, SnapTimer::default());
transform.rotation = Quat::from_rotation_z(match direction {
CardinalDirection::North => 0.,
CardinalDirection::East => -PI / 2.,
CardinalDirection::South => PI,
CardinalDirection::West => PI / 2.,
});
} else if actions.just_pressed(NavigationAction::SnapReverse) {
snap_timers.insert(entity, SnapTimer::default());
transform.rotate(Quat::from_rotation_z(PI));
} else if actions.just_pressed(NavigationAction::SnapCardinal) {
let yaw: Angle = direction.into();
let yaw = yaw.radians();
transform.rotation = Quat::from_rotation_z(yaw);
tts.speak(direction.to_string(), true)?;
}
}
Ok(())
}
fn tick_snap_timers(time: Res<Time>, mut snap_timers: ResMut<SnapTimers>) {
for timer in snap_timers.values_mut() {
timer.tick(time.delta());
}
snap_timers.retain(|_, v| !v.finished());
}
fn controls(
mut commands: Commands,
time: Res<Time>,
@ -229,57 +281,6 @@ fn controls(
}
}
}
fn snap(
mut tts: ResMut<Tts>,
mut snap_timers: ResMut<SnapTimers>,
mut query: Query<
(
Entity,
&ActionState<NavigationAction>,
&mut Transform,
&CardinalDirection,
),
With<Player>,
>,
) -> Result<(), Box<dyn Error>> {
for (entity, actions, mut transform, direction) in &mut query {
if snap_timers.contains_key(&entity) {
continue;
} else if actions.just_pressed(NavigationAction::SnapLeft) {
snap_timers.insert(entity, SnapTimer::default());
transform.rotation = Quat::from_rotation_z(match direction {
CardinalDirection::North => PI,
CardinalDirection::East => PI / 2.,
CardinalDirection::South => 0.,
CardinalDirection::West => -PI / 2.,
});
} else if actions.just_pressed(NavigationAction::SnapRight) {
snap_timers.insert(entity, SnapTimer::default());
transform.rotation = Quat::from_rotation_z(match direction {
CardinalDirection::North => 0.,
CardinalDirection::East => -PI / 2.,
CardinalDirection::South => PI,
CardinalDirection::West => PI / 2.,
});
} else if actions.just_pressed(NavigationAction::SnapReverse) {
snap_timers.insert(entity, SnapTimer::default());
transform.rotate(Quat::from_rotation_z(PI));
} else if actions.just_pressed(NavigationAction::SnapCardinal) {
let yaw: Angle = direction.into();
let yaw = yaw.radians();
transform.rotation = Quat::from_rotation_z(yaw);
tts.speak(direction.to_string(), true)?;
}
}
Ok(())
}
fn tick_snap_timers(time: Res<Time>, mut snap_timers: ResMut<SnapTimers>) {
for timer in snap_timers.values_mut() {
timer.tick(time.delta());
}
snap_timers.retain(|_, v| !v.finished());
}
fn update_direction(
mut commands: Commands,
@ -382,7 +383,7 @@ fn log_area_descriptions<State>(
}
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub struct MovementControls;
pub struct Movement;
#[derive(Resource, Clone, Debug)]
pub struct NavigationPlugin<State> {
@ -416,18 +417,17 @@ where
.add_plugin(InputManagerPlugin::<NavigationAction>::default())
.add_systems((update_direction, add_speed).in_base_set(CoreSet::PreUpdate))
.add_systems(
(remove_direction, log_area_descriptions::<State>).in_base_set(CoreSet::PostUpdate),
(snap.pipe(error_handler), controls)
.chain()
.in_set(Movement),
)
.add_systems((tick_snap_timers, speak_direction.pipe(error_handler)));
if self.states.is_empty() {
app.add_systems((controls.in_set(MovementControls), snap.pipe(error_handler)).chain());
} else {
.add_systems((tick_snap_timers, speak_direction.pipe(error_handler)))
.add_systems(
(remove_direction, log_area_descriptions::<State>).in_base_set(CoreSet::PostUpdate),
);
if !self.states.is_empty() {
for state in &self.states {
app.add_systems(
(controls.in_set(MovementControls), snap.pipe(error_handler))
.chain()
.in_set(OnUpdate(state.clone())),
);
app.configure_set(Movement.in_set(OnUpdate(state.clone())));
}
}
}