camera moves

This commit is contained in:
Edgar 2023-06-29 10:56:38 +02:00
parent a7c4802c6d
commit cefff9652b
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
2 changed files with 28 additions and 0 deletions

View file

@ -1,3 +1,5 @@
#![allow(clippy::type_complexity)]
use bevy::prelude::*; use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*; use bevy_ecs_tilemap::prelude::*;
use bevy_inspector_egui::quick::WorldInspectorPlugin; 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_systems((player::player_input, 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)) .add_system(misc::aim_target_system.after(physics::move_system))
.add_system(player::player_camera.after(misc::aim_target_system))
.run(); .run();
} }

View file

@ -71,3 +71,28 @@ pub fn player_mouse(
} }
} }
} }
pub fn player_camera(
mut query_player: Query<(&mut AimTarget, &Transform), (With<Player>, Without<MainCamera>)>,
mut query_camera: Query<&mut Transform, With<MainCamera>>,
) {
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();
}
}
}