Move debug_camera to the game crate, activate FPSDebugCameraSystem
This commit is contained in:
parent
713d865dd7
commit
96a59b68fa
4 changed files with 7 additions and 6 deletions
|
|
@ -17,7 +17,7 @@ use winit::event::{ElementState, Event, WindowEvent};
|
|||
use winit::event::DeviceEvent::MouseMotion;
|
||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
use raidillon_core::DebugUIBuffer;
|
||||
use raidillon_engine::systems::fps_camera::FPSDebugCameraSystem;
|
||||
use systems::debug_camera::FPSDebugCameraSystem;
|
||||
use raidillon_glium::RenderingSystem;
|
||||
use raidillon_physics::Physics;
|
||||
use crate::systems::{KeybindsSystem, KinematicCharacterController, PhysicsSystem};
|
||||
|
|
@ -123,6 +123,7 @@ fn main() {
|
|||
engine.system_manager.add::<PhysicsSystem>();
|
||||
engine.system_manager.add::<KeybindsSystem>();
|
||||
engine.system_manager.add::<KinematicCharacterController>();
|
||||
engine.system_manager.add::<FPSDebugCameraSystem>();
|
||||
engine.system_manager.add::<RenderingTestSystem>();
|
||||
engine.system_manager.add::<UpdateAspectRatioSystem>();
|
||||
// engine.system_manager.add::<InputTestSystem>();
|
||||
|
|
|
|||
140
game/src/systems/debug_camera.rs
Normal file
140
game/src/systems/debug_camera.rs
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
use raidillon_engine::system::{System, SystemContext};
|
||||
use glam::{Quat, Vec3};
|
||||
use winit::event::DeviceEvent::MouseMotion;
|
||||
use winit::event::{ElementState, Event, MouseButton, WindowEvent};
|
||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
use winit::window::CursorGrabMode;
|
||||
use raidillon_assets::model_path;
|
||||
use raidillon_platform::{Camera, PlatformContext};
|
||||
use raidillon_engine::InputState;
|
||||
use raidillon_engine::EngineResources;
|
||||
use raidillon_core::scene::Scene;
|
||||
use raidillon_ecs::components::CameraMode;
|
||||
|
||||
pub struct FPSDebugCameraSystem {
|
||||
mouse_delta: (f64, f64),
|
||||
mouse_enabled: bool,
|
||||
position: Vec3,
|
||||
yaw: f32,
|
||||
pitch: f32,
|
||||
speed: f32,
|
||||
sensitivity: f32,
|
||||
}
|
||||
|
||||
impl Default for FPSDebugCameraSystem {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
mouse_delta: Default::default(),
|
||||
mouse_enabled: Default::default(),
|
||||
position: Vec3::new(0.0, 0.0, 2.0),
|
||||
yaw: -90.0,
|
||||
pitch: 0.0,
|
||||
speed: 8.0,
|
||||
sensitivity: 0.1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl System for FPSDebugCameraSystem {
|
||||
fn handle_event(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
if !self.is_camera_mode_valid(scene) {
|
||||
return
|
||||
}
|
||||
let pctx = res.get::<PlatformContext>().unwrap();
|
||||
let event2 = pctx.current_event.clone();
|
||||
match event2 {
|
||||
Event::DeviceEvent { device_id, event} => {
|
||||
match event {
|
||||
MouseMotion { delta } => {
|
||||
self.mouse_delta.0 += delta.0;
|
||||
self.mouse_delta.1 += delta.1;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
Event::WindowEvent { event, .. } => match event {
|
||||
WindowEvent::MouseInput { state, button, .. } => {
|
||||
if button == MouseButton::Right {
|
||||
// blood and tear
|
||||
let window = pctx.window.lock().unwrap();
|
||||
match state {
|
||||
ElementState::Pressed => {
|
||||
if window
|
||||
.set_cursor_grab(CursorGrabMode::Confined)
|
||||
.or_else(|_| window.set_cursor_grab(CursorGrabMode::Locked))
|
||||
.is_ok()
|
||||
{
|
||||
window.set_cursor_visible(false);
|
||||
self.mouse_enabled = true;
|
||||
}
|
||||
}
|
||||
ElementState::Released => {
|
||||
let _ = window.set_cursor_grab(CursorGrabMode::None);
|
||||
window.set_cursor_visible(true);
|
||||
self.mouse_enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {},
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn frame_update(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
if !self.is_camera_mode_valid(scene) {
|
||||
return
|
||||
}
|
||||
let (pctx, input) = res.get_many::<(PlatformContext, InputState)>().unwrap();
|
||||
|
||||
if self.mouse_enabled {
|
||||
self.yaw += self.mouse_delta.0 as f32 * self.sensitivity;
|
||||
self.pitch -= self.mouse_delta.1 as f32 * self.sensitivity;
|
||||
self.pitch = self.pitch.clamp(-89.0, 89.0);
|
||||
}
|
||||
|
||||
let front = self.front();
|
||||
let right_vec = front.cross(Vec3::Y).normalize();
|
||||
|
||||
if input.key_held(KeyCode::KeyW) {
|
||||
self.position += front * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
if input.key_held(KeyCode::KeyS) {
|
||||
self.position -= front * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
if input.key_held(KeyCode::KeyA) {
|
||||
self.position -= right_vec * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
if input.key_held(KeyCode::KeyD) {
|
||||
self.position += right_vec * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
|
||||
scene.world.query_mut::<&mut Camera>().into_iter().for_each(|(_, camera)| {
|
||||
camera.eye = self.position;
|
||||
camera.center = self.position + front;
|
||||
});
|
||||
self.mouse_delta = (0.0, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl FPSDebugCameraSystem {
|
||||
pub fn front(&self) -> Vec3 {
|
||||
let yaw_rad = self.yaw.to_radians();
|
||||
let pitch_rad = self.pitch.to_radians();
|
||||
Vec3::new(
|
||||
yaw_rad.cos() * pitch_rad.cos(),
|
||||
pitch_rad.sin(),
|
||||
yaw_rad.sin() * pitch_rad.cos(),
|
||||
).normalize()
|
||||
}
|
||||
|
||||
fn is_camera_mode_valid(&self, scene: &mut Scene) -> bool {
|
||||
let mut q = scene.world.query::<(&Camera, &CameraMode)>();
|
||||
let (cam_ent, (cam, cam_mode)) = q
|
||||
.iter()
|
||||
.next()
|
||||
.unwrap();
|
||||
*cam_mode == CameraMode::Debug
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
mod physics;
|
||||
mod kinematic_character_controller;
|
||||
mod keybinds;
|
||||
pub mod debug_camera;
|
||||
|
||||
pub use physics::PhysicsSystem;
|
||||
pub use kinematic_character_controller::KinematicCharacterController;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue