1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! Time management and fixed timestep support.
//!
//! This module provides:
//! - [`Time`] - Frame timing and delta time tracking
//! - [`FixedTime`] - Fixed timestep for deterministic updates
//!
//! # Examples
//!
//! ```
//! use archetype_ecs::time::{Time, FixedTime};
//!
//! let mut time = Time::new();
//! let mut fixed = FixedTime::new(60); // 60 Hz
//!
//! // In your game loop:
//! time.update();
//! for _ in 0..fixed.tick(time.delta()) {
//! // Run physics at fixed 60 Hz
//! }
//! ```
use std::time::Duration;
/// Time resource for tracking frame timing
#[derive(Clone, Debug)]
pub struct Time {
/// Time since last frame
delta: Duration,
/// Total elapsed time since start
elapsed: Duration,
/// Frame counter
frame_count: u64,
/// Time scale multiplier (1.0 = normal speed)
time_scale: f32,
/// Time at start of current frame
startup_time: std::time::Instant,
/// Time of last frame
last_update: std::time::Instant,
}
impl Time {
/// Create new Time resource
pub fn new() -> Self {
let now = std::time::Instant::now();
Self {
delta: Duration::ZERO,
elapsed: Duration::ZERO,
frame_count: 0,
time_scale: 1.0,
startup_time: now,
last_update: now,
}
}
/// Update time (call once per frame)
pub fn update(&mut self) {
let now = std::time::Instant::now();
self.delta = now.duration_since(self.last_update);
self.elapsed = now.duration_since(self.startup_time);
self.last_update = now;
self.frame_count += 1;
}
/// Get delta time (time since last frame)
pub fn delta(&self) -> Duration {
self.delta
}
/// Get scaled delta time
pub fn delta_seconds(&self) -> f32 {
self.delta.as_secs_f32() * self.time_scale
}
/// Get total elapsed time
pub fn elapsed(&self) -> Duration {
self.elapsed
}
/// Get elapsed time in seconds
pub fn elapsed_seconds(&self) -> f32 {
self.elapsed.as_secs_f32()
}
/// Get current frame count
pub fn frame_count(&self) -> u64 {
self.frame_count
}
/// Set time scale (1.0 = normal, 0.5 = half speed, 2.0 = double speed)
pub fn set_time_scale(&mut self, scale: f32) {
self.time_scale = scale.max(0.0);
}
/// Get time scale
pub fn time_scale(&self) -> f32 {
self.time_scale
}
/// Pause time (set scale to 0)
pub fn pause(&mut self) {
self.time_scale = 0.0;
}
/// Resume time (set scale to 1)
pub fn resume(&mut self) {
self.time_scale = 1.0;
}
/// Check if time is paused
pub fn is_paused(&self) -> bool {
self.time_scale == 0.0
}
}
impl Default for Time {
fn default() -> Self {
Self::new()
}
}
/// Fixed timestep for deterministic updates
#[derive(Clone, Debug)]
pub struct FixedTime {
/// Fixed timestep duration
timestep: Duration,
/// Accumulated time from variable frame rate
accumulator: Duration,
/// Overstep from last frame (for interpolation)
overstep: Duration,
}
impl FixedTime {
/// Create new FixedTime with given frequency (Hz)
pub fn new(hz: u32) -> Self {
let timestep = Duration::from_secs_f32(1.0 / hz as f32);
Self {
timestep,
accumulator: Duration::ZERO,
overstep: Duration::ZERO,
}
}
/// Create with explicit timestep duration
pub fn from_duration(timestep: Duration) -> Self {
Self {
timestep,
accumulator: Duration::ZERO,
overstep: Duration::ZERO,
}
}
/// Update accumulator and return number of fixed steps to run
pub fn tick(&mut self, delta: Duration) -> usize {
self.accumulator += delta;
let mut steps = 0;
while self.accumulator >= self.timestep {
self.accumulator -= self.timestep;
steps += 1;
}
self.overstep = self.accumulator;
steps
}
/// Get fixed timestep duration
pub fn timestep(&self) -> Duration {
self.timestep
}
/// Get timestep in seconds
pub fn timestep_seconds(&self) -> f32 {
self.timestep.as_secs_f32()
}
/// Get overstep (for interpolation)
pub fn overstep(&self) -> Duration {
self.overstep
}
/// Get overstep as fraction of timestep (0.0 to 1.0)
pub fn overstep_fraction(&self) -> f32 {
if self.timestep.as_secs_f32() > 0.0 {
self.overstep.as_secs_f32() / self.timestep.as_secs_f32()
} else {
0.0
}
}
}
impl Default for FixedTime {
fn default() -> Self {
Self::new(60) // 60 Hz default
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_time_creation() {
let time = Time::new();
assert_eq!(time.frame_count(), 0);
assert_eq!(time.time_scale(), 1.0);
}
#[test]
fn test_time_pause() {
let mut time = Time::new();
time.pause();
assert!(time.is_paused());
time.resume();
assert!(!time.is_paused());
}
#[test]
fn test_fixed_time_60hz() {
let mut fixed = FixedTime::new(60);
// 16.67ms frame (60 FPS)
let steps = fixed.tick(Duration::from_millis(16));
assert_eq!(steps, 0); // Not quite a full step yet
// Another frame
let steps = fixed.tick(Duration::from_millis(17));
assert_eq!(steps, 1); // Now we have enough
}
#[test]
fn test_fixed_time_slow_frame() {
let mut fixed = FixedTime::new(60);
// 33ms frame (30 FPS) - should run 2 fixed steps
let steps = fixed.tick(Duration::from_millis(33));
assert_eq!(steps, 1); // First step
}
#[test]
fn test_overstep_fraction() {
let mut fixed = FixedTime::new(60);
fixed.tick(Duration::from_millis(8));
let fraction = fixed.overstep_fraction();
assert!(fraction > 0.0 && fraction < 1.0);
}
}