Free fly camera code cleanup

This commit is contained in:
SeanOMik 2024-01-06 20:37:23 -05:00
parent 22b582c3f8
commit 265752ee76
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
1 changed files with 8 additions and 83 deletions

View File

@ -1,10 +1,10 @@
use std::{ops::Deref, ptr::NonNull}; use std::ptr::NonNull;
use lyra_engine::{ use lyra_engine::{
game::Game, game::Game,
input::{InputButtons, KeyCode, MouseMotion, ActionHandler}, input::ActionHandler,
math::{Quat, Vec3, EulerRot}, math::{Quat, Vec3, EulerRot},
plugin::Plugin, ecs::{system::{System, IntoSystem}, world::World, Access, Component}, DeltaTime, EventQueue, scene::CameraComponent, plugin::Plugin, ecs::{system::{System, IntoSystem}, world::World, Access, Component}, DeltaTime, scene::CameraComponent,
}; };
#[derive(Clone, Component)] #[derive(Clone, Component)]
@ -46,48 +46,9 @@ pub struct FreeFlyCameraPlugin;
impl System for FreeFlyCameraPlugin { impl System for FreeFlyCameraPlugin {
fn execute(&mut self, mut world: NonNull<World>) -> anyhow::Result<()> { fn execute(&mut self, mut world: NonNull<World>) -> anyhow::Result<()> {
let world = unsafe { world.as_mut() }; let world = unsafe { world.as_mut() };
let mut camera_rot = Vec3::default();
let delta_time = **world.get_resource::<DeltaTime>(); let delta_time = **world.get_resource::<DeltaTime>();
let handler = world.get_resource::<ActionHandler>(); let handler = world.get_resource::<ActionHandler>();
/* if let Some(look_x) = handler.get_axis_modifier("look_left_right") {
camera_rot.x += look_x;
}
if let Some(look_y) = handler.get_axis_modifier("look_up_down") {
camera_rot.y += look_y;
} */
/* if let Some(keys) = keys.as_ref() {
if keys.is_pressed(KeyCode::Left) {
camera_rot.y += 1.0;
}
if keys.is_pressed(KeyCode::Right) {
camera_rot.y -= 1.0;
}
if keys.is_pressed(KeyCode::Up) {
camera_rot.x += 1.0;
}
if keys.is_pressed(KeyCode::Down) {
camera_rot.x -= 1.0;
}
if keys.is_pressed(KeyCode::E) {
camera_rot.z -= 1.0;
}
if keys.is_pressed(KeyCode::Q) {
camera_rot.z += 1.0;
}
} */
for (mut cam, fly) in world for (mut cam, fly) in world
.view_iter::<(&mut CameraComponent, &FreeFlyCamera)>() .view_iter::<(&mut CameraComponent, &FreeFlyCamera)>()
{ {
@ -95,42 +56,11 @@ impl System for FreeFlyCameraPlugin {
let left = cam.transform.left(); let left = cam.transform.left();
let up = Vec3::Y; let up = Vec3::Y;
// handle camera movement
/* if let Some(keys) = keys.as_ref() {
let mut velocity = Vec3::ZERO;
if let Some(m) = handler.get_pressed_modifier("forward_backward") {
velocity += m * forward;
}
if let Some(m) = handler.get_pressed_modifier("left_right") {
velocity += m * left;
}
if keys.is_pressed(KeyCode::C) {
velocity += up;
}
if keys.is_pressed(KeyCode::Z) {
velocity -= up;
}
if velocity != Vec3::ZERO {
let temp = if keys.is_pressed(KeyCode::E) {
fly.speed * delta_time * fly.slow_speed_factor
} else {
fly.speed * delta_time
};
cam.transform.translation += velocity.normalize() * temp;
}
} */
let mut velocity = Vec3::ZERO;
let move_y = handler.get_axis_modifier("up_down").unwrap_or(0.0); let move_y = handler.get_axis_modifier("up_down").unwrap_or(0.0);
let move_x = handler.get_axis_modifier("left_right").unwrap_or(0.0); let move_x = handler.get_axis_modifier("left_right").unwrap_or(0.0);
let move_z = handler.get_axis_modifier("forward_backward").unwrap_or(0.0); let move_z = handler.get_axis_modifier("forward_backward").unwrap_or(0.0);
let mut velocity = Vec3::ZERO;
velocity += move_y * up; velocity += move_y * up;
velocity += move_x * left; velocity += move_x * left;
velocity += move_z * forward; velocity += move_z * forward;
@ -139,18 +69,13 @@ impl System for FreeFlyCameraPlugin {
cam.transform.translation += velocity.normalize() * fly.speed * delta_time; // TODO: speeding up cam.transform.translation += velocity.normalize() * fly.speed * delta_time; // TODO: speeding up
} }
// handle camera rotation
/* if let Some(mut events) = events.clone() {
while let Some(motion) = events.pop_front() {
camera_rot.x -= motion.delta.y * fly.mouse_sensitivity;
camera_rot.y -= motion.delta.x * fly.mouse_sensitivity;
}
} */
let motion_x = handler.get_axis_modifier("look_left_right").unwrap_or(0.0); let motion_x = handler.get_axis_modifier("look_left_right").unwrap_or(0.0);
let motion_y = handler.get_axis_modifier("look_up_down").unwrap_or(0.0); let motion_y = handler.get_axis_modifier("look_up_down").unwrap_or(0.0);
let motion_z = handler.get_axis_modifier("look_rotate").unwrap_or(0.0);
let mut camera_rot = Vec3::ZERO;
camera_rot.y -= motion_x * fly.mouse_sensitivity; camera_rot.y -= motion_x * fly.mouse_sensitivity;
camera_rot.x -= motion_y * fly.mouse_sensitivity; camera_rot.x -= motion_y * fly.mouse_sensitivity;
camera_rot.z -= motion_z * fly.mouse_sensitivity;
if camera_rot != Vec3::ZERO { if camera_rot != Vec3::ZERO {
let look_velocity = camera_rot * fly.look_speed * delta_time; let look_velocity = camera_rot * fly.look_speed * delta_time;