Crosshair aims 🎉

This commit is contained in:
Daniel Flanagan 2024-07-31 17:14:09 -05:00
parent d05fc09e8a
commit 7bf8602796
3 changed files with 46 additions and 4 deletions

View file

@ -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?

View file

@ -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<Input>,
keys: Res<ButtonInput<KeyCode>>,
// mouse: Res<ButtonInput<MouseButton>>,
window: Query<&Window, With<PrimaryWindow>>,
) {
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);
}

View file

@ -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<Input>, mut player: Query<&mut Heading, With<Player>>) {
pub fn control(
input: Res<Input>,
mut crosshair: Query<&mut Transform, With<Crosshair>>,
mut player: Query<&mut Heading, With<Player>>,
// time: Res<Time>,
) {
if let Ok(mut heading) = player.get_single_mut() {
heading.0 = input.movement;
}
if let Ok(mut transform) = crosshair.get_single_mut() {
transform.rotation = Quat::from_rotation_z(input.angle + (TAU / 4.));
}
}
pub fn player_debug_info(mut player: Query<(&Heading, &Transform), With<Player>>) {