santa/
controls.rs

1use geom::{Angle, Speed};
2use widgetry::{EventCtx, Key};
3
4// TODO The timestep accumulation seems fine. What's wrong? Clamping errors repeated?
5const HACK: f64 = 5.0;
6
7pub struct InstantController {
8    /// Which of the 8 directions are we facing, based on the last set of keys pressed down?
9    pub facing: Angle,
10}
11
12impl InstantController {
13    pub fn new() -> InstantController {
14        InstantController {
15            facing: Angle::ZERO,
16        }
17    }
18
19    pub fn displacement(&mut self, ctx: &mut EventCtx, speed: Speed) -> Option<(f64, f64)> {
20        let dt = ctx.input.nonblocking_is_update_event()?;
21        // Work around a few bugs here.
22        //
23        // 1) The Santa sprites are all facing 180 degrees, not 0, so invert X.
24        // 2) Invert y so that negative is up.
25        //
26        // It's confusing, but self.facing winds up working for rotating the sprite, and the output
27        // displacement works.
28        self.facing = angle_from_arrow_keys(ctx)?.opposite();
29        let magnitude = (dt * HACK * speed).inner_meters();
30        let (sin, cos) = self.facing.normalized_radians().sin_cos();
31        Some((-magnitude * cos, -magnitude * sin))
32    }
33}
34
35pub fn angle_from_arrow_keys(ctx: &EventCtx) -> Option<Angle> {
36    let mut x: f64 = 0.0;
37    let mut y: f64 = 0.0;
38    if ctx.is_key_down(Key::LeftArrow) || ctx.is_key_down(Key::A) {
39        x -= 1.0;
40    }
41    if ctx.is_key_down(Key::RightArrow) || ctx.is_key_down(Key::D) {
42        x += 1.0;
43    }
44    if ctx.is_key_down(Key::UpArrow) || ctx.is_key_down(Key::W) {
45        y -= 1.0;
46    }
47    if ctx.is_key_down(Key::DownArrow) || ctx.is_key_down(Key::S) {
48        y += 1.0;
49    }
50
51    if x == 0.0 && y == 0.0 {
52        return None;
53    }
54    Some(Angle::new_rads(y.atan2(x)))
55}