WIP prefabs loader
This commit is contained in:
66
game/main/src/plugins/camera.rs
Normal file
66
game/main/src/plugins/camera.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
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 window: Single<&mut Window, With<PrimaryWindow>>,
|
||||
camera: Single<&mut CameraMode>,
|
||||
) {
|
||||
use bevy::window::CursorGrabMode;
|
||||
let mut mode = camera.into_inner();
|
||||
if key.just_pressed(KeyCode::Escape) {
|
||||
if window.cursor_options.visible {
|
||||
*mode = CameraMode::Player;
|
||||
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
||||
window.cursor_options.visible = false;
|
||||
} else {
|
||||
*mode = CameraMode::Disabled;
|
||||
window.cursor_options.grab_mode = CursorGrabMode::None;
|
||||
window.cursor_options.visible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user