From 7bf8602796daf22cfc0c6f472b7cbad7cbe9ff4c Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Wed, 31 Jul 2024 17:14:09 -0500 Subject: [PATCH] Crosshair aims :tada: --- Cargo.toml | 2 +- src/input.rs | 36 ++++++++++++++++++++++++++++++++++-- src/player.rs | 12 +++++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e1a50d6..6f0bb25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,13 +22,13 @@ bevy = { version = "0.14.0", default-features = false, features = [ "wayland", "wav", # sound files "sysinfo_plugin", # probably will want this for troubleshooting or just surfacing useful information to the gamer (such as FPS?) + "bevy_gizmos", # debug geometry? # TODO: would be nice to get this working while developing "dynamic_linking", # NOTE: Features we may want at some point. # "vorbis", # music -- maybe mp3? - # "bevy_gizmos", # debug geometry? # "track_change_detection", # for hot-reloading of assets? (scripts?) # "file_watcher", # for hot-reloading of assets? (scripts?) # "serialize" # for saving games or game-related information? diff --git a/src/input.rs b/src/input.rs index 1036432..bd2ea11 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,4 +1,8 @@ -use bevy::prelude::*; +use bevy::{ + color::palettes::css::{GREEN, RED}, + prelude::*, + window::PrimaryWindow, +}; #[derive(Resource, Default, Debug, Clone, PartialEq)] pub struct Input { @@ -9,9 +13,11 @@ pub struct Input { } 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.; @@ -33,10 +39,36 @@ pub fn process_input( } } input.movement = input.movement.normalize_or_zero(); - // input.target // not set using keyboard, see mouse 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); + input.angle = modified_target.to_angle(); + info!( + "pos@{target:?} -- {center:?} -> {modified_target:?} for angle of {:?}", + input.angle + ); + } + } + info!("Input: {:#?}", input); } diff --git a/src/player.rs b/src/player.rs index af7ac6c..9c6a310 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,3 +1,5 @@ +use std::f32::consts::TAU; + use crate::camera::Watched; use crate::movement::{Heading, Mover, Speed, Velocity, YSortable}; use crate::prelude::*; @@ -89,10 +91,18 @@ pub fn startup( }); } -pub fn control(input: Res, mut player: Query<&mut Heading, With>) { +pub fn control( + input: Res, + mut crosshair: Query<&mut Transform, With>, + mut player: Query<&mut Heading, With>, + // time: Res