Implement MenuSystem, reimplement the mouse grab and menu system, add a new common module in the game systems for common functions, various other fixes
This commit is contained in:
parent
47c3b2b111
commit
b17a7636d8
7 changed files with 219 additions and 150 deletions
|
|
@ -5,9 +5,11 @@ use winit::keyboard::{KeyCode, PhysicalKey};
|
|||
use winit::window::CursorGrabMode;
|
||||
use raidillon_app::prelude::*;
|
||||
|
||||
use crate::systems::common::{camera_front, is_camera_mode_valid, is_mouse_look_enabled};
|
||||
use crate::systems::menu::MenuState;
|
||||
|
||||
pub struct FPSDebugCameraSystem {
|
||||
mouse_delta: (f64, f64),
|
||||
mouse_enabled: bool,
|
||||
position: Vec3,
|
||||
yaw: f32,
|
||||
pitch: f32,
|
||||
|
|
@ -19,7 +21,6 @@ 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,
|
||||
|
|
@ -31,7 +32,7 @@ impl Default for FPSDebugCameraSystem {
|
|||
|
||||
impl System for FPSDebugCameraSystem {
|
||||
fn handle_event(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
if !self.is_camera_mode_valid(scene) {
|
||||
if !(is_camera_mode_valid(scene, CameraMode::Debug) && is_mouse_look_enabled(scene)) {
|
||||
return
|
||||
}
|
||||
let pctx = res.get::<PlatformContext>().unwrap();
|
||||
|
|
@ -46,89 +47,43 @@ impl System for FPSDebugCameraSystem {
|
|||
_ => {}
|
||||
}
|
||||
},
|
||||
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 {
|
||||
if is_mouse_look_enabled(scene) {
|
||||
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 front = camera_front(self.yaw, self.pitch);
|
||||
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;
|
||||
if is_mouse_look_enabled(scene) {
|
||||
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;
|
||||
});
|
||||
if is_camera_mode_valid(scene, CameraMode::Debug) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue