~starkingdoms/starkingdoms

ref: 54589315a90d94a48978d94420ff0a656d81a2df starkingdoms/crates/unified/src/client/key_input.rs -rw-r--r-- 2.3 KiB
54589315 — core feat: thruster metadata 24 days ago
                                                                                
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
use crate::ecs::ThrustEvent;
use bevy::dev_tools::picking_debug::DebugPickingMode;
use crate::prelude::*;
use bevy::{
    app::{App, Update},
    ecs::{message::MessageWriter, system::Res},
    input::{ButtonInput, keyboard::KeyCode},
};
use std::ops::Deref;
use crate::client::ship::attachment::AttachmentDebugRes;

pub fn key_input_plugin(app: &mut App) {
    app.add_systems(Update, directional_keys)
        .add_systems(Update, debug_render_keybind)
        .init_resource::<PhysicsDebugRes>();
}


#[derive(Resource, Default)]
pub struct PhysicsDebugRes(pub bool);
impl Deref for PhysicsDebugRes {
    type Target = bool;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

fn debug_render_keybind(
    keys: Res<ButtonInput<KeyCode>>,
    mut picking_debug_mode: ResMut<DebugPickingMode>,
    mut attachment_debug: ResMut<AttachmentDebugRes>,
) {
    if keys.just_pressed(KeyCode::F4) {
        *picking_debug_mode = DebugPickingMode::Noisy;
    }
    if keys.just_pressed(KeyCode::F5) {
        attachment_debug.0 = !attachment_debug.0;
    }
}

fn directional_keys(keys: Res<ButtonInput<KeyCode>>, mut thrust_event: MessageWriter<ThrustEvent>) {
    if keys.just_pressed(KeyCode::KeyW) || keys.just_pressed(KeyCode::ArrowUp) {
        thrust_event.write(ThrustEvent::Up(true));
    } else if keys.just_released(KeyCode::KeyW) || keys.just_released(KeyCode::ArrowUp) {
        thrust_event.write(ThrustEvent::Up(false));
    }

    if keys.just_pressed(KeyCode::KeyS) || keys.just_pressed(KeyCode::ArrowDown) {
        thrust_event.write(ThrustEvent::Down(true));
    } else if keys.just_released(KeyCode::KeyS) || keys.just_released(KeyCode::ArrowDown) {
        thrust_event.write(ThrustEvent::Down(false));
    }

    if keys.just_pressed(KeyCode::KeyA) || keys.just_pressed(KeyCode::ArrowLeft) {
        thrust_event.write(ThrustEvent::Left(true));
    } else if keys.just_released(KeyCode::KeyA) || keys.just_released(KeyCode::ArrowLeft) {
        thrust_event.write(ThrustEvent::Left(false));
    }

    if keys.just_pressed(KeyCode::KeyD) || keys.just_pressed(KeyCode::ArrowRight) {
        thrust_event.write(ThrustEvent::Right(true));
    } else if keys.just_released(KeyCode::KeyD) || keys.just_released(KeyCode::ArrowRight) {
        thrust_event.write(ThrustEvent::Right(false));
    }
}