This commit is contained in:
Quaternions 2023-10-10 02:40:24 -07:00
parent e2af6fc4ed
commit 0ee17ac3d9
2 changed files with 151 additions and 0 deletions

View File

@ -7,6 +7,7 @@ use instruction::{TimedInstruction, InstructionConsumer};
mod bvh; mod bvh;
mod aabb; mod aabb;
mod model; mod model;
mod timers;
mod zeroes; mod zeroes;
mod worker; mod worker;
mod physics; mod physics;

150
src/timers.rs Normal file
View File

@ -0,0 +1,150 @@
type TIME=crate::physics::TIME;
pub struct Timescale{
num:i64,
den:std::num::NonZeroU64,
}
pub struct Paused{}
pub struct Unpaused{}
pub struct PausedScaled{scale:Timescale}
pub struct UnpausedScaled{scale:Timescale}
pub trait TimerState{}
impl TimerState for Paused{}
impl TimerState for Unpaused{}
impl TimerState for PausedScaled{}
impl TimerState for UnpausedScaled{}
pub trait IsPaused{}
impl IsPaused for Paused{}
impl IsPaused for PausedScaled{}
pub trait IsUnpaused{}
impl IsUnpaused for Unpaused{}
impl IsUnpaused for UnpausedScaled{}
pub trait IsScaled{}
impl IsScaled for PausedScaled{}
impl IsScaled for UnpausedScaled{}
pub trait IsUnscaled{}
impl IsUnscaled for Paused{}
impl IsUnscaled for Unpaused{}
pub struct Timer<State:TimerState>{
offset:crate::physics::TIME,
state:State,
}
fn get_offset(time:TIME,write_time:TIME)->TIME{
write_time-time
}
fn get_offset_scaled(time:TIME,write_time:TIME,scale:&Timescale)->TIME{
write_time-time*scale.num/scale.den.get() as i64
}
fn paused()->Timer<Paused>{
Timer{
offset:0,
state:Paused{},
}
}
fn unpaused()->Timer<Unpaused>{
Timer{
offset:0,
state:Unpaused{},
}
}
fn paused_scaled(scale:Timescale)->Timer<PausedScaled>{
Timer{
offset:0,
state:PausedScaled{scale},
}
}
fn unpaused_scaled(scale:Timescale)->Timer<UnpausedScaled>{
Timer{
offset:0,
state:UnpausedScaled{scale},
}
}
impl Timer<Paused>{
pub fn time(&self)->TIME{
self.offset
}
pub fn unpause(self,time:TIME)->Timer<Unpaused>{
Timer{
offset:get_offset(time,self.time()),
state:Unpaused{},
}
}
pub fn set_time(&mut self,time:TIME,write_time:TIME){
self.offset=get_offset(time,write_time);
}
pub fn set_scale(self,time:TIME,scale:Timescale)->Timer<PausedScaled>{
Timer{
offset:get_offset_scaled(time,self.time(),&scale),
state:PausedScaled{scale},
}
}
}
impl Timer<Unpaused>{
pub fn time(&self,time:TIME)->TIME{
self.offset+time
}
pub fn pause(self,time:TIME)->Timer<Paused>{
Timer{
offset:self.time(time),
state:Paused{},
}
}
pub fn set_time(&mut self,time:TIME,write_time:TIME){
self.offset=get_offset(time,write_time);
}
pub fn set_scale(self,time:TIME,scale:Timescale)->Timer<UnpausedScaled>{
Timer{
offset:get_offset_scaled(time,self.time(time),&scale),
state:UnpausedScaled{scale},
}
}
}
impl Timer<PausedScaled>{
pub fn time(&self)->TIME{
self.offset
}
pub fn unpause(self,time:TIME)->Timer<UnpausedScaled>{
Timer{
offset:get_offset_scaled(time,self.time(),&self.state.scale),
state:UnpausedScaled{scale:self.state.scale},
}
}
pub fn set_time(&mut self,time:TIME,write_time:TIME){
self.offset=get_offset_scaled(time,write_time,&self.state.scale);
}
pub fn set_scale(self,time:TIME,scale:Timescale)->Timer<PausedScaled>{
Timer{
offset:get_offset_scaled(time,self.time(),&scale),
state:PausedScaled{scale},
}
}
}
impl Timer<UnpausedScaled>{
pub fn time(&self,time:TIME)->TIME{
self.offset+time*self.state.scale.num/self.state.scale.den.get() as i64
}
pub fn pause(self,time:TIME)->Timer<PausedScaled>{
Timer{
offset:self.time(time),
state:PausedScaled{scale:self.state.scale},
}
}
pub fn set_time(&mut self,time:TIME,write_time:TIME){
self.offset=get_offset_scaled(time,write_time,&self.state.scale);
}
pub fn set_scale(self,time:TIME,scale:Timescale)->Timer<UnpausedScaled>{
Timer{
offset:get_offset_scaled(time,self.time(time),&scale),
//self.offset+time*self.state.scale.num/self.state.scale.den.get() as i64-time*scale.num/scale.den.get() as i64
state:UnpausedScaled{scale},
}
}
}