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));
}
}