not right
This commit is contained in:
parent
4e4adcb934
commit
6a12d213e8
63
src/timer.rs
63
src/timer.rs
@ -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
|
||||
//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
|
||||
|
||||
pub trait TimerState:Copy{
|
||||
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;
|
||||
@ -13,15 +13,21 @@ pub trait TimerState:Copy{
|
||||
}
|
||||
|
||||
#[derive(Clone,Copy,Debug)]
|
||||
struct Scaled{
|
||||
pub struct Scaled{
|
||||
scale:Ratio64,
|
||||
offset:Time,
|
||||
}
|
||||
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()))
|
||||
}
|
||||
fn get_scale(&self)->Ratio64{
|
||||
const fn get_scale(&self)->Ratio64{
|
||||
self.scale
|
||||
}
|
||||
fn set_scale(&mut self,time:Time,new_scale:Ratio64){
|
||||
@ -46,9 +52,14 @@ impl TimerState for Scaled{
|
||||
}
|
||||
|
||||
#[derive(Clone,Copy,Debug)]
|
||||
struct Realtime{
|
||||
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
|
||||
@ -71,11 +82,13 @@ pub struct Timer<T>{
|
||||
}
|
||||
|
||||
impl Timer<Realtime>{
|
||||
pub fn realtime(offset:Time)->Self{
|
||||
Self{
|
||||
state:Realtime{offset},
|
||||
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{
|
||||
@ -98,19 +111,31 @@ impl std::fmt::Display for Error{
|
||||
impl std::error::Error for Error{}
|
||||
|
||||
impl Timer<Scaled>{
|
||||
pub fn scaled(scale:Ratio64,offset:Time)->Self{
|
||||
Self{
|
||||
state:Scaled{scale,offset},
|
||||
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(scale:Ratio64,offset:Time)->Self{
|
||||
Self{
|
||||
state:Scaled{scale,offset},
|
||||
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 fn get_scale(&mut self)->Ratio64{
|
||||
pub const fn get_scale(&self)->Ratio64{
|
||||
self.state.get_scale()
|
||||
}
|
||||
pub fn set_scale(&mut self,time:Time,new_scale:Ratio64){
|
||||
|
Loading…
Reference in New Issue
Block a user