80 lines
1.9 KiB
Rust
80 lines
1.9 KiB
Rust
use bevy::prelude::*;
|
|
#[cfg(feature = "dev")]
|
|
use bevy::window::PrimaryWindow;
|
|
use bevy_rapier3d::plugin::PhysicsSet;
|
|
|
|
use crate::{components::camera::*, states::play::PlaySystems};
|
|
|
|
pub struct CameraPlugin;
|
|
|
|
impl Plugin for CameraPlugin
|
|
{
|
|
fn build(&self, app: &mut App)
|
|
{
|
|
app.add_systems(Update, camera_pitch.in_set(PlaySystems));
|
|
app.add_systems(
|
|
PostUpdate,
|
|
//Update after physics moves entities
|
|
camera_attachment.in_set(PlaySystems).after(PhysicsSet::Writeback),
|
|
);
|
|
#[cfg(feature = "dev")]
|
|
app.add_systems(Update, camera_toggle.in_set(PlaySystems));
|
|
}
|
|
}
|
|
|
|
pub fn camera_attachment(
|
|
attachment_targets: Query<&GlobalTransform>,
|
|
cam: Single<(&mut Transform, &CameraAttachment, &CameraMode)>,
|
|
)
|
|
{
|
|
let (mut transform, attach, mode) = cam.into_inner();
|
|
|
|
if let Ok(tgt) = attachment_targets.get(attach.0)
|
|
{
|
|
match mode
|
|
{
|
|
CameraMode::Player =>
|
|
{
|
|
transform.rotation = tgt.rotation();
|
|
transform.translation = tgt.translation();
|
|
}
|
|
CameraMode::Ship => todo!("Ship Mode"),
|
|
CameraMode::Disabled => (),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn camera_pitch(cam_query: Query<(&mut Transform, &CameraPitch)>)
|
|
{
|
|
for (mut cam_transform, pitch) in cam_query
|
|
{
|
|
cam_transform.rotation = Quat::from_rotation_x(pitch.0);
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "dev")]
|
|
pub fn camera_toggle(
|
|
key: Res<ButtonInput<KeyCode>>,
|
|
mut cursor_options: Single<&mut CursorOptions, With<PrimaryWindow>>,
|
|
camera: Single<&mut CameraMode>,
|
|
)
|
|
{
|
|
use bevy::window::CursorGrabMode;
|
|
let mut mode = camera.into_inner();
|
|
if key.just_pressed(KeyCode::Escape)
|
|
{
|
|
if cursor_options.visible
|
|
{
|
|
*mode = CameraMode::Player;
|
|
cursor_options.grab_mode = CursorGrabMode::Locked;
|
|
cursor_options.visible = false;
|
|
}
|
|
else
|
|
{
|
|
*mode = CameraMode::Disabled;
|
|
cursor_options.grab_mode = CursorGrabMode::None;
|
|
cursor_options.visible = true;
|
|
}
|
|
}
|
|
}
|