game ticks api

This commit is contained in:
Quaternions 2024-10-16 18:47:03 -07:00
parent 6f12c20e36
commit 658c20892e
2 changed files with 22 additions and 0 deletions

View File

@ -130,4 +130,23 @@ impl Runnable<'_>{
// No need to schedule the thread here // No need to schedule the thread here
Ok(()) Ok(())
} }
pub fn has_scheduled_threads(&self)->Result<bool,mlua::Error>{
scheduler_mut(&self.lua,|scheduler|
Ok(scheduler.has_scheduled_threads())
)
}
pub fn game_tick(&self)->Result<(),mlua::Error>{
if let Some(threads)=scheduler_mut(&self.lua,|scheduler|Ok(scheduler.tick_threads()))?{
for thread in threads{
//TODO: return dt and total run time
let result=thread.resume::<mlua::MultiValue>((1.0/20.0,0.0))
.map_err(|error|Error::Lua{source:"source unavailable".to_owned(),error});
match result{
Ok(_)=>(),
Err(e)=>println!("game_tick Error: {e}"),
}
}
}
Ok(())
}
} }

View File

@ -43,6 +43,9 @@ impl Scheduler{
pub const fn tick(&self)->Tick{ pub const fn tick(&self)->Tick{
self.tick self.tick
} }
pub fn has_scheduled_threads(&self)->bool{
!self.schedule.is_empty()
}
pub fn schedule_thread(&mut self,delay:u64,thread:mlua::Thread){ pub fn schedule_thread(&mut self,delay:u64,thread:mlua::Thread){
self.schedule.entry(self.tick+delay.max(1)) self.schedule.entry(self.tick+delay.max(1))
.or_insert(Vec::new()) .or_insert(Vec::new())