2023-01-20 22:40:51 +00:00
|
|
|
use bevy::prelude::*;
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-03-13 00:06:36 +00:00
|
|
|
use crate::{bike::CyberBikeModel, input::InputState};
|
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,
|
|
|
|
}
|
|
|
|
|
2023-01-23 05:06:08 +00:00
|
|
|
#[derive(Debug, Resource)]
|
|
|
|
pub struct DebugCamOffset {
|
|
|
|
pub rot: f32,
|
|
|
|
pub dist: f32,
|
2023-01-24 04:12:15 +00:00
|
|
|
pub alt: f32,
|
2023-01-23 05:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for DebugCamOffset {
|
|
|
|
fn default() -> Self {
|
|
|
|
DebugCamOffset {
|
|
|
|
rot: 60.0,
|
|
|
|
dist: 10.0,
|
2023-01-24 04:12:15 +00:00
|
|
|
alt: 4.0,
|
2023-01-23 05:06:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 05:42:16 +00:00
|
|
|
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
|
2023-01-20 22:40:51 +00:00
|
|
|
.spawn(Camera3dBundle {
|
2022-08-13 22:30:37 +00:00
|
|
|
projection: bevy::render::camera::Projection::Perspective(hero_projection),
|
2022-01-12 08:18:13 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
2022-03-02 05:42:16 +00:00
|
|
|
.insert(CyberCameras::Hero);
|
|
|
|
|
|
|
|
commands
|
2023-01-20 22:40:51 +00:00
|
|
|
.spawn(Camera3dBundle::default())
|
2022-03-02 05:42:16 +00:00
|
|
|
.insert(CyberCameras::Debug);
|
2022-01-12 08:18:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 01:32:39 +00:00
|
|
|
fn follow_cyberbike(
|
2022-05-08 17:58:48 +00:00
|
|
|
mut query: ParamSet<(
|
2022-03-02 05:42:16 +00:00
|
|
|
// 0: the bike
|
2022-05-08 17:58:48 +00:00
|
|
|
Query<&Transform, With<CyberBikeModel>>,
|
2022-03-02 05:42:16 +00:00
|
|
|
// 1: the cameras
|
2022-05-08 17:58:48 +00:00
|
|
|
Query<(&mut Transform, &CyberCameras)>,
|
2022-03-02 05:42:16 +00:00
|
|
|
)>,
|
2022-01-27 00:53:37 +00:00
|
|
|
input: Res<InputState>,
|
2023-01-23 05:06:08 +00:00
|
|
|
offset: Res<DebugCamOffset>,
|
2022-01-12 08:18:13 +00:00
|
|
|
) {
|
2022-05-08 17:58:48 +00:00
|
|
|
let bike_xform = *query.p0().single();
|
2022-01-12 08:18:13 +00:00
|
|
|
let up = bike_xform.translation.normalize();
|
|
|
|
|
2022-05-08 17:58:48 +00:00
|
|
|
for (mut cam_xform, cam_type) in query.p1().iter_mut() {
|
2022-03-02 05:42:16 +00:00
|
|
|
match *cam_type {
|
|
|
|
CyberCameras::Hero => {
|
|
|
|
let look_at = bike_xform.translation + (bike_xform.forward() * 200.0);
|
2023-01-21 07:05:43 +00:00
|
|
|
let cam_pos = bike_xform.translation + (bike_xform.back() * 1.05) + (up * 1.08);
|
2022-03-02 05:42:16 +00:00
|
|
|
|
|
|
|
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 => {
|
2023-01-23 05:06:08 +00:00
|
|
|
let mut ncx = bike_xform.to_owned();
|
|
|
|
ncx.rotate(Quat::from_axis_angle(up, offset.rot.to_radians()));
|
|
|
|
ncx.translation += ncx.forward() * offset.dist;
|
2023-01-24 04:12:15 +00:00
|
|
|
ncx.translation += ncx.up() * offset.alt;
|
2023-01-23 05:06:08 +00:00
|
|
|
*cam_xform = ncx;
|
2022-03-02 05:42:16 +00:00
|
|
|
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(
|
|
|
|
state: Res<State<CyberCameras>>,
|
2022-08-13 22:30:37 +00:00
|
|
|
mut query: Query<(&mut Camera, &CyberCameras)>,
|
2022-03-02 05:42:16 +00:00
|
|
|
) {
|
2022-05-08 17:58:48 +00:00
|
|
|
// find the camera with the current state, set it as the ActiveCamera
|
2022-08-13 22:30:37 +00:00
|
|
|
query.iter_mut().for_each(|(mut cam, cyber)| {
|
|
|
|
if cyber.eq(state.current()) {
|
|
|
|
cam.is_active = true;
|
|
|
|
} else {
|
|
|
|
cam.is_active = false;
|
|
|
|
}
|
|
|
|
});
|
2022-03-02 05:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-01-26 01:10:23 +00:00
|
|
|
|
2022-01-12 08:18:13 +00:00
|
|
|
impl Plugin for CyberCamPlugin {
|
|
|
|
fn build(&self, app: &mut bevy::prelude::App) {
|
2023-01-27 01:16:05 +00:00
|
|
|
// common stuff
|
|
|
|
app.insert_resource(DebugCamOffset::default())
|
|
|
|
.add_startup_system(setup_cybercams)
|
2022-03-14 06:37:11 +00:00
|
|
|
.add_state(CyberCameras::Hero)
|
2022-03-02 05:42:16 +00:00
|
|
|
.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
|
|
|
}
|
|
|
|
}
|