From 927566ca3d260a7f6d7444a7ee4093e6d074e595 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Thu, 26 Oct 2023 22:26:13 -0400 Subject: [PATCH] Add delta time, use it in the free fly camera --- examples/testbed/src/free_fly_camera.rs | 17 +++++----- src/ecs/components/delta_time.rs | 41 +++++++++++++++++++++++++ src/ecs/components/mod.rs | 14 ++++++++- src/plugin.rs | 3 +- 4 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 src/ecs/components/delta_time.rs diff --git a/examples/testbed/src/free_fly_camera.rs b/examples/testbed/src/free_fly_camera.rs index 10cdb45..fb0bce7 100644 --- a/examples/testbed/src/free_fly_camera.rs +++ b/examples/testbed/src/free_fly_camera.rs @@ -2,13 +2,12 @@ use std::ops::Deref; use edict::{Component, World}; use lyra_engine::{ - ecs::{components::camera::CameraComponent, EventQueue, SimpleSystem}, + ecs::{components::{camera::CameraComponent, DeltaTime}, EventQueue, SimpleSystem}, game::Game, input::{InputButtons, KeyCode, MouseMotion}, - math::{Angle, Quat, Vec3, EulerRot}, + math::{Quat, Vec3, EulerRot}, plugin::Plugin, }; -use tracing::debug; #[derive(Clone, Component)] pub struct FreeFlyCamera { @@ -21,9 +20,9 @@ pub struct FreeFlyCamera { impl Default for FreeFlyCamera { fn default() -> Self { Self { - speed: 0.07, - look_speed: 0.01, - mouse_sensitivity: 0.04, + speed: 3.0, + look_speed: 0.09, + mouse_sensitivity: 0.4, look_with_keys: false, } } @@ -46,6 +45,8 @@ impl SimpleSystem for FreeFlyCameraController { fn execute_mut(&mut self, world: &mut World) -> anyhow::Result<()> { let mut camera_rot = Vec3::default(); + let delta_time = **world.get_resource::().unwrap(); + let events = world .get_resource_mut::() .and_then(|q| q.read_events::()); @@ -116,7 +117,7 @@ impl SimpleSystem for FreeFlyCameraController { } if velocity != Vec3::ZERO { - cam.transform.translation += velocity.normalize() * fly.speed; + cam.transform.translation += velocity.normalize() * fly.speed * delta_time; } } @@ -129,7 +130,7 @@ impl SimpleSystem for FreeFlyCameraController { } if camera_rot != Vec3::ZERO { - let look_velocity = camera_rot * fly.look_speed; + let look_velocity = camera_rot * fly.look_speed * delta_time; let (mut y, mut x, _) = cam.transform.rotation.to_euler(EulerRot::YXZ); x += look_velocity.x; diff --git a/src/ecs/components/delta_time.rs b/src/ecs/components/delta_time.rs new file mode 100644 index 0000000..92b6d71 --- /dev/null +++ b/src/ecs/components/delta_time.rs @@ -0,0 +1,41 @@ +use std::borrow::BorrowMut; + +use edict::{Component, World}; +use instant::Instant; + +use crate::plugin::Plugin; + +#[derive(Clone, Component)] +pub struct DeltaTime(f32, Instant); + +impl std::ops::Deref for DeltaTime { + type Target = f32; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl std::ops::DerefMut for DeltaTime { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +fn delta_time_system(world: &mut World) -> anyhow::Result<()> { + let now = Instant::now(); + let mut delta = world.get_resource_mut::().unwrap(); + delta.0 = delta.1.elapsed().as_secs_f32(); + delta.1 = now; + + Ok(()) +} + +pub struct DeltaTimePlugin; + +impl Plugin for DeltaTimePlugin { + fn setup(&self, game: &mut crate::game::Game) { + game.world().insert_resource(DeltaTime(0.0, Instant::now())); + game.with_system("delta_time", delta_time_system, &[]); + } +} \ No newline at end of file diff --git a/src/ecs/components/mod.rs b/src/ecs/components/mod.rs index 6976828..aeb9278 100755 --- a/src/ecs/components/mod.rs +++ b/src/ecs/components/mod.rs @@ -1,5 +1,17 @@ pub mod mesh; +pub use mesh::*; + pub mod model; +pub use model::*; + pub mod transform; +pub use transform::*; + pub mod camera; -pub mod free_fly_camera; \ No newline at end of file +pub use camera::*; + +pub mod free_fly_camera; +pub use free_fly_camera::*; + +pub mod delta_time; +pub use delta_time::*; \ No newline at end of file diff --git a/src/plugin.rs b/src/plugin.rs index 07ef5b0..1b78b32 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,6 +1,7 @@ use lyra_resource::ResourceManager; use crate::ecs::EventsPlugin; +use crate::ecs::components::DeltaTimePlugin; use crate::game::Game; use crate::input::InputPlugin; use crate::render::window::WindowPlugin; @@ -107,10 +108,10 @@ pub struct DefaultPlugins; impl Plugin for DefaultPlugins { fn setup(&self, game: &mut Game) { - // setup input EventsPlugin.setup(game); InputPlugin.setup(game); ResourceManagerPlugin.setup(game); WindowPlugin::default().setup(game); + DeltaTimePlugin.setup(game); } } \ No newline at end of file