Skip to content

Commit 3ec7297

Browse files
committed
Add input consensus
It is now possible to use keyboard/mouse/gamepad in tandem. Also, FpsControllerInput is now stateless and truly represents one frame of input.
1 parent 9fb68e2 commit 3ec7297

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/controller.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ impl Plugin for FpsControllerPlugin {
3434
app.add_systems(
3535
PreUpdate,
3636
(
37-
fps_controller_input,
37+
fps_controller_reset_input,
38+
fps_controller_keyboard_mouse_input,
3839
fps_controller_gamepad_input,
3940
fps_controller_look,
4041
fps_controller_move,
@@ -209,7 +210,13 @@ impl Default for FpsController {
209210

210211
const ANGLE_EPSILON: f32 = 0.001953125;
211212

212-
pub fn fps_controller_input(
213+
pub fn fps_controller_reset_input(mut query: Query<&mut FpsControllerInput>) {
214+
for mut input in query.iter_mut() {
215+
*input = default();
216+
}
217+
}
218+
219+
pub fn fps_controller_keyboard_mouse_input(
213220
key_input: Res<ButtonInput<KeyCode>>,
214221
mut mouse_events: EventReader<MouseMotion>,
215222
mut query: Query<(&FpsController, &mut FpsControllerInput)>,
@@ -232,15 +239,15 @@ pub fn fps_controller_input(
232239
input.yaw = input.yaw.rem_euclid(TAU);
233240
}
234241

235-
input.movement = Vec3::new(
242+
input.movement += Vec3::new(
236243
to_axis(&key_input, controller.key_right, controller.key_left),
237244
to_axis(&key_input, controller.key_up, controller.key_down),
238245
to_axis(&key_input, controller.key_forward, controller.key_back),
239246
);
240-
input.sprint = key_input.pressed(controller.key_sprint);
241-
input.jump = key_input.pressed(controller.key_jump);
242-
input.fly = key_input.just_pressed(controller.key_fly);
243-
input.crouch = key_input.pressed(controller.key_crouch);
247+
input.sprint = input.sprint || key_input.pressed(controller.key_sprint);
248+
input.jump = input.jump || key_input.pressed(controller.key_jump);
249+
input.fly = input.fly || key_input.just_pressed(controller.key_fly);
250+
input.crouch = input.crouch || key_input.pressed(controller.key_crouch);
244251
}
245252
}
246253

@@ -280,19 +287,19 @@ pub fn fps_controller_gamepad_input(
280287
button(controller.pad_fly_up),
281288
button(controller.pad_fly_down),
282289
);
283-
input.movement = Vec3::new(move_vec.x, vertical_axis, move_vec.y);
284-
input.sprint = button_input.pressed(button(controller.pad_sprint));
285-
input.jump = button_input.pressed(button(controller.pad_jump));
286-
input.fly = button_input.just_pressed(button(controller.pad_fly));
287-
input.crouch = button_input.pressed(button(controller.pad_crouch));
290+
input.movement += Vec3::new(move_vec.x, vertical_axis, move_vec.y);
291+
input.sprint = input.sprint || button_input.pressed(button(controller.pad_sprint));
292+
input.jump = input.jump || button_input.pressed(button(controller.pad_jump));
293+
input.fly = input.fly || button_input.just_pressed(button(controller.pad_fly));
294+
input.crouch = input.crouch || button_input.pressed(button(controller.pad_crouch));
288295
}
289296
}
290297
}
291298

292299
pub fn fps_controller_look(mut query: Query<(&mut FpsController, &FpsControllerInput)>) {
293300
for (mut controller, input) in query.iter_mut() {
294-
controller.pitch = input.pitch;
295-
controller.yaw = input.yaw;
301+
controller.pitch += input.pitch;
302+
controller.yaw += input.yaw;
296303
}
297304
}
298305

@@ -335,7 +342,7 @@ pub fn fps_controller_move(
335342
controller.fly_speed
336343
};
337344
let mut move_to_world =
338-
Mat3::from_euler(EulerRot::YXZ, input.yaw, input.pitch, 0.0);
345+
Mat3::from_euler(EulerRot::YXZ, controller.yaw, controller.pitch, 0.0);
339346
move_to_world.z_axis *= -1.0; // Forward is -Z
340347
move_to_world.y_axis = Vec3::Y; // Vertical movement aligned with world up
341348
velocity.linvel = move_to_world * input.movement * fly_speed;
@@ -364,7 +371,7 @@ pub fn fps_controller_move(
364371
);
365372

366373
let speeds = Vec3::new(controller.side_speed, 0.0, controller.forward_speed);
367-
let mut move_to_world = Mat3::from_axis_angle(Vec3::Y, input.yaw);
374+
let mut move_to_world = Mat3::from_axis_angle(Vec3::Y, controller.yaw);
368375
move_to_world.z_axis *= -1.0; // Forward is -Z
369376
let mut wish_direction = move_to_world * (input.movement * speeds);
370377
let mut wish_speed = wish_direction.length();

0 commit comments

Comments
 (0)