2023-10-27 02:26:13 +00:00
|
|
|
use std::borrow::BorrowMut;
|
|
|
|
|
|
|
|
use edict::{Component, World};
|
|
|
|
use instant::Instant;
|
|
|
|
|
|
|
|
use crate::plugin::Plugin;
|
|
|
|
|
|
|
|
#[derive(Clone, Component)]
|
2023-10-29 21:54:04 +00:00
|
|
|
pub struct DeltaTime(f32, Option<Instant>);
|
2023-10-27 02:26:13 +00:00
|
|
|
|
|
|
|
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();
|
2023-10-29 21:54:04 +00:00
|
|
|
delta.0 = delta.1.unwrap_or(now).elapsed().as_secs_f32();
|
|
|
|
delta.1 = Some(now);
|
|
|
|
|
|
|
|
//println!("delta: {}", delta.0);
|
2023-10-27 02:26:13 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DeltaTimePlugin;
|
|
|
|
|
|
|
|
impl Plugin for DeltaTimePlugin {
|
|
|
|
fn setup(&self, game: &mut crate::game::Game) {
|
2023-10-29 21:54:04 +00:00
|
|
|
game.world().insert_resource(DeltaTime(0.0, None));
|
2023-10-27 02:26:13 +00:00
|
|
|
game.with_system("delta_time", delta_time_system, &[]);
|
|
|
|
}
|
|
|
|
}
|