This commit is contained in:
Quaternions 2024-07-31 13:16:16 -07:00
parent 1318fd2856
commit 4247c2ef5a
2 changed files with 68 additions and 0 deletions

View File

@ -1,5 +1,6 @@
pub mod bvh; pub mod bvh;
pub mod map; pub mod map;
pub mod run;
pub mod aabb; pub mod aabb;
pub mod model; pub mod model;
pub mod timer; pub mod timer;

67
src/run.rs Normal file
View File

@ -0,0 +1,67 @@
use crate::timer::{Timer,Scaled,Error as TimerError};
use crate::integer::Time;
pub enum InvalidationReason{
}
#[derive(Debug)]
pub enum Error{
Timer(TimerError),
AlreadyStarted,
NotStarted,
AlreadyFinished,
}
impl std::fmt::Display for Error{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for Error{}
pub struct Run{
timer:Timer<Scaled>,
invalidated:Option<InvalidationReason>,
created:Time,
started:Option<Time>,
finished:Option<Time>,
}
impl Run{
pub fn new(created:Time)->Self{
Self{
timer:Timer::scaled_paused(
created,
Time::ZERO,
crate::integer::Ratio64::ONE,
),
invalidated:None,
created,
started:None,
finished:None,
}
}
pub fn start(&mut self,time:Time)->Result<(),Error>{
match self.started{
Some(_)=>Err(Error::AlreadyStarted),
None=>{
self.started=Some(time);
self.timer.unpause(time).map_err(Error::Timer)?;
Ok(())
}
}
}
pub fn finish(&mut self,time:Time)->Result<(),Error>{
if self.started.is_none(){
return Err(Error::NotStarted);
}
match self.finished{
Some(_)=>Err(Error::AlreadyFinished),
None=>{
self.finished=Some(time);
self.timer.pause(time).map_err(Error::Timer)?;
Ok(())
}
}
}
}