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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use geom::{Angle, Pt2D, Speed};
use widgetry::{EventCtx, Key};

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

pub trait Controller {
    fn displacement(&mut self, ctx: &mut EventCtx, speed: Speed) -> (f64, f64);
}

pub struct InstantController;

impl InstantController {
    pub fn new() -> InstantController {
        InstantController
    }
}

impl Controller for InstantController {
    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();
            if ctx.is_key_down(Key::LeftArrow) {
                dx -= dist;
            }
            if ctx.is_key_down(Key::RightArrow) {
                dx += dist;
            }
            if ctx.is_key_down(Key::UpArrow) {
                dy -= dist;
            }
            if ctx.is_key_down(Key::DownArrow) {
                dy += dist;
            }
        }

        (dx, dy)
    }
}

pub struct RotateController {
    angle: Angle,
}

impl RotateController {
    pub fn new() -> RotateController {
        RotateController { angle: Angle::ZERO }
    }
}

impl Controller for RotateController {
    fn displacement(&mut self, ctx: &mut EventCtx, fwd_speed: Speed) -> (f64, f64) {
        let rot_speed_degrees = 100.0;

        let mut dx = 0.0;
        let mut dy = 0.0;

        if let Some(dt) = ctx.input.nonblocking_is_update_event() {
            if ctx.is_key_down(Key::LeftArrow) {
                self.angle = self
                    .angle
                    .rotate_degs(-rot_speed_degrees * dt.inner_seconds());
            }
            if ctx.is_key_down(Key::RightArrow) {
                self.angle = self
                    .angle
                    .rotate_degs(rot_speed_degrees * dt.inner_seconds());
            }

            if ctx.is_key_down(Key::UpArrow) {
                let dist = dt * HACK * fwd_speed;
                let pt = Pt2D::new(0.0, 0.0).project_away(dist, self.angle);
                dx = pt.x();
                dy = pt.y();
            }
        }

        (dx, dy)
    }
}