Fly controller (#26)

This commit is contained in:
extrawurst
2025-04-12 17:16:41 +02:00
committed by GitHub
parent dab4ea0656
commit 9b3765c41d
10 changed files with 550 additions and 325 deletions

View File

@@ -3,20 +3,26 @@ use bevy::prelude::*;
use crate::GameState;
mod collisions;
pub mod controller;
pub mod controller_common;
pub mod controller_flying;
pub mod controller_running;
pub mod controls;
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone, Default)]
enum ControllerSet {
CollectInputs,
ApplyControls,
ApplyControlsFly,
#[default]
ApplyControlsRun,
}
#[derive(Resource, Debug, Clone, Copy, Default, PartialEq)]
pub struct ControlState {
/// Movement direction with a maximum length of 1.0
pub move_dir: Vec2,
pub look_dir: Vec2,
pub jump: bool,
/// Determines if the camera can rotate freely around the player
pub view_mode: bool,
}
@@ -26,12 +32,54 @@ pub struct Controls {
pub gamepad_state: Option<ControlState>,
}
#[derive(Event)]
pub struct ControllerSwitchEvent;
#[derive(Resource, Debug, Default, PartialEq)]
pub struct SelectedController(ControllerSet);
pub fn plugin(app: &mut App) {
app.init_resource::<SelectedController>();
app.add_plugins(controls::plugin);
app.add_plugins(controller_common::plugin);
app.add_plugins(controller_flying::CharacterControllerPlugin);
app.add_plugins(controller_running::CharacterControllerPlugin);
app.add_event::<ControllerSwitchEvent>();
app.add_systems(
Update,
switch_controller.before(ControllerSet::CollectInputs),
);
app.configure_sets(
Update,
(ControllerSet::CollectInputs, ControllerSet::ApplyControls)
(
ControllerSet::CollectInputs,
ControllerSet::ApplyControlsFly.run_if(resource_equals(SelectedController(
ControllerSet::ApplyControlsFly,
))),
ControllerSet::ApplyControlsRun.run_if(resource_equals(SelectedController(
ControllerSet::ApplyControlsRun,
))),
)
.chain()
.run_if(in_state(GameState::Playing)),
);
}
fn switch_controller(
mut selected_controller: ResMut<SelectedController>,
keyboard: Res<ButtonInput<KeyCode>>,
mut event_controller_switch: EventWriter<ControllerSwitchEvent>,
) {
if keyboard.just_pressed(KeyCode::KeyF) {
event_controller_switch.send(ControllerSwitchEvent);
if selected_controller.0 == ControllerSet::ApplyControlsFly {
selected_controller.0 = ControllerSet::ApplyControlsRun;
} else {
selected_controller.0 = ControllerSet::ApplyControlsFly;
}
}
}