From 7482a0d26d92f5eb98d15245e976ef044f3463ce Mon Sep 17 00:00:00 2001 From: andristarr Date: Wed, 16 Oct 2024 23:09:32 +0200 Subject: [PATCH] aligning public apis of Time,Timer and Stopwatch (#15962) Fixes #15834 ## Migration Guide The APIs of `Time`, `Timer` and `Stopwatch` have been cleaned up for consistency with each other and the standard library's `Duration` type. The following methods have been renamed: - `Stowatch::paused` -> `Stopwatch::is_paused` - `Time::elapsed_seconds` -> `Time::elasped_secs` (including `_f64` and `_wrapped` variants) --- crates/bevy_animation/src/lib.rs | 2 +- crates/bevy_animation/src/transition.rs | 2 +- .../src/frame_time_diagnostics_plugin.rs | 2 +- crates/bevy_render/src/globals.rs | 4 +- crates/bevy_time/src/stopwatch.rs | 26 +-- crates/bevy_time/src/time.rs | 176 +++++++++--------- crates/bevy_time/src/timer.rs | 18 +- examples/2d/bloom_2d.rs | 2 +- examples/2d/bounding_2d.rs | 13 +- examples/2d/move_sprite.rs | 4 +- examples/2d/pixel_grid_snap.rs | 2 +- examples/2d/rotation.rs | 6 +- examples/2d/sprite_tile.rs | 2 +- examples/2d/text2d.rs | 8 +- examples/3d/3d_shapes.rs | 2 +- examples/3d/animated_material.rs | 2 +- examples/3d/anisotropy.rs | 2 +- examples/3d/auto_exposure.rs | 4 +- examples/3d/blend_modes.rs | 8 +- examples/3d/bloom_3d.rs | 4 +- examples/3d/camera_sub_view.rs | 2 +- examples/3d/clearcoat.rs | 4 +- examples/3d/deferred_rendering.rs | 8 +- examples/3d/fog.rs | 4 +- examples/3d/generate_custom_mesh.rs | 6 +- examples/3d/irradiance_volumes.rs | 2 +- examples/3d/lighting.rs | 4 +- examples/3d/load_gltf.rs | 2 +- examples/3d/mesh_ray_cast.rs | 2 +- examples/3d/motion_blur.rs | 2 +- examples/3d/parallax_mapping.rs | 6 +- examples/3d/parenting.rs | 2 +- examples/3d/query_gltf_primitives.rs | 4 +- examples/3d/reflection_probes.rs | 2 +- examples/3d/render_to_texture.rs | 8 +- examples/3d/rotate_environment_map.rs | 2 +- examples/3d/scrolling_fog.rs | 2 +- examples/3d/skybox.rs | 4 +- examples/3d/spotlight.rs | 10 +- examples/3d/ssao.rs | 2 +- examples/3d/ssr.rs | 2 +- examples/3d/tonemapping.rs | 2 +- examples/3d/transmission.rs | 36 ++-- examples/3d/transparency_3d.rs | 2 +- examples/3d/update_gltf_scene.rs | 4 +- examples/3d/volumetric_fog.rs | 2 +- examples/animation/animated_fox.rs | 4 +- examples/animation/animation_events.rs | 2 +- examples/animation/color_animation.rs | 4 +- examples/animation/custom_skinned_mesh.rs | 24 +-- examples/animation/easing_functions.rs | 2 +- examples/animation/gltf_skinned_mesh.rs | 2 +- .../external_source_external_thread.rs | 2 +- examples/audio/audio_control.rs | 2 +- examples/audio/soundtrack.rs | 4 +- examples/audio/spatial_audio_2d.rs | 12 +- examples/audio/spatial_audio_3d.rs | 12 +- examples/camera/2d_screen_shake.rs | 8 +- examples/camera/2d_top_down_camera.rs | 4 +- examples/camera/camera_orbit.rs | 2 +- examples/ecs/change_detection.rs | 6 +- examples/ecs/fallible_params.rs | 6 +- examples/ecs/fixed_timestep.rs | 10 +- examples/ecs/hierarchy.rs | 8 +- examples/ecs/iter_combinations.rs | 2 +- examples/ecs/removal_detection.rs | 2 +- examples/ecs/run_conditions.rs | 2 +- examples/games/alien_cake_addict.rs | 9 +- examples/games/breakout.rs | 6 +- examples/games/contributors.rs | 4 +- examples/games/desk_toy.rs | 6 +- examples/gizmos/2d_gizmos.rs | 16 +- examples/gizmos/3d_gizmos.rs | 18 +- examples/gizmos/axes.rs | 2 +- examples/gizmos/light_gizmos.rs | 6 +- examples/helpers/camera_controller.rs | 2 +- examples/input/text_input.rs | 2 +- examples/math/custom_primitives.rs | 4 +- examples/math/render_primitives.rs | 16 +- examples/math/sampling_primitives.rs | 4 +- .../movement/physics_in_fixed_timestep.rs | 4 +- examples/movement/smooth_follow.rs | 4 +- examples/picking/mesh_picking.rs | 2 +- examples/picking/sprite_picking.rs | 2 +- examples/remote/server.rs | 2 +- examples/shader/custom_post_processing.rs | 6 +- examples/shader/extended_material.rs | 2 +- .../shader_material_screenspace_texture.rs | 2 +- examples/shader/shader_prepass.rs | 2 +- examples/shader/storage_buffer.rs | 2 +- examples/state/computed_states.rs | 4 +- examples/state/custom_transitions.rs | 4 +- examples/state/states.rs | 4 +- examples/state/sub_states.rs | 4 +- examples/stress_tests/bevymark.rs | 4 +- .../stress_tests/many_animated_sprites.rs | 4 +- examples/stress_tests/many_cubes.rs | 2 +- examples/stress_tests/many_foxes.rs | 2 +- examples/stress_tests/many_gizmos.rs | 2 +- examples/stress_tests/many_lights.rs | 2 +- examples/stress_tests/many_sprites.rs | 4 +- examples/stress_tests/text_pipeline.rs | 2 +- examples/stress_tests/transform_hierarchy.rs | 2 +- examples/time/virtual_time.rs | 12 +- .../tools/scene_viewer/morph_viewer_plugin.rs | 2 +- .../tools/scene_viewer/scene_viewer_plugin.rs | 2 +- examples/transforms/3d_rotation.rs | 4 +- examples/transforms/align.rs | 2 +- examples/transforms/scale.rs | 2 +- examples/transforms/transform.rs | 4 +- examples/transforms/translation.rs | 2 +- examples/ui/overflow_debug.rs | 2 +- examples/ui/render_ui_to_texture.rs | 4 +- examples/ui/text.rs | 2 +- examples/ui/text_debug.rs | 2 +- examples/ui/ui_material.rs | 6 +- examples/ui/viewport_debug.rs | 2 +- examples/window/low_power.rs | 4 +- 118 files changed, 380 insertions(+), 370 deletions(-) diff --git a/crates/bevy_animation/src/lib.rs b/crates/bevy_animation/src/lib.rs index fa86e82b9e..e437769d41 100755 --- a/crates/bevy_animation/src/lib.rs +++ b/crates/bevy_animation/src/lib.rs @@ -1005,7 +1005,7 @@ pub fn advance_animations( animation_graphs: Res>, mut players: Query<(&mut AnimationPlayer, &AnimationGraphHandle)>, ) { - let delta_seconds = time.delta_seconds(); + let delta_seconds = time.delta_secs(); players .par_iter_mut() .for_each(|(mut player, graph_handle)| { diff --git a/crates/bevy_animation/src/transition.rs b/crates/bevy_animation/src/transition.rs index 3b9eaa2805..679c63bec3 100644 --- a/crates/bevy_animation/src/transition.rs +++ b/crates/bevy_animation/src/transition.rs @@ -122,7 +122,7 @@ pub fn advance_transitions( for transition in &mut animation_transitions.transitions.iter_mut().rev() { // Decrease weight. transition.current_weight = (transition.current_weight - - transition.weight_decline_per_sec * time.delta_seconds()) + - transition.weight_decline_per_sec * time.delta_secs()) .max(0.0); // Update weight. diff --git a/crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs index 99a7270617..040a05773c 100644 --- a/crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs @@ -33,7 +33,7 @@ impl FrameTimeDiagnosticsPlugin { ) { diagnostics.add_measurement(&Self::FRAME_COUNT, || frame_count.0 as f64); - let delta_seconds = time.delta_seconds_f64(); + let delta_seconds = time.delta_secs_f64(); if delta_seconds == 0.0 { return; } diff --git a/crates/bevy_render/src/globals.rs b/crates/bevy_render/src/globals.rs index e89648fb56..eabaaad05e 100644 --- a/crates/bevy_render/src/globals.rs +++ b/crates/bevy_render/src/globals.rs @@ -74,8 +74,8 @@ fn prepare_globals_buffer( frame_count: Res, ) { let buffer = globals_buffer.buffer.get_mut(); - buffer.time = time.elapsed_seconds_wrapped(); - buffer.delta_time = time.delta_seconds(); + buffer.time = time.elapsed_secs_wrapped(); + buffer.delta_time = time.delta_secs(); buffer.frame_count = frame_count.0; globals_buffer diff --git a/crates/bevy_time/src/stopwatch.rs b/crates/bevy_time/src/stopwatch.rs index 352749848d..b604a8890b 100644 --- a/crates/bevy_time/src/stopwatch.rs +++ b/crates/bevy_time/src/stopwatch.rs @@ -21,7 +21,7 @@ use bevy_utils::Duration; /// assert_eq!(stopwatch.elapsed_secs(), 1.0); /// /// stopwatch.reset(); // reset the stopwatch -/// assert!(stopwatch.paused()); +/// assert!(stopwatch.is_paused()); /// assert_eq!(stopwatch.elapsed_secs(), 0.0); /// ``` #[derive(Clone, Debug, Default, PartialEq, Eq)] @@ -29,7 +29,7 @@ use bevy_utils::Duration; #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Default))] pub struct Stopwatch { elapsed: Duration, - paused: bool, + is_paused: bool, } impl Stopwatch { @@ -40,7 +40,7 @@ impl Stopwatch { /// # use bevy_time::*; /// let stopwatch = Stopwatch::new(); /// assert_eq!(stopwatch.elapsed_secs(), 0.0); - /// assert_eq!(stopwatch.paused(), false); + /// assert_eq!(stopwatch.is_paused(), false); /// ``` pub fn new() -> Self { Default::default() @@ -128,7 +128,7 @@ impl Stopwatch { /// assert_eq!(stopwatch.elapsed_secs(), 1.5); /// ``` pub fn tick(&mut self, delta: Duration) -> &Self { - if !self.paused() { + if !self.is_paused() { self.elapsed = self.elapsed.saturating_add(delta); } self @@ -144,12 +144,12 @@ impl Stopwatch { /// let mut stopwatch = Stopwatch::new(); /// stopwatch.pause(); /// stopwatch.tick(Duration::from_secs_f32(1.5)); - /// assert!(stopwatch.paused()); + /// assert!(stopwatch.is_paused()); /// assert_eq!(stopwatch.elapsed_secs(), 0.0); /// ``` #[inline] pub fn pause(&mut self) { - self.paused = true; + self.is_paused = true; } /// Unpauses the stopwatch. Resume the effect of ticking on elapsed time. @@ -163,12 +163,12 @@ impl Stopwatch { /// stopwatch.tick(Duration::from_secs_f32(1.0)); /// stopwatch.unpause(); /// stopwatch.tick(Duration::from_secs_f32(1.0)); - /// assert!(!stopwatch.paused()); + /// assert!(!stopwatch.is_paused()); /// assert_eq!(stopwatch.elapsed_secs(), 1.0); /// ``` #[inline] pub fn unpause(&mut self) { - self.paused = false; + self.is_paused = false; } /// Returns `true` if the stopwatch is paused. @@ -177,15 +177,15 @@ impl Stopwatch { /// ``` /// # use bevy_time::*; /// let mut stopwatch = Stopwatch::new(); - /// assert!(!stopwatch.paused()); + /// assert!(!stopwatch.is_paused()); /// stopwatch.pause(); - /// assert!(stopwatch.paused()); + /// assert!(stopwatch.is_paused()); /// stopwatch.unpause(); - /// assert!(!stopwatch.paused()); + /// assert!(!stopwatch.is_paused()); /// ``` #[inline] - pub fn paused(&self) -> bool { - self.paused + pub fn is_paused(&self) -> bool { + self.is_paused } /// Resets the stopwatch. The reset doesn't affect the paused state of the stopwatch. diff --git a/crates/bevy_time/src/time.rs b/crates/bevy_time/src/time.rs index 739f7f6742..3db9bef56d 100644 --- a/crates/bevy_time/src/time.rs +++ b/crates/bevy_time/src/time.rs @@ -30,15 +30,15 @@ use { /// which will set [`delta()`](Time::delta) to zero. /// /// These values are also available in seconds as `f32` via -/// [`delta_seconds()`](Time::delta_seconds) and -/// [`elapsed_seconds()`](Time::elapsed_seconds), and also in seconds as `f64` -/// via [`delta_seconds_f64()`](Time::delta_seconds_f64) and -/// [`elapsed_seconds_f64()`](Time::elapsed_seconds_f64). +/// [`delta_secs()`](Time::delta_secs) and +/// [`elapsed_secs()`](Time::elapsed_secs), and also in seconds as `f64` +/// via [`delta_secs_f64()`](Time::delta_secs_f64) and +/// [`elapsed_secs_f64()`](Time::elapsed_secs_f64). /// -/// Since [`elapsed_seconds()`](Time::elapsed_seconds) will grow constantly and +/// Since [`elapsed_secs()`](Time::elapsed_secs) will grow constantly and /// is `f32`, it will exhibit gradual precision loss. For applications that /// require an `f32` value but suffer from gradual precision loss there is -/// [`elapsed_seconds_wrapped()`](Time::elapsed_seconds_wrapped) available. The +/// [`elapsed_secs_wrapped()`](Time::elapsed_secs_wrapped) available. The /// same wrapped value is also available as [`Duration`] and `f64` for /// consistency. The wrap period is by default 1 hour, and can be set by /// [`set_wrap_period()`](Time::set_wrap_period). @@ -193,14 +193,14 @@ pub struct Time { context: T, wrap_period: Duration, delta: Duration, - delta_seconds: f32, - delta_seconds_f64: f64, + delta_secs: f32, + delta_secs_f64: f64, elapsed: Duration, - elapsed_seconds: f32, - elapsed_seconds_f64: f64, + elapsed_secs: f32, + elapsed_secs_f64: f64, elapsed_wrapped: Duration, - elapsed_seconds_wrapped: f32, - elapsed_seconds_wrapped_f64: f64, + elapsed_secs_wrapped: f32, + elapsed_secs_wrapped_f64: f64, } impl Time { @@ -222,14 +222,14 @@ impl Time { /// [`Duration::ZERO`] is allowed and will set [`Self::delta`] to zero. pub fn advance_by(&mut self, delta: Duration) { self.delta = delta; - self.delta_seconds = self.delta.as_secs_f32(); - self.delta_seconds_f64 = self.delta.as_secs_f64(); + self.delta_secs = self.delta.as_secs_f32(); + self.delta_secs_f64 = self.delta.as_secs_f64(); self.elapsed += delta; - self.elapsed_seconds = self.elapsed.as_secs_f32(); - self.elapsed_seconds_f64 = self.elapsed.as_secs_f64(); + self.elapsed_secs = self.elapsed.as_secs_f32(); + self.elapsed_secs_f64 = self.elapsed.as_secs_f64(); self.elapsed_wrapped = duration_rem(self.elapsed, self.wrap_period); - self.elapsed_seconds_wrapped = self.elapsed_wrapped.as_secs_f32(); - self.elapsed_seconds_wrapped_f64 = self.elapsed_wrapped.as_secs_f64(); + self.elapsed_secs_wrapped = self.elapsed_wrapped.as_secs_f32(); + self.elapsed_secs_wrapped_f64 = self.elapsed_wrapped.as_secs_f64(); } /// Advance this clock to a specific `elapsed` time. @@ -280,15 +280,15 @@ impl Time { /// Returns how much time has advanced since the last [`update`](#method.update), as [`f32`] /// seconds. #[inline] - pub fn delta_seconds(&self) -> f32 { - self.delta_seconds + pub fn delta_secs(&self) -> f32 { + self.delta_secs } /// Returns how much time has advanced since the last [`update`](#method.update), as [`f64`] /// seconds. #[inline] - pub fn delta_seconds_f64(&self) -> f64 { - self.delta_seconds_f64 + pub fn delta_secs_f64(&self) -> f64 { + self.delta_secs_f64 } /// Returns how much time has advanced since [`startup`](#method.startup), as [`Duration`]. @@ -301,16 +301,16 @@ impl Time { /// /// **Note:** This is a monotonically increasing value. Its precision will degrade over time. /// If you need an `f32` but that precision loss is unacceptable, - /// use [`elapsed_seconds_wrapped`](#method.elapsed_seconds_wrapped). + /// use [`elapsed_secs_wrapped`](#method.elapsed_secs_wrapped). #[inline] - pub fn elapsed_seconds(&self) -> f32 { - self.elapsed_seconds + pub fn elapsed_secs(&self) -> f32 { + self.elapsed_secs } /// Returns how much time has advanced since [`startup`](#method.startup), as [`f64`] seconds. #[inline] - pub fn elapsed_seconds_f64(&self) -> f64 { - self.elapsed_seconds_f64 + pub fn elapsed_secs_f64(&self) -> f64 { + self.elapsed_secs_f64 } /// Returns how much time has advanced since [`startup`](#method.startup) modulo @@ -324,17 +324,17 @@ impl Time { /// the [`wrap_period`](#method.wrap_period), as [`f32`] seconds. /// /// This method is intended for applications (e.g. shaders) that require an [`f32`] value but - /// suffer from the gradual precision loss of [`elapsed_seconds`](#method.elapsed_seconds). + /// suffer from the gradual precision loss of [`elapsed_secs`](#method.elapsed_secs). #[inline] - pub fn elapsed_seconds_wrapped(&self) -> f32 { - self.elapsed_seconds_wrapped + pub fn elapsed_secs_wrapped(&self) -> f32 { + self.elapsed_secs_wrapped } /// Returns how much time has advanced since [`startup`](#method.startup) modulo /// the [`wrap_period`](#method.wrap_period), as [`f64`] seconds. #[inline] - pub fn elapsed_seconds_wrapped_f64(&self) -> f64 { - self.elapsed_seconds_wrapped_f64 + pub fn elapsed_secs_wrapped_f64(&self) -> f64 { + self.elapsed_secs_wrapped_f64 } /// Returns a reference to the context of this specific clock. @@ -356,14 +356,14 @@ impl Time { context: (), wrap_period: self.wrap_period, delta: self.delta, - delta_seconds: self.delta_seconds, - delta_seconds_f64: self.delta_seconds_f64, + delta_secs: self.delta_secs, + delta_secs_f64: self.delta_secs_f64, elapsed: self.elapsed, - elapsed_seconds: self.elapsed_seconds, - elapsed_seconds_f64: self.elapsed_seconds_f64, + elapsed_secs: self.elapsed_secs, + elapsed_secs_f64: self.elapsed_secs_f64, elapsed_wrapped: self.elapsed_wrapped, - elapsed_seconds_wrapped: self.elapsed_seconds_wrapped, - elapsed_seconds_wrapped_f64: self.elapsed_seconds_wrapped_f64, + elapsed_secs_wrapped: self.elapsed_secs_wrapped, + elapsed_secs_wrapped_f64: self.elapsed_secs_wrapped_f64, } } } @@ -374,14 +374,14 @@ impl Default for Time { context: Default::default(), wrap_period: Self::DEFAULT_WRAP_PERIOD, delta: Duration::ZERO, - delta_seconds: 0.0, - delta_seconds_f64: 0.0, + delta_secs: 0.0, + delta_secs_f64: 0.0, elapsed: Duration::ZERO, - elapsed_seconds: 0.0, - elapsed_seconds_f64: 0.0, + elapsed_secs: 0.0, + elapsed_secs_f64: 0.0, elapsed_wrapped: Duration::ZERO, - elapsed_seconds_wrapped: 0.0, - elapsed_seconds_wrapped_f64: 0.0, + elapsed_secs_wrapped: 0.0, + elapsed_secs_wrapped_f64: 0.0, } } } @@ -402,14 +402,14 @@ mod test { assert_eq!(time.wrap_period(), Time::<()>::DEFAULT_WRAP_PERIOD); assert_eq!(time.delta(), Duration::ZERO); - assert_eq!(time.delta_seconds(), 0.0); - assert_eq!(time.delta_seconds_f64(), 0.0); + assert_eq!(time.delta_secs(), 0.0); + assert_eq!(time.delta_secs_f64(), 0.0); assert_eq!(time.elapsed(), Duration::ZERO); - assert_eq!(time.elapsed_seconds(), 0.0); - assert_eq!(time.elapsed_seconds_f64(), 0.0); + assert_eq!(time.elapsed_secs(), 0.0); + assert_eq!(time.elapsed_secs_f64(), 0.0); assert_eq!(time.elapsed_wrapped(), Duration::ZERO); - assert_eq!(time.elapsed_seconds_wrapped(), 0.0); - assert_eq!(time.elapsed_seconds_wrapped_f64(), 0.0); + assert_eq!(time.elapsed_secs_wrapped(), 0.0); + assert_eq!(time.elapsed_secs_wrapped_f64(), 0.0); } #[test] @@ -419,29 +419,29 @@ mod test { time.advance_by(Duration::from_millis(250)); assert_eq!(time.delta(), Duration::from_millis(250)); - assert_eq!(time.delta_seconds(), 0.25); - assert_eq!(time.delta_seconds_f64(), 0.25); + assert_eq!(time.delta_secs(), 0.25); + assert_eq!(time.delta_secs_f64(), 0.25); assert_eq!(time.elapsed(), Duration::from_millis(250)); - assert_eq!(time.elapsed_seconds(), 0.25); - assert_eq!(time.elapsed_seconds_f64(), 0.25); + assert_eq!(time.elapsed_secs(), 0.25); + assert_eq!(time.elapsed_secs_f64(), 0.25); time.advance_by(Duration::from_millis(500)); assert_eq!(time.delta(), Duration::from_millis(500)); - assert_eq!(time.delta_seconds(), 0.5); - assert_eq!(time.delta_seconds_f64(), 0.5); + assert_eq!(time.delta_secs(), 0.5); + assert_eq!(time.delta_secs_f64(), 0.5); assert_eq!(time.elapsed(), Duration::from_millis(750)); - assert_eq!(time.elapsed_seconds(), 0.75); - assert_eq!(time.elapsed_seconds_f64(), 0.75); + assert_eq!(time.elapsed_secs(), 0.75); + assert_eq!(time.elapsed_secs_f64(), 0.75); time.advance_by(Duration::ZERO); assert_eq!(time.delta(), Duration::ZERO); - assert_eq!(time.delta_seconds(), 0.0); - assert_eq!(time.delta_seconds_f64(), 0.0); + assert_eq!(time.delta_secs(), 0.0); + assert_eq!(time.delta_secs_f64(), 0.0); assert_eq!(time.elapsed(), Duration::from_millis(750)); - assert_eq!(time.elapsed_seconds(), 0.75); - assert_eq!(time.elapsed_seconds_f64(), 0.75); + assert_eq!(time.elapsed_secs(), 0.75); + assert_eq!(time.elapsed_secs_f64(), 0.75); } #[test] @@ -451,29 +451,29 @@ mod test { time.advance_to(Duration::from_millis(250)); assert_eq!(time.delta(), Duration::from_millis(250)); - assert_eq!(time.delta_seconds(), 0.25); - assert_eq!(time.delta_seconds_f64(), 0.25); + assert_eq!(time.delta_secs(), 0.25); + assert_eq!(time.delta_secs_f64(), 0.25); assert_eq!(time.elapsed(), Duration::from_millis(250)); - assert_eq!(time.elapsed_seconds(), 0.25); - assert_eq!(time.elapsed_seconds_f64(), 0.25); + assert_eq!(time.elapsed_secs(), 0.25); + assert_eq!(time.elapsed_secs_f64(), 0.25); time.advance_to(Duration::from_millis(750)); assert_eq!(time.delta(), Duration::from_millis(500)); - assert_eq!(time.delta_seconds(), 0.5); - assert_eq!(time.delta_seconds_f64(), 0.5); + assert_eq!(time.delta_secs(), 0.5); + assert_eq!(time.delta_secs_f64(), 0.5); assert_eq!(time.elapsed(), Duration::from_millis(750)); - assert_eq!(time.elapsed_seconds(), 0.75); - assert_eq!(time.elapsed_seconds_f64(), 0.75); + assert_eq!(time.elapsed_secs(), 0.75); + assert_eq!(time.elapsed_secs_f64(), 0.75); time.advance_to(Duration::from_millis(750)); assert_eq!(time.delta(), Duration::ZERO); - assert_eq!(time.delta_seconds(), 0.0); - assert_eq!(time.delta_seconds_f64(), 0.0); + assert_eq!(time.delta_secs(), 0.0); + assert_eq!(time.delta_secs_f64(), 0.0); assert_eq!(time.elapsed(), Duration::from_millis(750)); - assert_eq!(time.elapsed_seconds(), 0.75); - assert_eq!(time.elapsed_seconds_f64(), 0.75); + assert_eq!(time.elapsed_secs(), 0.75); + assert_eq!(time.elapsed_secs_f64(), 0.75); } #[test] @@ -494,26 +494,26 @@ mod test { time.advance_by(Duration::from_secs(2)); assert_eq!(time.elapsed_wrapped(), Duration::from_secs(2)); - assert_eq!(time.elapsed_seconds_wrapped(), 2.0); - assert_eq!(time.elapsed_seconds_wrapped_f64(), 2.0); + assert_eq!(time.elapsed_secs_wrapped(), 2.0); + assert_eq!(time.elapsed_secs_wrapped_f64(), 2.0); time.advance_by(Duration::from_secs(2)); assert_eq!(time.elapsed_wrapped(), Duration::from_secs(1)); - assert_eq!(time.elapsed_seconds_wrapped(), 1.0); - assert_eq!(time.elapsed_seconds_wrapped_f64(), 1.0); + assert_eq!(time.elapsed_secs_wrapped(), 1.0); + assert_eq!(time.elapsed_secs_wrapped_f64(), 1.0); time.advance_by(Duration::from_secs(2)); assert_eq!(time.elapsed_wrapped(), Duration::ZERO); - assert_eq!(time.elapsed_seconds_wrapped(), 0.0); - assert_eq!(time.elapsed_seconds_wrapped_f64(), 0.0); + assert_eq!(time.elapsed_secs_wrapped(), 0.0); + assert_eq!(time.elapsed_secs_wrapped_f64(), 0.0); time.advance_by(Duration::new(3, 250_000_000)); assert_eq!(time.elapsed_wrapped(), Duration::from_millis(250)); - assert_eq!(time.elapsed_seconds_wrapped(), 0.25); - assert_eq!(time.elapsed_seconds_wrapped_f64(), 0.25); + assert_eq!(time.elapsed_secs_wrapped(), 0.25); + assert_eq!(time.elapsed_secs_wrapped_f64(), 0.25); } #[test] @@ -524,14 +524,14 @@ mod test { time.advance_by(Duration::from_secs(8)); assert_eq!(time.elapsed_wrapped(), Duration::from_secs(3)); - assert_eq!(time.elapsed_seconds_wrapped(), 3.0); - assert_eq!(time.elapsed_seconds_wrapped_f64(), 3.0); + assert_eq!(time.elapsed_secs_wrapped(), 3.0); + assert_eq!(time.elapsed_secs_wrapped_f64(), 3.0); time.set_wrap_period(Duration::from_secs(2)); assert_eq!(time.elapsed_wrapped(), Duration::from_secs(3)); - assert_eq!(time.elapsed_seconds_wrapped(), 3.0); - assert_eq!(time.elapsed_seconds_wrapped_f64(), 3.0); + assert_eq!(time.elapsed_secs_wrapped(), 3.0); + assert_eq!(time.elapsed_secs_wrapped_f64(), 3.0); time.advance_by(Duration::ZERO); @@ -539,7 +539,7 @@ mod test { // is left in `elapsed_wrapped()`. This test of values is here to ensure // that we notice if we change that behaviour. assert_eq!(time.elapsed_wrapped(), Duration::from_secs(0)); - assert_eq!(time.elapsed_seconds_wrapped(), 0.0); - assert_eq!(time.elapsed_seconds_wrapped_f64(), 0.0); + assert_eq!(time.elapsed_secs_wrapped(), 0.0); + assert_eq!(time.elapsed_secs_wrapped_f64(), 0.0); } } diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index c594a73140..8e88f8c2dd 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -120,6 +120,13 @@ impl Timer { self.stopwatch.elapsed_secs() } + /// Returns the time elapsed on the timer as an `f64`. + /// See also [`Timer::elapsed`](Timer::elapsed). + #[inline] + pub fn elapsed_secs_f64(&self) -> f64 { + self.stopwatch.elapsed_secs_f64() + } + /// Sets the elapsed time of the timer without any other considerations. /// /// See also [`Stopwatch::set`](Stopwatch::set). @@ -299,7 +306,7 @@ impl Timer { /// Returns `true` if the timer is paused. /// - /// See also [`Stopwatch::paused`](Stopwatch::paused). + /// See also [`Stopwatch::is_paused`](Stopwatch::is_paused). /// /// # Examples /// ``` @@ -313,7 +320,7 @@ impl Timer { /// ``` #[inline] pub fn paused(&self) -> bool { - self.stopwatch.paused() + self.stopwatch.is_paused() } /// Resets the timer. The reset doesn't affect the `paused` state of the timer. @@ -450,6 +457,7 @@ mod tests { // Tick once, check all attributes t.tick(Duration::from_secs_f32(0.25)); assert_eq!(t.elapsed_secs(), 0.25); + assert_eq!(t.elapsed_secs_f64(), 0.25); assert_eq!(t.duration(), Duration::from_secs_f32(10.0)); assert!(!t.finished()); assert!(!t.just_finished()); @@ -472,6 +480,7 @@ mod tests { t.unpause(); t.tick(Duration::from_secs_f32(500.0)); assert_eq!(t.elapsed_secs(), 10.0); + assert_eq!(t.elapsed_secs_f64(), 10.0); assert!(t.finished()); assert!(t.just_finished()); assert_eq!(t.times_finished_this_tick(), 1); @@ -480,6 +489,7 @@ mod tests { // Continuing to tick when finished should only change just_finished t.tick(Duration::from_secs_f32(1.0)); assert_eq!(t.elapsed_secs(), 10.0); + assert_eq!(t.elapsed_secs_f64(), 10.0); assert!(t.finished()); assert!(!t.just_finished()); assert_eq!(t.times_finished_this_tick(), 0); @@ -493,6 +503,7 @@ mod tests { // Tick once, check all attributes t.tick(Duration::from_secs_f32(0.75)); assert_eq!(t.elapsed_secs(), 0.75); + assert_eq!(t.elapsed_secs_f64(), 0.75); assert_eq!(t.duration(), Duration::from_secs_f32(2.0)); assert!(!t.finished()); assert!(!t.just_finished()); @@ -503,6 +514,7 @@ mod tests { // Tick past the end and make sure elapsed wraps t.tick(Duration::from_secs_f32(1.5)); assert_eq!(t.elapsed_secs(), 0.25); + assert_eq!(t.elapsed_secs_f64(), 0.25); assert!(t.finished()); assert!(t.just_finished()); assert_eq!(t.times_finished_this_tick(), 1); @@ -511,6 +523,7 @@ mod tests { // Continuing to tick should turn off both finished & just_finished for repeating timers t.tick(Duration::from_secs_f32(1.0)); assert_eq!(t.elapsed_secs(), 1.25); + assert_eq!(t.elapsed_secs_f64(), 1.25); assert!(!t.finished()); assert!(!t.just_finished()); assert_eq!(t.times_finished_this_tick(), 0); @@ -525,6 +538,7 @@ mod tests { t.tick(Duration::from_secs_f32(3.5)); assert_eq!(t.times_finished_this_tick(), 3); assert_eq!(t.elapsed_secs(), 0.5); + assert_eq!(t.elapsed_secs_f64(), 0.5); assert!(t.finished()); assert!(t.just_finished()); t.tick(Duration::from_secs_f32(0.2)); diff --git a/examples/2d/bloom_2d.rs b/examples/2d/bloom_2d.rs index 3ffe6e1181..7613728baa 100644 --- a/examples/2d/bloom_2d.rs +++ b/examples/2d/bloom_2d.rs @@ -112,7 +112,7 @@ fn update_bloom_settings( commands.entity(entity).remove::(); } - let dt = time.delta_seconds(); + let dt = time.delta_secs(); if keycode.pressed(KeyCode::KeyA) { bloom.intensity -= dt / 10.0; diff --git a/examples/2d/bounding_2d.rs b/examples/2d/bounding_2d.rs index b5e9445be0..ff4d371d93 100644 --- a/examples/2d/bounding_2d.rs +++ b/examples/2d/bounding_2d.rs @@ -38,7 +38,7 @@ struct Spin; fn spin(time: Res