35 lines
969 B
Rust
35 lines
969 B
Rust
use bevy::prelude::*;
|
|
|
|
use crate::player::*;
|
|
|
|
pub struct Camera;
|
|
pub fn camera_follow_player(
|
|
player: Query<(&Transform, &Player), Without<Camera>>,
|
|
mut camera: Query<(&mut Transform, &Camera), Without<Player>>,
|
|
time: Res<Time>,
|
|
) {
|
|
let player_pos = if let Some(player) = player.iter().next() {
|
|
player.0.translation
|
|
} else {
|
|
return;
|
|
};
|
|
|
|
let ds = time.delta_seconds() * 5.0;
|
|
// TODO Change this to normal movement
|
|
for (mut trans, _) in camera.iter_mut() {
|
|
trans.look_at(player_pos, Vec3::Y);
|
|
|
|
// keep a distance to the player
|
|
if trans.translation.distance(player_pos) > 170.0 {
|
|
let mut d = trans.rotation * Vec3::Z * ds;
|
|
d.y = 0.0;
|
|
trans.translation -= d;
|
|
}
|
|
if trans.translation.distance(player_pos) < 130.0 {
|
|
let mut d = trans.rotation * Vec3::Z * ds;
|
|
d.y = 0.0;
|
|
trans.translation += d;
|
|
}
|
|
}
|
|
}
|