~starkingdoms/starkingdoms

ref: d733c4e3b55f6eaed09948f4309c32ab33f4c02b starkingdoms/crates/unified/src/client/key_input.rs -rw-r--r-- 4.2 KiB
d733c4e3 — core feat: avian! 28 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
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
use crate::attachment::{JointOf, Peer, SnapOf};
use crate::ecs::{Part, ThrustEvent};
use bevy::color::palettes::css::{FUCHSIA, GREEN, ORANGE, WHITE};
use bevy::dev_tools::picking_debug::DebugPickingMode;
use bevy::gizmos::gizmos::Gizmos;
use bevy::math::{Vec3, Vec3Swizzles};
use crate::prelude::*;
use bevy::{
    app::{App, Update},
    ecs::{message::MessageWriter, system::Res},
    input::{ButtonInput, keyboard::KeyCode},
};
use std::ops::Deref;
use avian2d::prelude::*;

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

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

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
#[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));
    }
}

fn draw_attachment_debug(
    joints: Query<(&Transform, &JointOf, Option<&Peer>)>,
    snaps: Query<(&Transform, &SnapOf)>,
    parts: Query<&GlobalTransform, With<Part>>,
    mut gizmos: Gizmos,
    state: ResMut<AttachmentDebugRes>,
) {
    if !state.0 {
        return;
    }
    for (offset, parent, peer) in joints.iter() {
        let Ok(parent_pos) = parts.get(parent.0) else {
            continue;
        };
        let joint_target = parent_pos.transform_point(offset.translation);
        gizmos.cross_2d(joint_target.xy(), 4.0, FUCHSIA);

        if let Some(peer_id) = peer
            && let Ok(peer) = joints.get(peer_id.peer_joint_entity_id)
        {
            let Ok(peer_parent_pos) = parts.get(peer.1.0) else {
                continue;
            };
            gizmos.arrow_2d(
                peer_parent_pos.translation().xy(),
                peer_parent_pos.translation().xy()
                    + ((peer.0.rotation * peer_parent_pos.rotation())
                        .mul_vec3(Vec3::Y)
                        .xy()
                        * 20.0),
                ORANGE,
            );
            gizmos.arrow_2d(
                parent_pos.translation().xy(),
                peer_parent_pos.translation().xy(),
                WHITE,
            );
        }
    }
    for (offset, parent) in snaps.iter() {
        let Ok(parent_pos) = parts.get(parent.0) else {
            continue;
        };
        let joint_snap = parent_pos.transform_point(offset.translation);
        gizmos.cross_2d(joint_snap.xy(), 4.0, GREEN);
    }
}