Add double-jump to toggle creative flight. Closes #46
This commit is contained in:
parent
66dd64871d
commit
1aaf8292ba
|
@ -19,6 +19,7 @@ use collision::{Aabb, Aabb3};
|
||||||
use cgmath::{self, Point3, Vector3, Matrix4, Decomposed, Rotation3, Rad, Quaternion};
|
use cgmath::{self, Point3, Vector3, Matrix4, Decomposed, Rotation3, Rad, Quaternion};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::hash::BuildHasherDefault;
|
use std::hash::BuildHasherDefault;
|
||||||
|
use std::time::Instant;
|
||||||
use crate::types::hash::FNVHash;
|
use crate::types::hash::FNVHash;
|
||||||
use crate::settings::Stevenkey;
|
use crate::settings::Stevenkey;
|
||||||
use crate::shared::Position as BPosition;
|
use crate::shared::Position as BPosition;
|
||||||
|
@ -441,6 +442,9 @@ impl ecs::System for PlayerRenderer {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct PlayerMovement {
|
pub struct PlayerMovement {
|
||||||
pub flying: bool,
|
pub flying: bool,
|
||||||
|
pub want_to_fly: bool,
|
||||||
|
pub when_last_jump_pressed: Option<Instant>,
|
||||||
|
pub when_last_jump_released: Option<Instant>,
|
||||||
pub did_touch_ground: bool,
|
pub did_touch_ground: bool,
|
||||||
pub pressed_keys: HashMap<Stevenkey, bool, BuildHasherDefault<FNVHash>>,
|
pub pressed_keys: HashMap<Stevenkey, bool, BuildHasherDefault<FNVHash>>,
|
||||||
}
|
}
|
||||||
|
@ -533,6 +537,27 @@ impl ecs::System for MovementHandler {
|
||||||
let gamemode = m.get_component(e, self.gamemode).unwrap();
|
let gamemode = m.get_component(e, self.gamemode).unwrap();
|
||||||
movement.flying |= gamemode.always_fly();
|
movement.flying |= gamemode.always_fly();
|
||||||
|
|
||||||
|
// Detect double-tapping jump to toggle creative flight
|
||||||
|
if movement.is_key_pressed(Stevenkey::Jump) {
|
||||||
|
if movement.when_last_jump_pressed.is_none() {
|
||||||
|
movement.when_last_jump_pressed = Some(Instant::now());
|
||||||
|
if !movement.when_last_jump_released.is_none() {
|
||||||
|
let dt = movement.when_last_jump_pressed.unwrap() - movement.when_last_jump_released.unwrap();
|
||||||
|
if dt.as_secs() == 0 && dt.subsec_millis() <= crate::settings::DOUBLE_JUMP_MS {
|
||||||
|
movement.want_to_fly = !movement.want_to_fly;
|
||||||
|
//println!("double jump! dt={:?} toggle want_to_fly = {}", dt, movement.want_to_fly);
|
||||||
|
|
||||||
|
if gamemode.can_fly() && !gamemode.always_fly() {
|
||||||
|
movement.flying = movement.want_to_fly;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if !movement.when_last_jump_pressed.is_none() {
|
||||||
|
movement.when_last_jump_released = Some(Instant::now());
|
||||||
|
movement.when_last_jump_pressed = None;
|
||||||
|
}
|
||||||
|
|
||||||
let position = m.get_component_mut(e, self.position).unwrap();
|
let position = m.get_component_mut(e, self.position).unwrap();
|
||||||
let rotation = m.get_component(e, self.rotation).unwrap();
|
let rotation = m.get_component(e, self.rotation).unwrap();
|
||||||
let velocity = m.get_component_mut(e, self.velocity).unwrap();
|
let velocity = m.get_component_mut(e, self.velocity).unwrap();
|
||||||
|
|
|
@ -59,6 +59,8 @@ pub const CL_KEYBIND_SNEAK: console::CVar<i64> = create_keybind!(LShift, "cl_key
|
||||||
pub const CL_KEYBIND_SPRINT: console::CVar<i64> = create_keybind!(LControl, "cl_keybind_sprint", "Keybinding for sprinting");
|
pub const CL_KEYBIND_SPRINT: console::CVar<i64> = create_keybind!(LControl, "cl_keybind_sprint", "Keybinding for sprinting");
|
||||||
pub const CL_KEYBIND_JUMP: console::CVar<i64> = create_keybind!(Space, "cl_keybind_jump", "Keybinding for jumping");
|
pub const CL_KEYBIND_JUMP: console::CVar<i64> = create_keybind!(Space, "cl_keybind_jump", "Keybinding for jumping");
|
||||||
|
|
||||||
|
pub const DOUBLE_JUMP_MS: u32 = 100;
|
||||||
|
|
||||||
pub fn register_vars(vars: &mut console::Vars) {
|
pub fn register_vars(vars: &mut console::Vars) {
|
||||||
vars.register(R_MAX_FPS);
|
vars.register(R_MAX_FPS);
|
||||||
vars.register(R_FOV);
|
vars.register(R_FOV);
|
||||||
|
|
Loading…
Reference in New Issue