Skip to content

Commit

Permalink
added pause and resume simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
frederickjjoubert committed Mar 28, 2023
1 parent e96a906 commit a05fa7c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@ pub struct GamePlugin;

impl Plugin for GamePlugin {
fn build(&self, app: &mut App) {
app.add_state::<SimulationState>()
app
// Events
.add_event::<GameOver>()
// Plugins
// States
.add_state::<SimulationState>()
// OnEnter Systems
.add_system(pause_simulation.in_schedule(OnEnter(AppState::Game)))
// My Plugins
.add_plugin(EnemyPlugin)
.add_plugin(PlayerPlugin)
.add_plugin(ScorePlugin)
.add_plugin(StarPlugin)
// Systems
.add_system(toggle_simulation.run_if(in_state(AppState::Game)));
.add_system(toggle_simulation.run_if(in_state(AppState::Game)))
// Exit State Systems
.add_system(resume_simulation.in_schedule(OnExit(AppState::Game)));
}
}

#[derive(States, Debug, Clone, Copy, Eq, PartialEq, Hash, Default)]
pub enum SimulationState {
Running,
#[default]
Running,
Paused,
}
8 changes: 8 additions & 0 deletions src/game/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ use bevy::prelude::*;

use crate::game::SimulationState;

pub fn pause_simulation(mut simulation_state_next_state: ResMut<NextState<SimulationState>>) {
simulation_state_next_state.set(SimulationState::Paused);
}

pub fn resume_simulation(mut simulation_state_next_state: ResMut<NextState<SimulationState>>) {
simulation_state_next_state.set(SimulationState::Running);
}

pub fn toggle_simulation(
keyboard_input: Res<Input<KeyCode>>,
simulation_state: Res<State<SimulationState>>,
Expand Down
1 change: 0 additions & 1 deletion src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use bevy::prelude::*;
use bevy::window::PrimaryWindow;

use crate::events::*;
use crate::game::SimulationState;
use crate::AppState;

pub fn spawn_camera(mut commands: Commands, window_query: Query<&Window, With<PrimaryWindow>>) {
Expand Down

0 comments on commit a05fa7c

Please sign in to comment.