32 lines
955 B
Rust
32 lines
955 B
Rust
use bevy::prelude::*;
|
|
|
|
use crate::{
|
|
components::camera::{FollowCam, FollowTarget},
|
|
states::play::PlaySystems,
|
|
};
|
|
|
|
pub struct FollowCamPlugin;
|
|
|
|
impl Plugin for FollowCamPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_systems(Update, (set_cam_target, follow_cam).chain().in_set(PlaySystems));
|
|
}
|
|
}
|
|
|
|
fn set_cam_target(transforms: Query<&GlobalTransform>, cam_target: Single<(&mut FollowTarget, &FollowCam)>) {
|
|
let (mut tgt, cam) = cam_target.into_inner();
|
|
if let Ok(tgt_transform) = transforms.get(cam.target) {
|
|
tgt.pos = tgt_transform.translation();
|
|
tgt.rot = tgt_transform.rotation();
|
|
tgt.up = tgt_transform.up();
|
|
}
|
|
}
|
|
|
|
fn follow_cam(cam: Single<(&mut Transform, &FollowTarget, &FollowCam)>) {
|
|
let (mut transform, tgt, cam) = cam.into_inner();
|
|
|
|
let offset = tgt.rot * Vec3::new(0.0, cam.height, cam.distance);
|
|
transform.translation = offset + tgt.pos;
|
|
*transform = transform.looking_at(tgt.pos, tgt.up);
|
|
}
|