~starkingdoms/starkingdoms

ref: 103444e57478b2f702c26a6dfb9a62b34a60e5fe starkingdoms/crates/unified/src/client/key_input.rs -rw-r--r-- 1.5 KiB
103444e5 — core chore(wasm)!: fix compilations on webassembly 5 months 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
use bevy::{app::{App, Update}, ecs::{event::EventWriter, system::Res}, input::{keyboard::KeyCode, ButtonInput}};

use crate::ecs::ThrustEvent;

pub fn key_input_plugin(app: &mut App) {
    app.add_systems(Update, directional_keys);
}

pub fn directional_keys(
    keys: Res<ButtonInput<KeyCode>>,
    mut thrust_event: EventWriter<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));
    }
}