diff options
author | Lassi Pulkkinen <lassi@pulk.fi> | 2024-10-31 03:11:21 +0200 |
---|---|---|
committer | Lassi Pulkkinen <lassi@pulk.fi> | 2024-10-31 03:51:35 +0200 |
commit | ae44478b30d890fe0fb04022f44d474dcdcc3f9d (patch) | |
tree | 5f462459ae4b47d22114eed717d1382d08cf4dfe /control.ha |
Diffstat (limited to 'control.ha')
-rw-r--r-- | control.ha | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/control.ha b/control.ha new file mode 100644 index 0000000..ee1f22d --- /dev/null +++ b/control.ha @@ -0,0 +1,71 @@ +use sdl2::*; + +type ControlKey = struct { + key: Key, + pressed: bool, +}; + +let CONTROL_KEY_FORWARD = ControlKey { key = KEY_W, ... }; +let CONTROL_KEY_BACKWARD = ControlKey { key = KEY_S, ... }; +let CONTROL_KEY_LEFT = ControlKey { key = KEY_A, ... }; +let CONTROL_KEY_RIGHT = ControlKey { key = KEY_D, ... }; +let CONTROL_KEY_JUMP = ControlKey { key = KEY_SPACE, ... }; +let CONTROL_KEY_SNEAK = ControlKey { key = KEY_LSHIFT, ... }; + +const CONTROL_KEYS = [ + &CONTROL_KEY_FORWARD, + &CONTROL_KEY_BACKWARD, + &CONTROL_KEY_LEFT, + &CONTROL_KEY_RIGHT, + &CONTROL_KEY_JUMP, + &CONTROL_KEY_SNEAK, +]; + +fn control_input(event: InputEvent) void = { + match (event) { + case CancelEvent => + for (let i = 0z; i < len(CONTROL_KEYS); i += 1) { + CONTROL_KEYS[i].pressed = false; + }; + case let event: KeyEvent => + const pressed = switch (event.status) { + case ButtonStatus::DOWN, ButtonStatus::HELD => + yield true; + case ButtonStatus::UP => + yield false; + }; + for (let i = 0z; i < len(CONTROL_KEYS); i += 1) { + if (CONTROL_KEYS[i].key == event.key) { + CONTROL_KEYS[i].pressed = pressed; + }; + }; + case => void; + }; +}; + +type Control = struct { + yaw: f32, + pitch: f32, + walk: f32, + strafe: f32, + jump: bool, + sneak: bool, +}; + +def MOUSE_SENSITIVITY = 0.115f32; + +fn control_frame() void = { + let mx = 0, my = 0; + SDL_GetRelativeMouseState(&mx, &my); + + player_control(&Control { + yaw = mx: f32 * MOUSE_SENSITIVITY, + pitch = my: f32 * MOUSE_SENSITIVITY, + walk = (if (CONTROL_KEY_FORWARD.pressed) 1.0f32 else 0.0f32) + - (if (CONTROL_KEY_BACKWARD.pressed) 1.0f32 else 0.0f32), + strafe = (if (CONTROL_KEY_LEFT.pressed) 1.0f32 else 0.0f32) + - (if (CONTROL_KEY_RIGHT.pressed) 1.0f32 else 0.0f32), + jump = CONTROL_KEY_JUMP.pressed, + sneak = CONTROL_KEY_SNEAK.pressed, + }); +}; |