Add pause/resume control

This commit is contained in:
Tyler Hallada 2020-08-13 11:02:41 -04:00
parent 62b212a7a6
commit 6b46c28607
2 changed files with 9 additions and 3 deletions

View File

@ -31,6 +31,7 @@ Options:
|key | control| |key | control|
|----|--------| |----|--------|
| R | reset the simulation | | R | reset the simulation |
| Space | pause / resume the simulation |
| Left Click | hold and move mouse to pan the view | | Left Click | hold and move mouse to pan the view |
| Middle Click | hold and move mouse up and down to zoom in and out | | Middle Click | hold and move mouse up and down to zoom in and out |
| Right Click | click on a body to focus the camera on that body | | Right Click | click on a body to focus the camera on that body |

View File

@ -37,6 +37,7 @@ struct State {
cursor_position: Option<Vec2>, cursor_position: Option<Vec2>,
zooming: bool, zooming: bool,
panning: bool, panning: bool,
paused: bool,
follow_body_index: Option<usize>, follow_body_index: Option<usize>,
} }
@ -142,8 +143,10 @@ fn add_bodies(
} }
} }
fn time_step(mut grav_tree: ResMut<Simulation>) { fn time_step(state: Res<State>, mut grav_tree: ResMut<Simulation>) {
grav_tree.0 = grav_tree.0.time_step(); if !state.paused {
grav_tree.0 = grav_tree.0.time_step();
}
} }
fn update_bodies(grav_tree: Res<Simulation>, mut body_query: Query<(&Entity, &mut Translation)>) { fn update_bodies(grav_tree: Res<Simulation>, mut body_query: Query<(&Entity, &mut Translation)>) {
@ -257,7 +260,7 @@ fn keyboard_input(
) { ) {
for event in state.keyboard_event_reader.iter(&keyboard_input_events) { for event in state.keyboard_event_reader.iter(&keyboard_input_events) {
if let Some(key_code) = event.key_code { if let Some(key_code) = event.key_code {
if key_code == KeyCode::R { if key_code == KeyCode::R && event.state == ElementState::Pressed {
for (_, scale, mut translation) in &mut query.iter() { for (_, scale, mut translation) in &mut query.iter() {
if let Some(window) = windows.get_primary() { if let Some(window) = windows.get_primary() {
translation.0.set_x(0.); translation.0.set_x(0.);
@ -268,6 +271,8 @@ fn keyboard_input(
); );
} }
} }
} else if key_code == KeyCode::Space && event.state == ElementState::Pressed {
state.paused = !state.paused;
} }
} }
} }