1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use geom::{Angle, Speed};
use widgetry::{EventCtx, Key};
const HACK: f64 = 5.0;
pub struct InstantController {
pub facing: Angle,
}
impl InstantController {
pub fn new() -> InstantController {
InstantController {
facing: Angle::ZERO,
}
}
pub fn displacement(&mut self, ctx: &mut EventCtx, speed: Speed) -> Option<(f64, f64)> {
let dt = ctx.input.nonblocking_is_update_event()?;
let mut x: f64 = 0.0;
let mut y: f64 = 0.0;
if ctx.is_key_down(Key::LeftArrow) {
x += 1.0;
}
if ctx.is_key_down(Key::RightArrow) {
x -= 1.0;
}
if ctx.is_key_down(Key::UpArrow) {
y += 1.0;
}
if ctx.is_key_down(Key::DownArrow) {
y -= 1.0;
}
if x == 0.0 && y == 0.0 {
return None;
}
self.facing = Angle::new_rads(y.atan2(x));
let magnitude = (dt * HACK * speed).inner_meters();
let (sin, cos) = self.facing.normalized_radians().sin_cos();
Some((-magnitude * cos, -magnitude * sin))
}
}