2025-05-14 23:14:41 +02:00
|
|
|
//! Sets up the game's camera controls etc.
|
2025-05-21 15:56:58 +02:00
|
|
|
//! Uses [bevy_third_person_camera](https://lib.rs/crates/bevy_third_person_camera)
|
|
|
|
use bevy::prelude::*;
|
|
|
|
use bevy_third_person_camera::*;
|
2025-05-14 23:14:41 +02:00
|
|
|
|
2025-06-17 20:50:46 +02:00
|
|
|
use crate::AppState;
|
2025-05-14 23:14:41 +02:00
|
|
|
|
|
|
|
use super::GameplaySet;
|
|
|
|
|
|
|
|
pub fn plugin(app: &mut App) {
|
2025-05-21 15:56:58 +02:00
|
|
|
app.add_plugins(ThirdPersonCameraPlugin)
|
|
|
|
.add_systems(
|
|
|
|
OnEnter(AppState::Ingame),
|
|
|
|
setup_game_camera.in_set(GameplaySet),
|
2025-05-14 23:14:41 +02:00
|
|
|
)
|
2025-06-16 17:21:13 +02:00
|
|
|
// .add_systems(Update, auto_target)
|
2025-05-21 15:56:58 +02:00
|
|
|
.add_systems(OnExit(AppState::Ingame), remove_tpc.in_set(GameplaySet));
|
2025-05-14 23:14:41 +02:00
|
|
|
}
|
|
|
|
|
2025-05-21 15:56:58 +02:00
|
|
|
/// Adds [ThirdPersonCamera] to our existing camera.
|
|
|
|
pub fn setup_game_camera(mut c: Commands, cam: Single<Entity, With<Camera3d>>) {
|
2025-06-10 20:11:08 +02:00
|
|
|
c.entity(*cam).insert(ThirdPersonCamera {
|
|
|
|
cursor_lock_key: KeyCode::Space,
|
|
|
|
cursor_lock_toggle_enabled: true,
|
|
|
|
gamepad_settings: CustomGamepadSettings::default(),
|
|
|
|
cursor_lock_active: true,
|
|
|
|
sensitivity: Vec2::new(1.0, 1.0),
|
|
|
|
mouse_orbit_button_enabled: true,
|
|
|
|
mouse_orbit_button: MouseButton::Middle,
|
|
|
|
offset_enabled: false,
|
|
|
|
offset: Offset::new(0.5, 0.4),
|
|
|
|
offset_toggle_enabled: false,
|
|
|
|
offset_toggle_speed: 5.0,
|
|
|
|
offset_toggle_key: KeyCode::KeyE,
|
|
|
|
zoom_enabled: true,
|
|
|
|
zoom: Zoom::new(1.5, 30.0),
|
|
|
|
zoom_sensitivity: 1.0,
|
|
|
|
..Default::default()
|
|
|
|
});
|
2025-05-21 15:56:58 +02:00
|
|
|
info!("Third person camera set up!")
|
2025-05-14 23:14:41 +02:00
|
|
|
}
|
|
|
|
|
2025-06-16 17:21:13 +02:00
|
|
|
// fn auto_target(
|
|
|
|
// mut c: Commands,
|
|
|
|
// without_custom: Query<Entity, (With<ThirdPersonCameraTarget>, Without<TPCTarget>)>,
|
|
|
|
// without_lib: Query<Entity, (Without<ThirdPersonCameraTarget>, With<TPCTarget>)>,
|
|
|
|
// ) {
|
|
|
|
// without_custom.iter().for_each(|e| {
|
|
|
|
// info!("Deleting ThirdPersonCameraTarget from {e}");
|
|
|
|
// c.entity(e).remove::<ThirdPersonCameraTarget>();
|
|
|
|
// });
|
|
|
|
// without_lib.iter().for_each(|e| {
|
|
|
|
// info!("Inserting TPCTarget into {e}");
|
|
|
|
// c.entity(e).insert(ThirdPersonCameraTarget);
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
2025-05-21 15:56:58 +02:00
|
|
|
/// Removes [ThirdPersonCamera] from the camera.
|
|
|
|
pub fn remove_tpc(mut c: Commands, cam: Single<Entity, (With<Camera3d>, With<ThirdPersonCamera>)>) {
|
|
|
|
c.entity(*cam).remove::<ThirdPersonCamera>();
|
2025-05-14 23:14:41 +02:00
|
|
|
}
|