From cefff9652b9ec037ea59be5034c08ff985431ad4 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Thu, 29 Jun 2023 10:56:38 +0200 Subject: [PATCH] camera moves --- src/main.rs | 3 +++ src/player.rs | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main.rs b/src/main.rs index 48508c6..aa97461 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![allow(clippy::type_complexity)] + use bevy::prelude::*; use bevy_ecs_tilemap::prelude::*; use bevy_inspector_egui::quick::WorldInspectorPlugin; @@ -27,6 +29,7 @@ fn main() { .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)) + .add_system(player::player_camera.after(misc::aim_target_system)) .run(); } diff --git a/src/player.rs b/src/player.rs index bb37dd9..6c75762 100644 --- a/src/player.rs +++ b/src/player.rs @@ -71,3 +71,28 @@ pub fn player_mouse( } } } + +pub fn player_camera( + mut query_player: Query<(&mut AimTarget, &Transform), (With, Without)>, + mut query_camera: Query<&mut Transform, With>, +) { + let mut camera_transform = query_camera.single_mut(); + let (mut aim_target, player_transform) = query_player.single_mut(); + + let old = camera_transform.translation; + + camera_transform.translation = Vec3::new( + player_transform.translation.x, + player_transform.translation.y, + camera_transform.translation.z, + ); + + // Keep the aim same when camera is moving + let diff = camera_transform.translation - old; + + if diff.length() > 0.0 { + if let Some(t) = aim_target.0.as_mut() { + *t += diff.xy(); + } + } +}