diff options
| author | LLLL Colonq <llll@colonq> | 2024-12-07 17:25:37 -0500 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2024-12-07 17:25:37 -0500 |
| commit | d5a5b454a4670a72826882c6ce4c87d1f597767c (patch) | |
| tree | 5963ed2b53063d3579019e6b22a629ac52d3b612 /src/state.rs | |
| parent | aa465e6ebee75e9e9ea03196db4dd83fde766f0a (diff) | |
Native
Diffstat (limited to 'src/state.rs')
| -rw-r--r-- | src/state.rs | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/state.rs b/src/state.rs index 8c70c51..c5478a9 100644 --- a/src/state.rs +++ b/src/state.rs @@ -93,9 +93,15 @@ pub struct PointLight { pub attenuation: glam::Vec2, } +#[cfg(target_arch = "wasm32")] +type Timestamp = f64; + +#[cfg(not(target_arch = "wasm32"))] +type Timestamp = std::time::Instant; + pub struct State { pub acc: f64, - pub last: f64, + pub last: Timestamp, pub tick: u64, pub rebinding: Option<Key>, @@ -119,10 +125,16 @@ pub struct State { pub log: Vec<(u64, String)>, } -pub fn now(ctx: &context::Context) -> f64 { +#[cfg(target_arch = "wasm32")] +pub fn now(ctx: &context::Context) -> Timestamp { ctx.performance.now() / 1000.0 } +#[cfg(not(target_arch = "wasm32"))] +pub fn now(_ctx: &context::Context) -> Timestamp { + std::time::Instant::now() +} + pub fn default_keybindings() -> BiHashMap<winit::keyboard::KeyCode, Key> { BiHashMap::from_iter(vec![ (winit::keyboard::KeyCode::KeyW, Key::Up), @@ -156,9 +168,12 @@ impl State { let cwaker = Box::leak(Box::new(waker.into())); let waker_ctx = std::task::Context::from_waker(cwaker); + let acc = 0.0; + let last = now(ctx); + Self { - acc: 0.0, - last: now(ctx), + acc, + last, // we initialize the tick to 1000, which allows us to use "0" as the default time for // various animation starts on entities without having them all play at game start tick: 1000, @@ -421,7 +436,12 @@ impl State { pub fn run_update<G>(&mut self, ctx: &context::Context, game: &mut G) where G: Game { let now = now(ctx); + + #[cfg(target_arch = "wasm32")] let diff = now - self.last; + #[cfg(not(target_arch = "wasm32"))] + let diff = now.duration_since(self.last).as_secs_f64(); + self.acc += diff; self.last = now; |
