2 Commits

Author SHA1 Message Date
db4ba34a42 nope, run needs timers with typestate 2024-07-31 13:18:53 -07:00
56ccc4b58b wip: run 2024-07-31 13:16:16 -07:00
3 changed files with 72 additions and 211 deletions

View File

@@ -1,8 +1,8 @@
pub mod bvh;
pub mod map;
pub mod run;
pub mod aabb;
pub mod model;
pub mod timer;
pub mod zeroes;
pub mod integer;
pub mod updatable;
@@ -10,4 +10,4 @@ pub mod instruction;
pub mod gameplay_attributes;
pub mod gameplay_modes;
pub mod gameplay_style;
pub mod controls_bitflag;
pub mod controls_bitflag;

70
src/run.rs Normal file
View File

@@ -0,0 +1,70 @@
use crate::timer::{Timer,Scaled,Error as TimerError};
use crate::integer::Time;
pub enum InvalidationReason{
}
#[derive(Debug)]
pub enum Error{
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{}
enum RunState{
Created{timer:Timer<Paused>},
Started{timer:Timer<Unpaused>},
Finished{timer:Timer<Paused>},
}
pub struct Run{
invalidated:Option<InvalidationReason>,
created:Time,
state:RunState,
}
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(())
}
}
}
}

View File

@@ -1,209 +0,0 @@
use crate::integer::{Time,Ratio64};
//this could be about half as long if I only had
//scaled timers and just used a scale of 1
//but I thought the concept of a timer that could
//only be paused and not scaled was cool
trait TimerState:Copy{
fn get_time(&self,time:Time)->Time;
fn set_time(&mut self,time:Time,new_time:Time);
fn get_offset(&self)->Time;
fn set_offset(&mut self,offset:Time);
}
#[derive(Clone,Copy,Debug)]
pub struct Scaled{
scale:Ratio64,
offset:Time,
}
impl Scaled{
const fn identity()->Self{
Self{scale:Ratio64::ONE,offset:Time::ZERO}
}
const fn with_scale(scale:Ratio64)->Self{
Self{scale,offset:Time::ZERO}
}
const fn scale(&self,time:Time)->Time{
Time::raw(self.scale.mul_int(time.get()))
}
const fn get_scale(&self)->Ratio64{
self.scale
}
fn set_scale(&mut self,time:Time,new_scale:Ratio64){
let new_time=self.get_time(time);
self.scale=new_scale;
self.set_time(time,new_time);
}
}
impl TimerState for Scaled{
fn get_time(&self,time:Time)->Time{
self.scale(time)+self.offset
}
fn set_time(&mut self,time:Time,new_time:Time){
self.offset=new_time-self.scale(time);
}
fn get_offset(&self)->Time{
self.offset
}
fn set_offset(&mut self,offset:Time){
self.offset=offset;
}
}
#[derive(Clone,Copy,Debug)]
pub struct Realtime{
offset:Time,
}
impl Realtime{
const fn identity()->Self{
Self{offset:Time::ZERO}
}
}
impl TimerState for Realtime{
fn get_time(&self,time:Time)->Time{
time+self.offset
}
fn set_time(&mut self,time:Time,new_time:Time){
self.offset=new_time-time;
}
fn get_offset(&self)->Time{
self.offset
}
fn set_offset(&mut self,offset:Time){
self.offset=offset;
}
}
#[derive(Clone,Debug)]
pub struct Timer<T>{
state:T,
paused:bool,
}
impl Timer<Realtime>{
pub fn realtime(time:Time,new_time:Time)->Self{
let mut timer=Self{
state:Realtime::identity(),
paused:false,
};
timer.set_time(time,new_time);
timer
}
pub fn realtime_paused(offset:Time)->Self{
Self{
state:Realtime{offset},
paused:true,
}
}
}
#[derive(Debug)]
pub enum Error{
AlreadyPaused,
AlreadyUnpaused,
}
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{}
impl Timer<Scaled>{
pub fn new(time:Time,new_time:Time,scale:Ratio64,paused:bool)->Self{
let mut timer=Self{
state:Scaled::with_scale(scale),
paused,
};
timer.set_time(time,new_time);
timer
}
pub fn scaled(time:Time,new_time:Time,scale:Ratio64)->Self{
let mut timer=Self{
state:Scaled::with_scale(scale),
paused:false,
};
timer.set_time(time,new_time);
timer
}
pub fn scaled_paused(time:Time,new_time:Time,scale:Ratio64)->Self{
let mut timer=Self{
state:Scaled::with_scale(scale),
paused:true,
};
timer.set_time(time,new_time);
timer
}
pub const fn get_scale(&self)->Ratio64{
self.state.get_scale()
}
pub fn set_scale(&mut self,time:Time,new_scale:Ratio64){
self.state.set_scale(time,new_scale)
}
}
impl<T:TimerState> Timer<T>{
pub fn time(&self,time:Time)->Time{
match self.paused{
true=>self.state.get_offset(),
false=>self.state.get_time(time),
}
}
pub fn set_time(&mut self,time:Time,new_time:Time){
match self.paused{
true=>self.state.set_offset(new_time),
false=>self.state.set_time(time,new_time),
}
}
pub fn pause(&mut self,time:Time)->Result<(),Error>{
match self.paused{
true=>Err(Error::AlreadyPaused),
false=>{
let new_time=self.time(time);
self.state.set_offset(new_time);
self.paused=true;
Ok(())
},
}
}
pub fn unpause(&mut self,time:Time)->Result<(),Error>{
match self.paused{
true=>{
let new_time=self.time(time);
self.state.set_time(time,new_time);
self.paused=false;
Ok(())
},
false=>Err(Error::AlreadyUnpaused),
}
}
}
#[cfg(test)]
mod test{
use super::{Time,Timer,Error};
macro_rules! sec {
($s: expr) => {
Time::from_secs($s)
};
}
#[test]
fn test_timer()->Result<(),Error>{
//create a paused timer that reads 0s
let mut timer=Timer::realtime_paused(sec!(0));
//the paused timer at 1 second should read 0s
assert_eq!(timer.time(sec!(1)),sec!(0));
//unpause it after one second
timer.unpause(sec!(1))?;
//the timer at 6 seconds should read 5s
assert_eq!(timer.time(sec!(6)),sec!(5));
//pause the timer after 11 seconds
timer.pause(sec!(11))?;
//the paused timer at 20 seconds should read 10s
assert_eq!(timer.time(sec!(20)),sec!(10));
Ok(())
}
}