2022-03-02 05:42:16 +00:00
|
|
|
use bevy::{
|
|
|
|
prelude::*,
|
|
|
|
render::camera::{ActiveCameras, Camera, CameraPlugin},
|
2022-01-27 00:53:37 +00:00
|
|
|
};
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-03-12 22:56:32 +00:00
|
|
|
use crate::{input::InputState, static_geometry::CyberBikeModel};
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-01-27 00:53:37 +00:00
|
|
|
// 85 degrees in radians
|
|
|
|
const MAX_PITCH: f32 = 1.48353;
|
|
|
|
|
2022-03-02 05:42:16 +00:00
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Component)]
|
|
|
|
enum CyberCameras {
|
|
|
|
Hero,
|
|
|
|
Debug,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CyberCameras {
|
|
|
|
fn next(self) -> Self {
|
|
|
|
match self {
|
|
|
|
CyberCameras::Debug => CyberCameras::Hero,
|
|
|
|
CyberCameras::Hero => CyberCameras::Debug,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-03-02 05:42:16 +00:00
|
|
|
fn setup_cybercams(mut commands: Commands) {
|
|
|
|
let hero_projection = PerspectiveProjection {
|
2022-02-24 01:38:24 +00:00
|
|
|
fov: std::f32::consts::FRAC_PI_3,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-01-12 08:18:13 +00:00
|
|
|
commands
|
|
|
|
.spawn_bundle(PerspectiveCameraBundle {
|
2022-03-02 05:42:16 +00:00
|
|
|
perspective_projection: hero_projection,
|
2022-01-12 08:18:13 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
2022-03-02 05:42:16 +00:00
|
|
|
.insert(CyberCameras::Hero);
|
|
|
|
|
|
|
|
commands
|
|
|
|
.spawn_bundle(PerspectiveCameraBundle::with_name("Inactive"))
|
|
|
|
.insert(CyberCameras::Debug);
|
2022-01-12 08:18:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 01:32:39 +00:00
|
|
|
fn follow_cyberbike(
|
2022-03-02 05:42:16 +00:00
|
|
|
mut query: QuerySet<(
|
|
|
|
// 0: the bike
|
|
|
|
QueryState<&Transform, With<CyberBikeModel>>,
|
|
|
|
// 1: the cameras
|
|
|
|
QueryState<(&mut Transform, &CyberCameras)>,
|
|
|
|
)>,
|
2022-01-27 00:53:37 +00:00
|
|
|
input: Res<InputState>,
|
2022-01-12 08:18:13 +00:00
|
|
|
) {
|
2022-03-02 05:42:16 +00:00
|
|
|
let bike_xform = *query.q0().single();
|
2022-01-12 08:18:13 +00:00
|
|
|
let up = bike_xform.translation.normalize();
|
|
|
|
|
2022-03-02 05:42:16 +00:00
|
|
|
for (mut cam_xform, cam_type) in query.q1().iter_mut() {
|
|
|
|
match *cam_type {
|
|
|
|
CyberCameras::Hero => {
|
|
|
|
let look_at = bike_xform.translation + (bike_xform.forward() * 200.0);
|
|
|
|
let cam_pos = bike_xform.translation + (bike_xform.back() * 2.7) + (up * 2.4);
|
|
|
|
|
|
|
|
cam_xform.translation = cam_pos;
|
|
|
|
cam_xform.look_at(look_at, up);
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-03-02 05:42:16 +00:00
|
|
|
// handle input pitch
|
|
|
|
let angle = input.pitch.powi(3) * MAX_PITCH;
|
|
|
|
let axis = cam_xform.right();
|
|
|
|
cam_xform.rotate(Quat::from_axis_angle(axis, angle));
|
|
|
|
}
|
|
|
|
CyberCameras::Debug => {
|
|
|
|
let pos = bike_xform.translation
|
|
|
|
+ (bike_xform.forward() * 10.0)
|
|
|
|
+ (bike_xform.left() * 30.0)
|
|
|
|
+ (bike_xform.up() * 7.0);
|
|
|
|
cam_xform.translation = pos;
|
|
|
|
cam_xform.look_at(bike_xform.translation, up);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 00:53:37 +00:00
|
|
|
|
2022-03-02 05:42:16 +00:00
|
|
|
fn update_active_camera(
|
|
|
|
mut active_cams: ResMut<ActiveCameras>,
|
|
|
|
state: Res<State<CyberCameras>>,
|
|
|
|
mut query: Query<(&mut Camera, &CyberCameras)>,
|
|
|
|
) {
|
|
|
|
active_cams.remove(CameraPlugin::CAMERA_3D);
|
|
|
|
|
|
|
|
// set all cameras to inactive
|
|
|
|
for (mut cam, _) in query.iter_mut() {
|
|
|
|
cam.name = Some("Inactive".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the camera with the current state, set its name to be active
|
|
|
|
for (mut cam, _) in query
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|(_, cybercam)| state.current().eq(cybercam))
|
|
|
|
{
|
|
|
|
cam.name = Some(CameraPlugin::CAMERA_3D.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
active_cams.add(CameraPlugin::CAMERA_3D);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cycle_cam_state(mut state: ResMut<State<CyberCameras>>, mut keys: ResMut<Input<KeyCode>>) {
|
|
|
|
if keys.just_pressed(KeyCode::D) {
|
|
|
|
let new_state = state.current().next();
|
|
|
|
info!("{:?}", new_state);
|
|
|
|
state.set(new_state).unwrap();
|
|
|
|
keys.reset(KeyCode::D);
|
|
|
|
}
|
2022-01-12 08:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CyberCamPlugin;
|
|
|
|
impl Plugin for CyberCamPlugin {
|
|
|
|
fn build(&self, app: &mut bevy::prelude::App) {
|
2022-03-02 05:42:16 +00:00
|
|
|
app.add_startup_system(setup_cybercams)
|
|
|
|
.add_state(CyberCameras::Hero)
|
|
|
|
.add_system(cycle_cam_state)
|
|
|
|
.add_system(update_active_camera)
|
2022-01-14 01:32:39 +00:00
|
|
|
.add_system(follow_cyberbike);
|
2022-01-12 08:18:13 +00:00
|
|
|
}
|
|
|
|
}
|