|
1 |
| -use bevy_utils::Duration; |
2 | 1 | use bevy_ecs::world::World;
|
3 | 2 | use bevy_reflect::{FromReflect, Reflect};
|
| 3 | +use bevy_utils::Duration; |
4 | 4 |
|
5 |
| -use crate::FixedUpdate; |
6 |
| -use crate::time::Time; |
7 |
| -use crate::virt::Virtual; |
| 5 | +use crate::{time::Time, virt::Virtual, FixedUpdate}; |
8 | 6 |
|
9 | 7 | /// The fixed timestep game clock following virtual time.
|
10 | 8 | ///
|
11 | 9 | /// Normally used as `Time<Fixed>`. It is automatically inserted as a resource
|
12 | 10 | /// by `TimePlugin` and updated based on `Time<Virtual>`. The fixed clock is
|
13 | 11 | /// automatically set as the generic `Time` resource during `FixedUpdate`
|
14 | 12 | /// schedule processing.
|
15 |
| -/// |
| 13 | +/// |
16 | 14 | /// The fixed timestep game clock runs at a regular interval and is extremely
|
17 |
| -/// useful for steady, frame-rate independent gameplay logic and physics. |
18 |
| -/// |
| 15 | +/// useful for steady, frame-rate independent gameplay logic and physics. |
| 16 | +/// |
19 | 17 | /// To run a system on a fixed timestep, add it to the `FixedUpdate` schedule.
|
20 | 18 | /// This schedule is run a number of times between `PreUpdate` and `Update`
|
21 | 19 | /// according to the accumulated `overstep()` time divided by the `timestep()`.
|
22 | 20 | /// This means the schedule may run 0, 1 or more times during a single update.
|
23 |
| -/// |
| 21 | +/// |
24 | 22 | /// `Time<Fixed>` and the generic `Time` resource will report a `delta()` equal
|
25 | 23 | /// to `timestep()` and always grow `elapsed()` by one `timestep()` per
|
26 | 24 | /// iteration.
|
27 |
| -/// |
| 25 | +/// |
28 | 26 | /// The fixed timestep clock follows the `Time<Virtual>` clock, which means it
|
29 | 27 | /// is affected by `paused()`, `relative_speed()` and `max_delta()` from virtual
|
30 | 28 | /// time. If the virtual clock is paused, the `FixedUpdate` schedule will not
|
31 | 29 | /// run. It is guaranteed that the `elapsed()` time in `Time<Fixed>` is always
|
32 | 30 | /// between the previous `elapsed()` and the next `elapsed()` value in
|
33 | 31 | /// `Time<Virtual>`, so the values are compatible.
|
34 |
| -/// |
| 32 | +/// |
35 | 33 | /// Changing the timestep size while the game is running should not normally be
|
36 | 34 | /// done, as having a regular interval is the point of this schedule, but it may
|
37 | 35 | /// be necessary for effects like "bullet-time" if the normal granularity of the
|
@@ -78,34 +76,41 @@ impl Time<Fixed> {
|
78 | 76 |
|
79 | 77 | /// Sets the amount of virtual time that must pass before the fixed timestep
|
80 | 78 | /// schedule is run again, as [`Duration`].
|
81 |
| - /// |
| 79 | + /// |
82 | 80 | /// Takes effect immediately on the next run of the schedule, respecting
|
83 | 81 | /// what is currently in `overstep()`.
|
84 | 82 | pub fn set_timestep(&mut self, timestep: Duration) {
|
85 |
| - assert_ne!(timestep, Duration::ZERO, "attempted to set fixed timestep to zero"); |
| 83 | + assert_ne!( |
| 84 | + timestep, |
| 85 | + Duration::ZERO, |
| 86 | + "attempted to set fixed timestep to zero" |
| 87 | + ); |
86 | 88 | self.context_mut().timestep = timestep;
|
87 | 89 | }
|
88 | 90 |
|
89 | 91 | /// Sets the amount of virtual time that must pass before the fixed timestep
|
90 | 92 | /// schedule is run again, as seconds.
|
91 |
| - /// |
| 93 | + /// |
92 | 94 | /// Timestep is stored as a `Duration`, which has fixed nanosecond
|
93 | 95 | /// resolution and will be converted from the floating point number.
|
94 |
| - /// |
| 96 | + /// |
95 | 97 | /// Takes effect immediately on the next run of the schedule, respecting
|
96 | 98 | /// what is currently in `overstep()`.
|
97 | 99 | pub fn set_timestep_seconds(&mut self, seconds: f64) {
|
98 |
| - assert!(seconds.is_sign_positive(), "seconds less than or equal to zero"); |
| 100 | + assert!( |
| 101 | + seconds.is_sign_positive(), |
| 102 | + "seconds less than or equal to zero" |
| 103 | + ); |
99 | 104 | assert!(seconds.is_finite(), "seconds is infinite");
|
100 | 105 | self.set_timestep(Duration::from_secs_f64(seconds));
|
101 | 106 | }
|
102 | 107 |
|
103 | 108 | /// Sets the amount of virtual time that must pass before the fixed timestep
|
104 | 109 | /// schedule is run again, as frequency.
|
105 |
| - /// |
| 110 | + /// |
106 | 111 | /// The timestep value is set to `1 / hz`, converted to a `Duration` which
|
107 | 112 | /// has fixed nanosecond resolution.
|
108 |
| - /// |
| 113 | + /// |
109 | 114 | /// Takes effect immediately on the next run of the schedule, respecting
|
110 | 115 | /// what is currently in `overstep()`.
|
111 | 116 | pub fn set_timestep_hz(&mut self, hz: f64) {
|
@@ -159,9 +164,7 @@ impl Default for Fixed {
|
159 | 164 | }
|
160 | 165 | }
|
161 | 166 |
|
162 |
| -pub fn run_fixed_update_schedule( |
163 |
| - world: &mut World |
164 |
| -) { |
| 167 | +pub fn run_fixed_update_schedule(world: &mut World) { |
165 | 168 | let delta = world.resource::<Time<Virtual>>().delta();
|
166 | 169 | world.resource_mut::<Time<Fixed>>().accumulate(delta);
|
167 | 170 |
|
|
0 commit comments