veloren/voxygen/src/key_state.rs

82 lines
2.1 KiB
Rust
Raw Normal View History

use vek::Vec2;
pub struct KeyState {
pub right: bool,
pub left: bool,
pub up: bool,
pub down: bool,
2020-03-24 07:38:16 +00:00
pub climb_up: bool,
pub climb_down: bool,
2020-11-03 22:46:07 +00:00
pub swim_up: bool,
pub swim_down: bool,
pub fly: bool,
2020-03-24 07:38:16 +00:00
pub toggle_wield: bool,
pub toggle_glide: bool,
2020-10-07 02:23:20 +00:00
pub toggle_lantern: bool,
2020-03-24 07:38:16 +00:00
pub toggle_sit: bool,
2020-08-02 05:09:11 +00:00
pub toggle_sneak: bool,
2020-05-27 06:41:55 +00:00
pub toggle_dance: bool,
pub auto_walk: bool,
2020-03-24 07:38:16 +00:00
pub swap_loadout: bool,
pub respawn: bool,
2020-08-02 05:25:57 +00:00
pub collect: bool,
2020-03-10 21:00:13 +00:00
pub analog_matrix: Vec2<f32>,
}
2020-08-02 05:25:57 +00:00
impl Default for KeyState {
fn default() -> Self {
Self {
right: false,
left: false,
up: false,
down: false,
2020-03-24 07:38:16 +00:00
climb_up: false,
climb_down: false,
2020-11-03 22:46:07 +00:00
swim_up: false,
swim_down: false,
fly: false,
2020-03-24 07:38:16 +00:00
toggle_wield: false,
toggle_glide: false,
2020-10-07 02:23:20 +00:00
toggle_lantern: false,
2020-03-24 07:38:16 +00:00
toggle_sit: false,
2020-08-02 05:09:11 +00:00
toggle_sneak: false,
2020-05-27 06:41:55 +00:00
toggle_dance: false,
auto_walk: false,
2020-03-24 07:38:16 +00:00
swap_loadout: false,
respawn: false,
2020-08-02 05:25:57 +00:00
collect: false,
2020-03-10 21:00:13 +00:00
analog_matrix: Vec2::zero(),
}
}
2020-08-02 05:25:57 +00:00
}
2020-08-02 05:25:57 +00:00
impl KeyState {
pub fn dir_vec(&self) -> Vec2<f32> {
2020-03-10 21:00:13 +00:00
let dir = if self.analog_matrix == Vec2::zero() {
Vec2::<f32>::new(
if self.right { 1.0 } else { 0.0 } + if self.left { -1.0 } else { 0.0 },
if self.up || self.auto_walk { 1.0 } else { 0.0 }
+ if self.down { -1.0 } else { 0.0 },
2020-03-10 21:00:13 +00:00
)
} else {
self.analog_matrix
};
2020-03-10 21:00:13 +00:00
if dir.magnitude_squared() <= 1.0 {
dir
} else {
dir.normalized()
}
}
2020-03-24 07:38:16 +00:00
pub fn climb(&self) -> Option<common::comp::Climb> {
use common::comp::Climb;
match (self.climb_up, self.climb_down) {
(true, false) => Some(Climb::Up),
(false, true) => Some(Climb::Down),
(true, true) => Some(Climb::Hold),
(false, false) => None,
}
}
}