rust-pilot/src/game/controls.rs

69 lines
1.7 KiB
Rust

use piston_window::*;
use piston_window::math::*;
pub struct Controls {
up_d: bool,
down_d: bool,
left_d: bool,
right_d: bool,
fire: bool
}
impl Controls {
pub fn new() -> Self {
Controls {
up_d: false, down_d: false, left_d: false, right_d: false,
fire: false
}
}
pub fn update(&mut self, inp: &Event) {
if let Some(b) = inp.press_args() {
match b {
Button::Keyboard(Key::Up) => { self.up_d = true; }
Button::Keyboard(Key::Down) => { self.down_d = true; }
Button::Keyboard(Key::Left) => { self.left_d = true; }
Button::Keyboard(Key::Right) => { self.right_d = true; }
_ => {}
}
}
if let Some(b) = inp.release_args() {
match b {
Button::Keyboard(Key::Up) => { self.up_d = false; }
Button::Keyboard(Key::Down) => { self.down_d = false; }
Button::Keyboard(Key::Left) => { self.left_d = false; }
Button::Keyboard(Key::Right) => { self.right_d = false; }
Button::Keyboard(Key::Space) => { self.fire = true; }
_ => {}
}
}
}
pub fn thrust_control(&self) -> Scalar {
if self.up_d {
1.0
} else if self.down_d {
-1.0
} else {
0.0
}
}
pub fn rotation_control(&self) -> Scalar {
if self.right_d {
1.0
} else if self.left_d {
-1.0
} else {
0.0
}
}
pub fn fire_control(&mut self) -> bool {
let firing = self.fire;
self.fire = false;
firing
}
}