From adcd7db4f1015afea00831575c4a3a7682ca75d1 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 20 Jan 2025 05:25:13 -0800 Subject: [PATCH] common: timer: the time of a paused timer does not depend on the parent time --- lib/common/src/run.rs | 2 +- lib/common/src/timer.rs | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/common/src/run.rs b/lib/common/src/run.rs index 8ab499c..2b21060 100644 --- a/lib/common/src/run.rs +++ b/lib/common/src/run.rs @@ -76,7 +76,7 @@ impl Run{ match &self.state{ RunState::Created=>Time::ZERO, RunState::Started{timer}=>timer.time(time), - RunState::Finished{timer}=>timer.time(time), + RunState::Finished{timer}=>timer.time(), } } pub fn start(&mut self,time:PhysicsTime)->Result<(),Error>{ diff --git a/lib/common/src/timer.rs b/lib/common/src/timer.rs index b75c68b..fcd4de7 100644 --- a/lib/common/src/timer.rs +++ b/lib/common/src/timer.rs @@ -157,7 +157,7 @@ impl TimerFixed where Time:Copy, { pub fn into_unpaused(self,time:Time)->TimerFixed{ - let new_time=self.time(time); + let new_time=self.time(); let mut timer=TimerFixed{ state:self.state, _paused:Unpaused, @@ -165,6 +165,9 @@ impl TimerFixed timer.set_time(time,new_time); timer } + pub fn time(&self)->Time{ + self.state.get_offset().coerce() + } } impl TimerFixed where Time:Copy, @@ -178,6 +181,9 @@ impl TimerFixed timer.set_time(time,new_time); timer } + pub fn time(&self,time:Time)->Time{ + self.state.get_time(time) + } } //the new constructor and time queries are generic across both @@ -199,12 +205,6 @@ impl TimerFixed{ pub fn into_state(self)->T{ self.state } - pub fn time(&self,time:Time)->Time{ - match P::IS_PAUSED{ - true=>self.state.get_offset().coerce(), - false=>self.state.get_time(time), - } - } pub fn set_time(&mut self,time:Time,new_time:Time){ match P::IS_PAUSED{ true=>self.state.set_offset(new_time.coerce()), @@ -256,7 +256,7 @@ impl Timer } pub fn time(&self,time:Time)->Time{ match self{ - Self::Paused(timer)=>timer.time(time), + Self::Paused(timer)=>timer.time(), Self::Unpaused(timer)=>timer.time(time), } } @@ -329,7 +329,7 @@ mod test{ //create a paused timer that reads 0s let timer=TimerFixed::,Paused>::from_state(Scaled::new(0.5f32.try_into().unwrap(),sec!(0))); //the paused timer at 1 second should read 0s - assert_eq!(timer.time(sec!(1)),sec!(0)); + assert_eq!(timer.time(),sec!(0)); //unpause it after one second let timer=timer.into_unpaused(sec!(1)); @@ -339,7 +339,7 @@ mod test{ //pause the timer after 11 seconds let timer=timer.into_paused(sec!(11)); //the paused timer at 20 seconds should read 5s - assert_eq!(timer.time(sec!(20)),sec!(5)); + assert_eq!(timer.time(),sec!(5)); } #[test] fn test_timer()->Result<(),Error>{