Commit b5f95278 authored by Rahix's avatar Rahix 🦀
Browse files

gamestate: Pass frame timestamp to state update method

Pass the timestamp from requestAnimationFrame() to the state update
function so the game-state doesn't have to call into JavaScript for
timestamps anymore.
parent e84900dd
...@@ -50,7 +50,7 @@ pub trait State { ...@@ -50,7 +50,7 @@ pub trait State {
fn init(&mut self, init: StateInitializer) -> Transition; fn init(&mut self, init: StateInitializer) -> Transition;
fn deinit(&mut self) {} fn deinit(&mut self) {}
fn update(&mut self) -> Transition { fn update(&mut self, _timestamp: f64) -> Transition {
Transition::Keep Transition::Keep
} }
#[allow(unused_variables)] #[allow(unused_variables)]
...@@ -261,8 +261,8 @@ impl StateStorage { ...@@ -261,8 +261,8 @@ impl StateStorage {
self.event_handlers.deregister_all(); self.event_handlers.deregister_all();
} }
pub fn update(&mut self) -> Transition { pub fn update(&mut self, timestamp: f64) -> Transition {
self.state.update() self.state.update(timestamp)
} }
fn event(&mut self, event: Event) -> Transition { fn event(&mut self, event: Event) -> Transition {
...@@ -272,7 +272,7 @@ impl StateStorage { ...@@ -272,7 +272,7 @@ impl StateStorage {
pub struct StateMachine { pub struct StateMachine {
states: Vec<StateStorage>, states: Vec<StateStorage>,
loop_handler: Option<closure::Closure<dyn FnMut(wasm_bindgen::JsValue)>>, loop_handler: Option<closure::Closure<dyn FnMut(f64)>>,
loop_scheduled: bool, loop_scheduled: bool,
} }
...@@ -287,7 +287,7 @@ impl StateMachine { ...@@ -287,7 +287,7 @@ impl StateMachine {
// Sadly Rc::new_cyclic() isn't stable yet ... // Sadly Rc::new_cyclic() isn't stable yet ...
let loop_handler = { let loop_handler = {
let this = this.clone(); let this = this.clone();
closure::Closure::wrap(Box::new(move |_timestamp| { closure::Closure::wrap(Box::new(move |timestamp| {
{ {
let mut sm = this.borrow_mut(); let mut sm = this.borrow_mut();
if !sm.loop_scheduled { if !sm.loop_scheduled {
...@@ -306,11 +306,11 @@ impl StateMachine { ...@@ -306,11 +306,11 @@ impl StateMachine {
return; return;
} }
active.update() active.update(timestamp)
}; };
StateMachine::do_transition(&this, t); StateMachine::do_transition(&this, t);
}) as Box<dyn FnMut(wasm_bindgen::JsValue)>) }) as Box<dyn FnMut(f64)>)
}; };
this.borrow_mut().loop_handler = Some(loop_handler); this.borrow_mut().loop_handler = Some(loop_handler);
......
...@@ -77,7 +77,7 @@ impl gamestate::State for HeavenState { ...@@ -77,7 +77,7 @@ impl gamestate::State for HeavenState {
.unwrap(); .unwrap();
} }
fn update(&mut self) -> gamestate::Transition { fn update(&mut self, _timestamp: f64) -> gamestate::Transition {
{ {
let rendering = self.resources.get_mut::<resources::Rendering>().unwrap(); let rendering = self.resources.get_mut::<resources::Rendering>().unwrap();
rendering.set_fill_style(&colors::BACKGROUND); rendering.set_fill_style(&colors::BACKGROUND);
......
...@@ -123,7 +123,7 @@ impl gamestate::State for InGameState { ...@@ -123,7 +123,7 @@ impl gamestate::State for InGameState {
gamestate::Transition::Keep gamestate::Transition::Keep
} }
fn update(&mut self) -> gamestate::Transition { fn update(&mut self, _timestamp: f64) -> gamestate::Transition {
use legion::IntoQuery; use legion::IntoQuery;
{ {
......
...@@ -74,7 +74,7 @@ impl gamestate::State for LevelLoadingState { ...@@ -74,7 +74,7 @@ impl gamestate::State for LevelLoadingState {
gamestate::Transition::Loop gamestate::Transition::Loop
} }
fn update(&mut self) -> gamestate::Transition { fn update(&mut self, _timestamp: f64) -> gamestate::Transition {
{ {
let rendering = self.resources.get_mut::<resources::Rendering>().unwrap(); let rendering = self.resources.get_mut::<resources::Rendering>().unwrap();
rendering.set_fill_style(&colors::BACKGROUND); rendering.set_fill_style(&colors::BACKGROUND);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment