From 08f78ab453311e94788cdeb9cbeddd01583cfff6 Mon Sep 17 00:00:00 2001 From: Vijfhoek Date: Wed, 2 Jun 2021 16:30:13 +0200 Subject: [PATCH] Add creative mode toggle --- src/state/world_state.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/state/world_state.rs b/src/state/world_state.rs index 28ca955..ce3c6a8 100644 --- a/src/state/world_state.rs +++ b/src/state/world_state.rs @@ -49,6 +49,7 @@ pub struct WorldState { pub up_speed: f32, pub sprinting: bool, + pub creative: bool, } impl WorldState { @@ -388,6 +389,7 @@ impl WorldState { backward_pressed: false, left_pressed: false, right_pressed: false, + creative: false, }; world_state.update_world_geometry(render_context); @@ -530,7 +532,25 @@ impl WorldState { VirtualKeyCode::S => self.backward_pressed = pressed, VirtualKeyCode::A => self.left_pressed = pressed, VirtualKeyCode::D => self.right_pressed = pressed, - VirtualKeyCode::Space if state == &ElementState::Pressed => self.up_speed = 0.6, + VirtualKeyCode::F2 if pressed => self.creative = !self.creative, + VirtualKeyCode::Space => { + self.up_speed = if self.creative { + if pressed { + 1.0 + } else { + 0.0 + } + } else { + 0.6 + } + } + VirtualKeyCode::LShift if self.creative => { + self.up_speed = if pressed { + -1.0 + } else { + 0.0 + } + } VirtualKeyCode::LControl => self.sprinting = state == &ElementState::Pressed, _ => (), } @@ -556,7 +576,7 @@ impl WorldState { let up = Vector3::unit_y() * self.up_speed * speed * dt_seconds; new_position += up; - if self.check_collision(new_position) { + if !self.creative && self.check_collision(new_position) { new_position -= up; self.up_speed = 0.0; } @@ -565,7 +585,7 @@ impl WorldState { let forward = Vector3::new(yaw_cos, 0.0, yaw_sin).normalize(); let forward = forward * forward_speed as f32 * speed * dt_seconds; new_position += forward; - if self.check_collision(new_position) { + if !self.creative && self.check_collision(new_position) { new_position -= forward; } @@ -573,14 +593,16 @@ impl WorldState { let right = Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize(); let right = right * right_speed as f32 * speed * dt_seconds; new_position += right; - if self.check_collision(new_position) { + if !self.creative && self.check_collision(new_position) { new_position -= right; } self.camera.position = new_position; - self.up_speed -= 1.6 * dt.as_secs_f32(); - self.up_speed *= 0.98_f32.powf(dt.as_secs_f32() / 20.0); + if !self.creative { + self.up_speed -= 1.6 * dt.as_secs_f32(); + self.up_speed *= 0.98_f32.powf(dt.as_secs_f32() / 20.0); + } } pub fn update(&mut self, dt: Duration, render_context: &RenderContext) {