summaryrefslogtreecommitdiff
path: root/src/input.rs
blob: 108514e560cfe10889ed87981d85594245c8a02d (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use glam::{IVec2, Vec2};
use winit::{
    event::{KeyEvent, MouseButton, WindowEvent},
    keyboard::{KeyCode, PhysicalKey},
};

use crate::{camera::Camera, sim::cell_manager::manager::CellManager};

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Input {
    // movement
    Up = 0,
    Left,
    Down,
    Right,

    // camera
    FreeCamera,
    CameraUp,
    CameraLeft,
    CameraDown,
    CameraRight,

    // sim management
    Pause,
    Step,
    ClearGrid,
    ClearEntities,
    ClearParticles,

    // game controls?
    Grenade,
    Tnt,

    // placeholder actions
    Action1,
    Action2,
    Action3,
    Action4,
    Action5,
    Action6,
}

const INPUT_VAR_LEN: usize = 22;

const KEYMAP: [(KeyCode, Input); INPUT_VAR_LEN] = [
    (KeyCode::KeyW, Input::Up),
    (KeyCode::KeyA, Input::Left),
    (KeyCode::KeyS, Input::Down),
    (KeyCode::KeyD, Input::Right),
    (KeyCode::KeyZ, Input::FreeCamera),
    (KeyCode::ArrowUp, Input::CameraUp),
    (KeyCode::ArrowLeft, Input::CameraLeft),
    (KeyCode::ArrowDown, Input::CameraDown),
    (KeyCode::ArrowRight, Input::CameraRight),
    (KeyCode::KeyU, Input::Pause),
    (KeyCode::KeyX, Input::Step),
    (KeyCode::KeyC, Input::ClearGrid),
    (KeyCode::KeyV, Input::ClearEntities),
    (KeyCode::KeyP, Input::ClearParticles),
    (KeyCode::KeyG, Input::Grenade),
    (KeyCode::KeyT, Input::Tnt),
    (KeyCode::Digit1, Input::Action1),
    (KeyCode::Digit2, Input::Action2),
    (KeyCode::Digit3, Input::Action3),
    (KeyCode::Digit4, Input::Action4),
    (KeyCode::Digit5, Input::Action5),
    (KeyCode::Digit6, Input::Action6),
];

pub struct InputManager {
    pub screen_mouse_pos: Vec2,
    pub world_mouse_pos: Vec2,
    pub chunk_mouse_pos: IVec2,
    pub local_mouse_pos: IVec2,

    pub lmb_pressed: bool,
    pub lmb_held: bool,
    pub rmb_pressed: bool,
    pub rmb_held: bool,
    pressed: [bool; INPUT_VAR_LEN],
    held: [bool; INPUT_VAR_LEN],
}

impl InputManager {
    pub fn reset_for_frame(&mut self) {
        self.pressed = [false; INPUT_VAR_LEN];
        self.lmb_pressed = false;
        self.rmb_pressed = false;
    }

    pub fn apply_event(&mut self, event: &WindowEvent, camera: &Camera) {
        match event {
            WindowEvent::KeyboardInput {
                event:
                    KeyEvent {
                        physical_key: PhysicalKey::Code(code),
                        repeat: false,
                        state,
                        ..
                    },
                ..
            } => {
                if let Some(&(_, input)) = KEYMAP.iter().find(|m| m.0 == *code) {
                    if state.is_pressed() {
                        self.held[input as usize] = true;
                        self.pressed[input as usize] = true;
                    } else {
                        self.held[input as usize] = false;
                    }
                }
            }
            WindowEvent::CursorMoved { position, .. } => {
                let wp = camera
                    .screen_position_to_world(Vec2::new(position.x as f32, position.y as f32));

                let ((cx, cy), (lx, ly)) =
                    CellManager::split_game_position(wp.x.round() as i32, wp.y.round() as i32);

                self.screen_mouse_pos = Vec2::new(position.x as f32, position.y as f32);
                self.world_mouse_pos = wp;
                self.chunk_mouse_pos = IVec2::new(cx, cy);
                self.local_mouse_pos = IVec2::new(lx as i32, ly as i32);
            }
            WindowEvent::MouseInput { state, button, .. } => {
                if *button == MouseButton::Left {
                    if state.is_pressed() {
                        self.lmb_pressed = true;
                        self.lmb_held = true;
                    } else {
                        self.lmb_held = false;
                    }
                } else if *button == MouseButton::Right {
                    if state.is_pressed() {
                        self.rmb_pressed = true;
                        self.rmb_held = true;
                    } else {
                        self.rmb_held = false;
                    }
                }
            }
            _ => {}
        }
    }

    pub fn pressed(&self, input: Input) -> bool {
        if input as usize >= INPUT_VAR_LEN {
            false
        } else {
            self.pressed[input as usize]
        }
    }

    pub fn held(&self, input: Input) -> bool {
        if input as usize >= INPUT_VAR_LEN {
            false
        } else {
            self.held[input as usize]
        }
    }

    pub fn new() -> Self {
        InputManager {
            screen_mouse_pos: Vec2::ZERO,
            world_mouse_pos: Vec2::ZERO,
            chunk_mouse_pos: IVec2::ZERO,
            local_mouse_pos: IVec2::ZERO,

            lmb_pressed: false,
            lmb_held: false,
            rmb_pressed: false,
            rmb_held: false,
            pressed: [false; INPUT_VAR_LEN],
            held: [false; INPUT_VAR_LEN],
        }
    }
}