santa/
controls.rs
1use geom::{Angle, Speed};
2use widgetry::{EventCtx, Key};
3
4const HACK: f64 = 5.0;
6
7pub struct InstantController {
8 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 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}