Skip to content

Commit

Permalink
simple out of bounds checking
Browse files Browse the repository at this point in the history
  • Loading branch information
doomy committed Dec 14, 2023
1 parent ce863d0 commit 3accd6e
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 56 deletions.
4 changes: 2 additions & 2 deletions assets/levels/level_1.gltf
Git LFS file not shown
4 changes: 2 additions & 2 deletions assets/levels/level_4.gltf
Git LFS file not shown
4 changes: 2 additions & 2 deletions assets/levels/level_5.gltf
Git LFS file not shown
4 changes: 2 additions & 2 deletions assets/library.blend
Git LFS file not shown
2 changes: 1 addition & 1 deletion src/components/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl OrbitCamera {
impl Default for OrbitCamera {
fn default() -> Self {
Self {
rotation: Default::default(),
rotation: 180f32.to_radians(),
view_angle_normalized: 0.5f32,
// zoom_range: 8f32..=15f32,
// zoom_height_range: 0.2f32..=8f32,
Expand Down
34 changes: 26 additions & 8 deletions src/plugins/gameplay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ impl Plugin for GameplayPlugin {
transition_on_win,
manage_goals,
deactivate_out_of_bounds_players,
check_for_loss,
// restart,
setup_goals.after(GltfBlueprintsSet::AfterSpawn),
)
.run_if(in_state(GameState::Main)),
)
.add_systems(Update, toggle_pause)
.add_systems(OnExit(GameState::Main), pause_physics)
.add_systems(OnEnter(GameState::Pause), pause_physics)
.add_systems(OnEnter(GameState::Main), unpause_physics);
}
}
Expand Down Expand Up @@ -108,10 +109,8 @@ fn transition_on_win(
) {
if let Ok(game_manager) = game_manager.get_single() {
if game_manager.current >= game_manager.goal {
// Stop the game timer
timer.finish(&time);
// Game win
next_state.set(GameState::Post);
next_state.set(GameState::Post { win: true });
}
}
}
Expand All @@ -120,7 +119,7 @@ fn transition_on_win(
/// it for cleanup/respawn
fn deactivate_out_of_bounds_players(
mut cmd: Commands,
players: Query<(Entity, &Transform), With<Player>>,
players: Query<(Entity, &Transform), (With<Player>, With<Active>)>,
floor: Query<&Transform, With<FloorBounds>>,
) {
if let Ok(floor) = floor.get_single() {
Expand All @@ -132,6 +131,28 @@ fn deactivate_out_of_bounds_players(
}
}

fn check_for_loss(
mut next_state: ResMut<NextState<GameState>>,
all_players: Query<Entity, (With<Player>, Without<ScoredPlayer>)>,
active_players: Query<
(),
(
With<Player>,
Without<ScoredPlayer>,
With<Active>,
Without<OutOfBounds>,
),
>,
new_oob_players: Query<(), (With<Player>, Added<OutOfBounds>)>,
) {
if !new_oob_players.is_empty() {
// ensure we still have some active players
if active_players.is_empty() {
next_state.set(GameState::Post { win: false })
}
}
}

/// Checks to see if we should be pausing or unpausing the game, and changes the state
fn toggle_pause(
actions: Res<ActionState<Action>>,
Expand Down Expand Up @@ -204,6 +225,3 @@ fn unpause_physics(mut physics_time: ResMut<Time<Physics>>) {
// cmd.spawn((FocalPoint, *transform));
// }
// }

/// TODO
fn check_for_loss() {}
8 changes: 7 additions & 1 deletion src/plugins/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ impl Plugin for LevelsPlugin {
process_colliders,
process_lights,
),
);
)
.add_systems(OnEnter(GameState::post()), finalize_timers);
}
}

fn finalize_timers(mut timer: ResMut<GameTime>, time: Res<Time>) {
// Stop the game timer
timer.finish(&time);
}

fn cleanup(
mut cmd: Commands,
time: Res<Time>,
Expand Down
106 changes: 69 additions & 37 deletions src/plugins/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Plugin for UiPlugin {
(menu_ui, move_menu_camera).run_if(in_state(GameState::Menu)),
)
.add_systems(Update, (game_ui,).run_if(in_state(GameState::Main)))
.add_systems(Update, post_game_ui.run_if(in_state(GameState::Post)))
.add_systems(Update, post_game_ui.run_if(in_state(GameState::post())))
.add_systems(Update, pause_menu_ui.run_if(in_state(GameState::Pause)))
.insert_resource(KbgpSettings {
bindings: {
Expand Down Expand Up @@ -224,7 +224,8 @@ fn game_ui(
fn post_game_ui(
mut cmd: Commands,
mut ctx: EguiContexts,
mut state: ResMut<NextState<GameState>>,
mut next_state: ResMut<NextState<GameState>>,
state: Res<State<GameState>>,
font: Res<settings::FontSettings>,
game_time: Res<GameTime>,
mut current_level: ResMut<CurrentLevelIndex>,
Expand All @@ -250,46 +251,77 @@ fn post_game_ui(
),
|ui| {
ui.vertical_centered_justified(|ui| {
let time = game_time.final_time().unwrap();
let seconds = time.as_secs();
let millis = time.as_millis() % 1000;
ui.label(
egui::RichText::new(format!("{:0>2}:{:0>3}", seconds, millis))
.font(FontId::proportional(font.size_subtitle())),
);
let win = *match state.get() {
GameState::Post { win } => win,
_ => unreachable!(),
};
if win {
let time = game_time.final_time().unwrap();
let seconds = time.as_secs();
let millis = time.as_millis() % 1000;
ui.label(
egui::RichText::new(format!("{:0>2}:{:0>3}", seconds, millis))
.font(FontId::proportional(font.size_subtitle())),
);

// Go to the next nevel if there is a next level
let maybe_next_level = current_level.0 + 1;
match game_assets.levels.get(maybe_next_level) {
Some(level) => {
ui.label(
egui::RichText::new("You win!")
.font(FontId::proportional(font.size_title()))
.color(Color32::GREEN),
);
if ui
.button("Next level")
.kbgp_navigation()
.kbgp_initial_focus()
.clicked()
{
current_level.0 += 1;
state.set(GameState::LevelTransition {
level: level.clone(),
});
// Go to the next nevel if there is a next level
let maybe_next_level = current_level.0 + 1;
match game_assets.levels.get(maybe_next_level) {
Some(level) => {
ui.label(
egui::RichText::new("You win!")
.font(FontId::proportional(font.size_title()))
.color(Color32::DARK_GREEN),
);
if ui
.button("Next level")
.kbgp_navigation()
.kbgp_initial_focus()
.clicked()
{
current_level.0 += 1;
next_state.set(GameState::LevelTransition {
level: level.clone(),
});
}
}
}
None => {
ui.label(
egui::RichText::new("You beat the game epic style")
.font(FontId::proportional(font.size_title()))
.color(Color32::GREEN),
);
None => {
ui.label(
egui::RichText::new("You beat the game epic style")
.font(FontId::proportional(font.size_title()))
.color(Color32::DARK_GREEN),
);

if ui.button("Main menu").clicked() {
state.set(GameState::Menu);
if ui
.button("Main menu")
.kbgp_navigation()
.kbgp_initial_focus()
.clicked()
{
next_state.set(GameState::Menu);
}
}
}
} else {
ui.label(
egui::RichText::new("Out of bounds!")
.font(FontId::proportional(font.size_title()))
.color(Color32::DARK_RED),
);
if ui
.button("Restart level")
.kbgp_navigation()
.kbgp_initial_focus()
.clicked()
{
next_state.set(GameState::LevelTransition {
level: game_assets
.levels
.get(current_level.0)
.unwrap()
.clone(),
});
}
}
})
},
Expand Down
8 changes: 7 additions & 1 deletion src/state/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ pub enum GameState {
Menu,
Main,
Pause,
Post,
Post {
win: bool,
},
// Allows us to consolidate level spawn and cleanup code.
// If no level specified, go to the main menu
LevelTransition {
Expand Down Expand Up @@ -37,4 +39,8 @@ impl GameState {
level: Default::default(),
}
}
#[inline(always)]
pub fn post() -> Self {
Self::Post { win: false }
}
}

0 comments on commit 3accd6e

Please sign in to comment.