better tracking

This commit is contained in:
Edgar 2023-05-06 09:55:09 +02:00
parent 3b6a67a4f6
commit 820444654a
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
3 changed files with 31 additions and 12 deletions

View file

@ -1,7 +1,9 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_inspector_egui::quick::WorldInspectorPlugin; use bevy_inspector_egui::quick::WorldInspectorPlugin;
use misc::AimTarget;
use physics::Velocity; use physics::Velocity;
pub mod misc;
pub mod physics; pub mod physics;
pub mod player; pub mod player;
pub mod tee; pub mod tee;
@ -9,13 +11,14 @@ pub mod tee;
fn main() { fn main() {
App::new() App::new()
.register_type::<Velocity>() .register_type::<Velocity>()
.register_type::<AimTarget>()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugin(WorldInspectorPlugin::new()) .add_plugin(WorldInspectorPlugin::new())
.add_startup_system(general_setup) .add_startup_system(general_setup)
.add_startup_system(player::add_player) .add_startup_system(player::add_player)
.add_system(player::player_input.before(physics::move_system)) .add_systems((player::player_input, player::player_mouse).before(physics::move_system))
.add_system(player::player_mouse.before(physics::move_system))
.add_system(physics::move_system) .add_system(physics::move_system)
.add_system(misc::aim_target_system.after(physics::move_system))
.run(); .run();
} }

15
src/misc.rs Normal file
View file

@ -0,0 +1,15 @@
use bevy::{math::Vec3Swizzles, prelude::*};
#[derive(Debug, Component, Reflect, Default)]
pub struct AimTarget(pub Option<Vec2>);
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;
}
}
}

View file

@ -1,6 +1,7 @@
use bevy::{math::Vec3Swizzles, prelude::*}; use bevy::{math::Vec3Swizzles, prelude::*, window::PrimaryWindow, winit::WinitWindows};
use crate::{ use crate::{
misc::AimTarget,
physics::Velocity, physics::Velocity,
tee::{TeeBundle, TeeBundleChildren}, tee::{TeeBundle, TeeBundleChildren},
MainCamera, MainCamera,
@ -17,7 +18,12 @@ pub fn add_player(mut commands: Commands, server: Res<AssetServer>) {
let tee_bundle_children = TeeBundleChildren::new(handle); let tee_bundle_children = TeeBundleChildren::new(handle);
commands commands
.spawn((Player, tee_bundle, Velocity::new(100.0))) .spawn((
Player,
tee_bundle,
Velocity::new(400.0),
AimTarget::default(),
))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(tee_bundle_children.body); parent.spawn(tee_bundle_children.body);
parent.spawn(tee_bundle_children.left_foot); parent.spawn(tee_bundle_children.left_foot);
@ -50,21 +56,16 @@ pub fn player_input(
pub fn player_mouse( pub fn player_mouse(
mut cursor_evr: EventReader<CursorMoved>, mut cursor_evr: EventReader<CursorMoved>,
mut query_player: Query<&mut Transform, With<Player>>, mut query_player: Query<&mut AimTarget, With<Player>>,
query_camera: Query<(&Camera, &GlobalTransform), With<MainCamera>>, query_camera: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
) { ) {
let (camera, camera_transform) = query_camera.single(); let (camera, camera_transform) = query_camera.single();
let mut target = query_player.single_mut();
for ev in cursor_evr.iter() { for ev in cursor_evr.iter() {
let real_pos = camera.viewport_to_world_2d(camera_transform, ev.position); let real_pos = camera.viewport_to_world_2d(camera_transform, ev.position);
if let Some(real_pos) = real_pos { if let Some(real_pos) = real_pos {
let mut transform = query_player.single_mut(); target.0 = Some(real_pos);
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;
} }
} }
} }