@@ -28,45 +28,60 @@ use bevy_utils::Duration;
28
28
use thiserror:: Error ;
29
29
30
30
/// The amount of time that must pass before the fixed timestep schedule is run again.
31
+ ///
32
+ /// For more information, see the [module-level documentation](self).
33
+ ///
34
+ /// When using bevy's default configuration, this will be updated using the [`Time`]
35
+ /// resource. To customize how `Time` is updated each frame, see [`TimeUpdateStrategy`].
36
+ ///
37
+ /// [`TimeUpdateStrategy`]: crate::TimeUpdateStrategy
31
38
#[ derive( Resource , Debug ) ]
32
39
pub struct FixedTime {
33
40
accumulated : Duration ,
41
+ /// The amount of time spanned by each fixed update.
34
42
/// Defaults to 1/60th of a second.
35
- /// To configure this value, simply mutate or overwrite this resource.
43
+ ///
44
+ /// To configure this value, simply mutate or overwrite this field.
36
45
pub period : Duration ,
37
46
}
38
47
39
48
impl FixedTime {
40
- /// Creates a new [`FixedTime`] struct
49
+ /// Creates a new [`FixedTime`] struct with a specified period.
41
50
pub fn new ( period : Duration ) -> Self {
42
51
FixedTime {
43
52
accumulated : Duration :: ZERO ,
44
53
period,
45
54
}
46
55
}
47
56
48
- /// Creates a new [`FixedTime`] struct with a period specified in `f32` seconds
57
+ /// Creates a new [`FixedTime`] struct with a period specified in seconds.
49
58
pub fn new_from_secs ( period : f32 ) -> Self {
50
59
FixedTime {
51
60
accumulated : Duration :: ZERO ,
52
61
period : Duration :: from_secs_f32 ( period) ,
53
62
}
54
63
}
55
64
56
- /// Adds the `delta_time` to the accumulated time so far.
65
+ /// Adds to this instance's accumulated time. `delta_time` should be the amount of in-game time
66
+ /// that has passed since `tick` was last called.
67
+ ///
68
+ /// Note that if you are using the default configuration of bevy, this will be called for you.
57
69
pub fn tick ( & mut self , delta_time : Duration ) {
58
70
self . accumulated += delta_time;
59
71
}
60
72
61
- /// Returns the current amount of accumulated time
73
+ /// Returns the current amount of accumulated time.
74
+ ///
75
+ /// Approximately, this represents how far behind the fixed update schedule is from the main schedule.
62
76
pub fn accumulated ( & self ) -> Duration {
63
77
self . accumulated
64
78
}
65
79
66
- /// Expends one `period` of accumulated time.
80
+ /// Attempts to advance by a single period. This will return [`FixedUpdateError`] if there is not enough
81
+ /// accumulated time -- in other words, if advancing time would put the fixed update schedule
82
+ /// ahead of the main schedule.
67
83
///
68
- /// [`Err(FixedUpdateError`)] will be returned if there is
69
- /// not enough accumulated time to span an entire period.
84
+ /// Note that if you are using the default configuration of bevy, this will be called for you.
70
85
pub fn expend ( & mut self ) -> Result < ( ) , FixedUpdateError > {
71
86
if let Some ( new_value) = self . accumulated . checked_sub ( self . period ) {
72
87
self . accumulated = new_value;
@@ -92,14 +107,19 @@ impl Default for FixedTime {
92
107
/// An error returned when working with [`FixedTime`].
93
108
#[ derive( Debug , Error ) ]
94
109
pub enum FixedUpdateError {
110
+ /// There is not enough accumulated time to advance the fixed update schedule.
95
111
#[ error( "At least one period worth of time must be accumulated." ) ]
96
112
NotEnoughTime {
113
+ /// The amount of time available to advance the fixed update schedule.
97
114
accumulated : Duration ,
115
+ /// The length of one fixed update.
98
116
period : Duration ,
99
117
} ,
100
118
}
101
119
102
120
/// Ticks the [`FixedTime`] resource then runs the [`FixedUpdate`].
121
+ ///
122
+ /// For more information, see the [module-level documentation](self).
103
123
pub fn run_fixed_update_schedule ( world : & mut World ) {
104
124
// Tick the time
105
125
let delta_time = world. resource :: < Time > ( ) . delta ( ) ;
0 commit comments