Add delta time, use it in the free fly camera
ci/woodpecker/push/build Pipeline was successful
Details
ci/woodpecker/push/build Pipeline was successful
Details
This commit is contained in:
parent
1f534bb1aa
commit
927566ca3d
|
@ -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::<DeltaTime>().unwrap();
|
||||
|
||||
let events = world
|
||||
.get_resource_mut::<EventQueue>()
|
||||
.and_then(|q| q.read_events::<MouseMotion>());
|
||||
|
@ -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;
|
||||
|
|
|
@ -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::<DeltaTime>().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, &[]);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
pub use camera::*;
|
||||
|
||||
pub mod free_fly_camera;
|
||||
pub use free_fly_camera::*;
|
||||
|
||||
pub mod delta_time;
|
||||
pub use delta_time::*;
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue