unity_mirror_rs/unity_engine/
player_looper.rs1use crate::mirror::NetworkLoop;
2use crate::unity_engine::time::Time;
3use crate::unity_engine::world::WorldManager;
4use std::time::Instant;
5
6pub struct PlayerLooper {
7 last_frame_time: Instant,
8 last_fixed_time: Instant,
9}
10
11impl PlayerLooper {
12 pub fn run() {
13 PlayerLooper {
14 last_frame_time: Instant::now(),
15 last_fixed_time: Instant::now(),
16 }
17 ._run()
18 }
19
20 pub fn fixed_update(&mut self) {
21 if let Some(elapsed) = self
22 .last_fixed_time
23 .checked_sub(Time::unscaled_time().elapsed())
24 {
25 if elapsed.elapsed() >= Time::get_fixed_data_time_duration() {
26 self.last_fixed_time = Instant::now();
27 WorldManager::fixed_update();
28 }
29 }
30 }
31
32 pub fn frame_update(&mut self) {
33 if let Some(elapsed) = self
34 .last_frame_time
35 .checked_sub(Time::unscaled_time().elapsed())
36 {
37 if elapsed.elapsed() >= Time::get_frame_rate_duration() {
38 self.last_frame_time = Instant::now();
39 NetworkLoop.network_early_update(); WorldManager::update();
41 WorldManager::late_update();
42 NetworkLoop.network_late_update(); }
44 }
45 }
46
47 fn _run(&mut self) {
48 Time::start_instant();
49 loop {
50 let tmp_instant = Instant::now();
51 self.fixed_update();
52 self.frame_update();
53 Time::frame_add();
54
55 let elapsed = tmp_instant.elapsed();
56 if elapsed < Time::get_min_interval() {
57 let diff_duration = Time::get_min_interval().abs_diff(elapsed);
58 std::thread::sleep(diff_duration);
59 }
60 }
61 }
62}