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
55
56
57
58
59
60
61
62
63
use geom::{Angle, Speed};
use widgetry::{EventCtx, Key};

// TODO The timestep accumulation seems fine. What's wrong? Clamping errors repeated?
const HACK: f64 = 5.0;

pub struct InstantController {
    /// Which of the 8 directions are we facing, based on the last set of keys pressed down?
    pub facing: Angle,
}

impl InstantController {
    pub fn new() -> InstantController {
        InstantController {
            facing: Angle::ZERO,
        }
    }

    pub fn displacement(&mut self, ctx: &mut EventCtx, speed: Speed) -> (f64, f64) {
        let mut dx = 0.0;
        let mut dy = 0.0;

        if let Some(dt) = ctx.input.nonblocking_is_update_event() {
            let dist = (dt * HACK * speed).inner_meters();
            let mut x = 0;
            let mut y = 0;
            // The x direction is inverted; somehow the usual Y inversion did this? Oh well,
            // it's isolated here nicely.
            if ctx.is_key_down(Key::LeftArrow) {
                dx -= dist;
                x = 1;
            }
            if ctx.is_key_down(Key::RightArrow) {
                dx += dist;
                x = -1;
            }
            if ctx.is_key_down(Key::UpArrow) {
                dy -= dist;
                y = -1;
            }
            if ctx.is_key_down(Key::DownArrow) {
                dy += dist;
                y = 1;
            }

            // TODO Better way to do this; acos and asin?
            self.facing = match (x, y) {
                (-1, -1) => Angle::degrees(135.0),
                (-1, 0) => Angle::degrees(180.0),
                (-1, 1) => Angle::degrees(225.0),
                (0, -1) => Angle::degrees(90.0),
                (0, 1) => Angle::degrees(270.0),
                (1, -1) => Angle::degrees(45.0),
                (1, 0) => Angle::degrees(0.0),
                (1, 1) => Angle::degrees(315.0),
                (0, 0) => self.facing,
                _ => unreachable!(),
            };
        }

        (dx, dy)
    }
}