use bevy::{prelude::*, window::PrimaryWindow};
#[derive(Resource, Default, Debug, Clone, PartialEq)]
pub struct Input {
pub movement: Vec2,
pub angle: f32,
pub action: bool,
pub cancel: bool,
}
pub fn process_input(
mut _gizmos: Gizmos,
mut input: ResMut,
keys: Res>,
// mouse: Res>,
window: Query<&Window, With>,
) {
input.movement.x = 0.;
input.movement.y = 0.;
for key in keys.get_pressed() {
match key {
KeyCode::KeyA | KeyCode::ArrowLeft => {
input.movement -= Vec2::X;
}
KeyCode::KeyD | KeyCode::ArrowRight => {
input.movement += Vec2::X;
}
KeyCode::KeyW | KeyCode::ArrowUp => {
input.movement += Vec2::Y;
}
KeyCode::KeyS | KeyCode::ArrowDown => {
input.movement -= Vec2::Y;
}
_ => {}
}
}
input.movement = input.movement.normalize_or_zero();
input.action = input.action || keys.any_pressed([KeyCode::Space, KeyCode::Enter]);
input.cancel = input.cancel || keys.just_pressed(KeyCode::Escape);
// FOR CURSOR POSITION
// origin is top-left
// x increases as mouse goes right
// y increases as mouse goes down
// FOR 2D WORLD COORDINATE
// origin is bottom-left
// x increases as thing moves right
// y increases as thing moves up
// with this in mind, to convert mouse coordinates to world coordinates
// for comparing against our logical window center, we take the absolute of the
// difference in height
// gizmos.line(Vec3::ZERO, Vec3::X * 100., GREEN);
if let Ok(window) = window.get_single() {
if let Some(target) = window.cursor_position() {
let size = window.size();
let center = size / 2.;
let modified_target = target.with_y(size.y - target.y) - center;
// gizmos.line(Vec2::ZERO.extend(0.), modified_target.extend(0.), RED);
// the angle of the resulting vector should be the angle from the
// center of the screen to the mouse
input.angle = modified_target.to_angle();
}
}
// info!("Input: {:#?}", input);
}
// TODO: controller input: https://bevy-cheatbook.github.io/input/gamepad.html
// Iterate controllers on startup and assign to players/units?
// What logic for handling controller connection/disconnection?
// Will we have splitscreen?
// Once a controller is assigned to a player, we can handle its input
// pub fn controller_input(
// axes: Res>,
// buttons: Res>,
// my_gamepad: Option>,
// ) {
// }