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
84
85
86
87
88
89
90
91
92
93
94
use geom::{Angle, Pt2D, Speed};
use widgetry::{EventCtx, Key};

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

pub struct InstantController {
    speed: Speed,
}

impl InstantController {
    pub fn new(speed: Speed) -> InstantController {
        InstantController {
            // TODO Hack
            speed: 5.0 * speed,
        }
    }
}

impl Controller for InstantController {
    fn displacement(&mut self, ctx: &mut EventCtx) -> (f64, f64) {
        let mut dx = 0.0;
        let mut dy = 0.0;

        if let Some(dt) = ctx.input.nonblocking_is_update_event() {
            ctx.input.use_update_event();

            let dist = (dt * self.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,
    rot_speed_degrees: f64,
    fwd_speed: Speed,
}

impl RotateController {
    pub fn new(fwd_speed: Speed) -> RotateController {
        RotateController {
            angle: Angle::ZERO,
            rot_speed_degrees: 100.0,
            // TODO Hack
            fwd_speed: 5.0 * fwd_speed,
        }
    }
}

impl Controller for RotateController {
    fn displacement(&mut self, ctx: &mut EventCtx) -> (f64, f64) {
        let mut dx = 0.0;
        let mut dy = 0.0;

        if let Some(dt) = ctx.input.nonblocking_is_update_event() {
            ctx.input.use_update_event();

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

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

        (dx, dy)
    }
}