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 self::ControllerInternal as CI;
use self::ControllerLayout as CL;
use glfw::{Action, Key, Window};
use log::debug;
use nalgebra::{zero, Vector2};
use util::Lerp;
#[derive(Debug)]
pub enum ControllerLayout {
WASD,
Arrows,
}
#[derive(Copy, Clone, Debug)]
struct ControllerInternal {
forward: Key,
is_forward: bool,
backward: Key,
is_backward: bool,
left: Key,
is_left: bool,
right: Key,
is_right: bool,
boost: Key,
is_boost: bool,
}
impl ControllerInternal {
pub fn new(controller_layout: &ControllerLayout) -> ControllerInternal {
match controller_layout {
CL::WASD => Default::default(),
CL::Arrows => CI {
forward: Key::Up,
backward: Key::Down,
left: Key::Left,
right: Key::Right,
boost: Key::RightShift,
..Default::default()
},
}
}
}
impl Default for ControllerInternal {
fn default() -> ControllerInternal {
ControllerInternal {
forward: Key::W,
is_forward: false,
backward: Key::S,
is_backward: false,
left: Key::A,
is_left: false,
right: Key::D,
is_right: false,
boost: Key::LeftShift,
is_boost: false,
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct Controller {
is_smooth: bool,
ci: ControllerInternal,
axis_goal: Vector2<f32>,
axis: Vector2<f32>,
boost: bool,
}
impl Controller {
pub fn new(smooth: bool, controller_layout: &ControllerLayout) -> Controller {
debug!("New smooth: {}, layout: {:?}", smooth, controller_layout);
Controller {
is_smooth: smooth,
ci: ControllerInternal::new(&controller_layout),
axis_goal: zero(),
axis: zero(),
boost: false,
}
}
pub fn process_input(&mut self, window: &Window, dt: f32) {
if window.get_key(self.ci.forward) == Action::Press && !self.ci.is_forward {
self.set_y_axis(1.);
self.ci.is_forward = true;
} else if window.get_key(self.ci.forward) == Action::Release && self.ci.is_forward {
self.set_y_axis(0.);
self.ci.is_forward = false;
}
if window.get_key(self.ci.backward) == Action::Press && !self.ci.is_backward {
self.set_y_axis(-1.);
self.ci.is_backward = true;
} else if window.get_key(self.ci.backward) == Action::Release && self.ci.is_backward {
self.set_y_axis(0.);
self.ci.is_backward = false;
}
if window.get_key(self.ci.left) == Action::Press && !self.ci.is_left {
self.set_x_axis(-1.);
self.ci.is_left = true;
} else if window.get_key(self.ci.left) == Action::Release && self.ci.is_left {
self.set_x_axis(0.);
self.ci.is_left = false;
}
if window.get_key(self.ci.right) == Action::Press && !self.ci.is_right {
self.set_x_axis(1.);
self.ci.is_right = true;
} else if window.get_key(self.ci.right) == Action::Release && self.ci.is_right {
self.set_x_axis(0.);
self.ci.is_right = false;
}
if window.get_key(self.ci.boost) == Action::Press && !self.ci.is_boost {
self.boost = true;
self.ci.is_boost = true;
} else if window.get_key(self.ci.boost) == Action::Release && self.ci.is_boost {
self.boost = false;
self.ci.is_boost = false;
}
if self.is_smooth {
self.axis = Vector2::lerp(&self.axis, &self.axis_goal, 5. * dt);
self.axis[0] = (self.axis[0] * 10_000.).trunc() / 10_000.;
self.axis[1] = (self.axis[1] * 10_000.).trunc() / 10_000.;
} else {
self.axis = self.axis_goal;
}
}
pub fn get_x_axis(&self) -> f32 {
self.axis[0]
}
pub fn get_y_axis(&self) -> f32 {
self.axis[1]
}
pub fn get_boost(&self) -> bool {
self.boost
}
fn set_x_axis(&mut self, value: f32) {
self.axis_goal[0] = value;
}
fn set_y_axis(&mut self, value: f32) {
self.axis_goal[1] = value;
}
}