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