use bevy::prelude::*; use crate::{bike::CyberBikeBody, input::InputState}; // 85 degrees in radians const MAX_PITCH: f32 = 1.48353; #[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Component, States, Default)] enum CyberCameras { #[default] Hero, Debug, } #[derive(Debug, Resource)] pub struct DebugCamOffset { pub rot: f32, pub dist: f32, pub alt: f32, } impl Default for DebugCamOffset { fn default() -> Self { DebugCamOffset { rot: 60.0, dist: 10.0, alt: 4.0, } } } impl CyberCameras { fn next(self) -> Self { match self { CyberCameras::Debug => CyberCameras::Hero, CyberCameras::Hero => CyberCameras::Debug, } } } fn setup_cybercams(mut commands: Commands) { let hero_projection = PerspectiveProjection { fov: std::f32::consts::FRAC_PI_3, ..Default::default() }; let fog_settings = FogSettings { color: Color::rgba(0.1, 0.2, 0.4, 1.0), directional_light_color: Color::rgba(1.0, 0.95, 0.75, 0.5), directional_light_exponent: 30.0, falloff: FogFalloff::from_visibility_colors( 350.0, /* distance in world units up to which objects retain visibility (>= 5% * contrast) */ Color::rgb(0.35, 0.5, 0.66), /* atmospheric extinction color (after light is lost * due to absorption by atmospheric particles) */ Color::rgb(0.8, 0.844, 1.0), /* atmospheric inscattering color (light gained due to * scattering from the sun) */ ), }; commands .spawn(Camera3dBundle { projection: bevy::render::camera::Projection::Perspective(hero_projection), ..Default::default() }) .insert(fog_settings.clone()) .insert(CyberCameras::Hero); commands .spawn(Camera3dBundle::default()) .insert(fog_settings) .insert(CyberCameras::Debug); } fn follow_cyberbike( mut query: ParamSet<( // 0: the bike Query<&Transform, With>, // 1: the cameras Query<(&mut Transform, &CyberCameras)>, )>, input: Res, offset: Res, ) { let bike_xform = *query.p0().single(); let up = bike_xform.translation.normalize(); for (mut cam_xform, cam_type) in query.p1().iter_mut() { match *cam_type { CyberCameras::Hero => { let look_at = bike_xform.translation + (bike_xform.forward() * 500.0); let cam_pos = bike_xform.translation + (bike_xform.back() * 0.1) + (up * 0.8); cam_xform.translation = cam_pos; cam_xform.look_at(look_at, up); // 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 mut ncx = bike_xform.to_owned(); ncx.rotate(Quat::from_axis_angle(up, offset.rot.to_radians())); ncx.translation += ncx.forward() * offset.dist; ncx.translation += ncx.up() * offset.alt; *cam_xform = ncx; cam_xform.look_at(bike_xform.translation, up); } } } } fn update_active_camera( state: Res>, mut query: Query<(&mut Camera, &CyberCameras)>, ) { // find the camera with the current state, set it as the ActiveCamera query.iter_mut().for_each(|(mut cam, cyber)| { if cyber.eq(&state.0) { cam.is_active = true; } else { cam.is_active = false; } }); } fn cycle_cam_state( state: Res>, mut next: ResMut>, mut keys: ResMut>, ) { if keys.just_pressed(KeyCode::D) { let new_state = state.0.next(); info!("{:?}", new_state); next.set(new_state); keys.reset(KeyCode::D); } } pub struct CyberCamPlugin; impl Plugin for CyberCamPlugin { fn build(&self, app: &mut bevy::prelude::App) { common(app); } } fn common(app: &mut bevy::prelude::App) { app.insert_resource(DebugCamOffset::default()) .add_startup_system(setup_cybercams) .add_state::() .add_system(cycle_cam_state) .add_system(update_active_camera) .add_system(follow_cyberbike); }