diff --git a/src/main.rs b/src/main.rs index cebe712..36d0c88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ use bevy::prelude::*; use bevy_inspector_egui::quick::WorldInspectorPlugin; +use misc::AimTarget; use physics::Velocity; +pub mod misc; pub mod physics; pub mod player; pub mod tee; @@ -9,13 +11,14 @@ pub mod tee; fn main() { App::new() .register_type::() + .register_type::() .add_plugins(DefaultPlugins) .add_plugin(WorldInspectorPlugin::new()) .add_startup_system(general_setup) .add_startup_system(player::add_player) - .add_system(player::player_input.before(physics::move_system)) - .add_system(player::player_mouse.before(physics::move_system)) + .add_systems((player::player_input, player::player_mouse).before(physics::move_system)) .add_system(physics::move_system) + .add_system(misc::aim_target_system.after(physics::move_system)) .run(); } diff --git a/src/misc.rs b/src/misc.rs new file mode 100644 index 0000000..daa4632 --- /dev/null +++ b/src/misc.rs @@ -0,0 +1,15 @@ +use bevy::{math::Vec3Swizzles, prelude::*}; + +#[derive(Debug, Component, Reflect, Default)] +pub struct AimTarget(pub Option); + +pub fn aim_target_system(mut query: Query<(&mut Transform, &AimTarget)>) { + for (mut t, target) in query.iter_mut().filter(|(_, b)| b.0.is_some()) { + if let Some(target) = target.0 { + let translation = t.translation.xy(); + let vector = (target - translation).normalize(); + let rotate_to_target = Quat::from_rotation_arc(Vec3::Y, vector.extend(0.0)); + t.rotation = rotate_to_target; + } + } +} diff --git a/src/player.rs b/src/player.rs index ee467d9..a69714e 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,6 +1,7 @@ -use bevy::{math::Vec3Swizzles, prelude::*}; +use bevy::{math::Vec3Swizzles, prelude::*, window::PrimaryWindow, winit::WinitWindows}; use crate::{ + misc::AimTarget, physics::Velocity, tee::{TeeBundle, TeeBundleChildren}, MainCamera, @@ -17,7 +18,12 @@ pub fn add_player(mut commands: Commands, server: Res) { let tee_bundle_children = TeeBundleChildren::new(handle); commands - .spawn((Player, tee_bundle, Velocity::new(100.0))) + .spawn(( + Player, + tee_bundle, + Velocity::new(400.0), + AimTarget::default(), + )) .with_children(|parent| { parent.spawn(tee_bundle_children.body); parent.spawn(tee_bundle_children.left_foot); @@ -50,21 +56,16 @@ pub fn player_input( pub fn player_mouse( mut cursor_evr: EventReader, - mut query_player: Query<&mut Transform, With>, + mut query_player: Query<&mut AimTarget, With>, query_camera: Query<(&Camera, &GlobalTransform), With>, ) { let (camera, camera_transform) = query_camera.single(); + let mut target = query_player.single_mut(); for ev in cursor_evr.iter() { let real_pos = camera.viewport_to_world_2d(camera_transform, ev.position); if let Some(real_pos) = real_pos { - let mut transform = query_player.single_mut(); - let translation = transform.translation.xy(); - let vector = (real_pos - translation).normalize(); - - let rotate_to_mouse = Quat::from_rotation_arc(Vec3::Y, vector.extend(0.0)); - - transform.rotation = rotate_to_mouse; + target.0 = Some(real_pos); } } }