cyber_rider/src/camera.rs

42 lines
1.2 KiB
Rust
Raw Normal View History

use bevy::prelude::*;
use crate::geometry::{CyberBike, SPAWN_ALTITUDE};
2022-01-14 00:14:08 +00:00
pub(crate) const CAM_DIST: f32 = 15.0;
#[derive(Component, Debug)]
pub struct CyberCam;
fn setup_cybercam(mut commands: Commands) {
commands
.spawn_bundle(PerspectiveCameraBundle {
2022-01-14 00:14:08 +00:00
transform: Transform::from_xyz(SPAWN_ALTITUDE + CAM_DIST, 0.0, 0.0)
.looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
})
.insert(CyberCam);
}
fn follow_cyberbike(
2022-01-14 00:14:08 +00:00
bike_query: Query<&Transform, (Without<CyberCam>, With<CyberBike>)>,
mut cam_query: Query<&mut Transform, (With<CyberCam>, Without<CyberBike>)>,
) {
2022-01-14 00:14:08 +00:00
let bike_xform = bike_query.single();
let up = bike_xform.translation.normalize();
2022-01-14 00:14:08 +00:00
let look_at = bike_xform.translation + (bike_xform.forward() * CAM_DIST);
let cam_pos = bike_xform.translation + (bike_xform.back() * CAM_DIST * 1.5) + (up * CAM_DIST);
2022-01-14 00:14:08 +00:00
let mut cam_xform = cam_query.single_mut();
cam_xform.translation = cam_pos;
cam_xform.look_at(look_at, up);
}
pub struct CyberCamPlugin;
impl Plugin for CyberCamPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.add_startup_system(setup_cybercam)
.add_system(follow_cyberbike);
}
}