not right

This commit is contained in:
Quaternions 2024-07-31 13:15:29 -07:00
parent 4e4adcb934
commit 6a12d213e8

View File

@ -1,11 +1,11 @@
use strafesnet_common::integer::{Time,Ratio64}; use crate::integer::{Time,Ratio64};
//this could be about half as long if I only had //this could be about half as long if I only had
//scaled timers and just used a scale of 1 //scaled timers and just used a scale of 1
//but I thought the concept of a timer that could //but I thought the concept of a timer that could
//only be paused and not scaled was cool //only be paused and not scaled was cool
pub trait TimerState:Copy{ trait TimerState:Copy{
fn get_time(&self,time:Time)->Time; fn get_time(&self,time:Time)->Time;
fn set_time(&mut self,time:Time,new_time:Time); fn set_time(&mut self,time:Time,new_time:Time);
fn get_offset(&self)->Time; fn get_offset(&self)->Time;
@ -13,15 +13,21 @@ pub trait TimerState:Copy{
} }
#[derive(Clone,Copy,Debug)] #[derive(Clone,Copy,Debug)]
struct Scaled{ pub struct Scaled{
scale:Ratio64, scale:Ratio64,
offset:Time, offset:Time,
} }
impl Scaled{ impl Scaled{
fn scale(&self,time:Time)->Time{ 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())) Time::raw(self.scale.mul_int(time.get()))
} }
fn get_scale(&self)->Ratio64{ const fn get_scale(&self)->Ratio64{
self.scale self.scale
} }
fn set_scale(&mut self,time:Time,new_scale:Ratio64){ fn set_scale(&mut self,time:Time,new_scale:Ratio64){
@ -46,9 +52,14 @@ impl TimerState for Scaled{
} }
#[derive(Clone,Copy,Debug)] #[derive(Clone,Copy,Debug)]
struct Realtime{ pub struct Realtime{
offset:Time, offset:Time,
} }
impl Realtime{
const fn identity()->Self{
Self{offset:Time::ZERO}
}
}
impl TimerState for Realtime{ impl TimerState for Realtime{
fn get_time(&self,time:Time)->Time{ fn get_time(&self,time:Time)->Time{
time+self.offset time+self.offset
@ -71,11 +82,13 @@ pub struct Timer<T>{
} }
impl Timer<Realtime>{ impl Timer<Realtime>{
pub fn realtime(offset:Time)->Self{ pub fn realtime(time:Time,new_time:Time)->Self{
Self{ let mut timer=Self{
state:Realtime{offset}, state:Realtime::identity(),
paused:false, paused:false,
} };
timer.set_time(time,new_time);
timer
} }
pub fn realtime_paused(offset:Time)->Self{ pub fn realtime_paused(offset:Time)->Self{
Self{ Self{
@ -98,19 +111,31 @@ impl std::fmt::Display for Error{
impl std::error::Error for Error{} impl std::error::Error for Error{}
impl Timer<Scaled>{ impl Timer<Scaled>{
pub fn scaled(scale:Ratio64,offset:Time)->Self{ pub fn new(time:Time,new_time:Time,scale:Ratio64,paused:bool)->Self{
Self{ let mut timer=Self{
state:Scaled{scale,offset}, 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, paused:false,
} };
timer.set_time(time,new_time);
timer
} }
pub fn scaled_paused(scale:Ratio64,offset:Time)->Self{ pub fn scaled_paused(time:Time,new_time:Time,scale:Ratio64)->Self{
Self{ let mut timer=Self{
state:Scaled{scale,offset}, state:Scaled::with_scale(scale),
paused:true, paused:true,
} };
timer.set_time(time,new_time);
timer
} }
pub fn get_scale(&mut self)->Ratio64{ pub const fn get_scale(&self)->Ratio64{
self.state.get_scale() self.state.get_scale()
} }
pub fn set_scale(&mut self,time:Time,new_scale:Ratio64){ pub fn set_scale(&mut self,time:Time,new_scale:Ratio64){