Add creative mode toggle

This commit is contained in:
Sijmen 2021-06-02 16:30:13 +02:00
parent aec3509107
commit 08f78ab453
Signed by: vijfhoek
GPG key ID: 82D05C89B28B0DAE

View file

@ -49,6 +49,7 @@ pub struct WorldState {
pub up_speed: f32, pub up_speed: f32,
pub sprinting: bool, pub sprinting: bool,
pub creative: bool,
} }
impl WorldState { impl WorldState {
@ -388,6 +389,7 @@ impl WorldState {
backward_pressed: false, backward_pressed: false,
left_pressed: false, left_pressed: false,
right_pressed: false, right_pressed: false,
creative: false,
}; };
world_state.update_world_geometry(render_context); world_state.update_world_geometry(render_context);
@ -530,7 +532,25 @@ impl WorldState {
VirtualKeyCode::S => self.backward_pressed = pressed, VirtualKeyCode::S => self.backward_pressed = pressed,
VirtualKeyCode::A => self.left_pressed = pressed, VirtualKeyCode::A => self.left_pressed = pressed,
VirtualKeyCode::D => self.right_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, VirtualKeyCode::LControl => self.sprinting = state == &ElementState::Pressed,
_ => (), _ => (),
} }
@ -556,7 +576,7 @@ impl WorldState {
let up = Vector3::unit_y() * self.up_speed * speed * dt_seconds; let up = Vector3::unit_y() * self.up_speed * speed * dt_seconds;
new_position += up; new_position += up;
if self.check_collision(new_position) { if !self.creative && self.check_collision(new_position) {
new_position -= up; new_position -= up;
self.up_speed = 0.0; self.up_speed = 0.0;
} }
@ -565,7 +585,7 @@ impl WorldState {
let forward = Vector3::new(yaw_cos, 0.0, yaw_sin).normalize(); let forward = Vector3::new(yaw_cos, 0.0, yaw_sin).normalize();
let forward = forward * forward_speed as f32 * speed * dt_seconds; let forward = forward * forward_speed as f32 * speed * dt_seconds;
new_position += forward; new_position += forward;
if self.check_collision(new_position) { if !self.creative && self.check_collision(new_position) {
new_position -= forward; new_position -= forward;
} }
@ -573,14 +593,16 @@ impl WorldState {
let right = Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize(); let right = Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize();
let right = right * right_speed as f32 * speed * dt_seconds; let right = right * right_speed as f32 * speed * dt_seconds;
new_position += right; new_position += right;
if self.check_collision(new_position) { if !self.creative && self.check_collision(new_position) {
new_position -= right; new_position -= right;
} }
self.camera.position = new_position; self.camera.position = new_position;
self.up_speed -= 1.6 * dt.as_secs_f32(); if !self.creative {
self.up_speed *= 0.98_f32.powf(dt.as_secs_f32() / 20.0); 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) { pub fn update(&mut self, dt: Duration, render_context: &RenderContext) {