Skip to content

Commit 71c4365

Browse files
CAD97cart
authored andcommitted
Split time functionality into bevy_time (bevyengine#4187)
# Objective Reduce the catch-all grab-bag of functionality in bevy_core by minimally splitting off time functionality into bevy_time. Functionality like that provided by bevyengine#3002 would increase the complexity of bevy_time, so this is a good candidate for pulling into its own unit. A step in addressing bevyengine#2931 and splitting bevy_core into more specific locations. ## Solution Pull the time module of bevy_core into a new crate, bevy_time. # Migration guide - Time related types (e.g. `Time`, `Timer`, `Stopwatch`, `FixedTimestep`, etc.) should be imported from `bevy::time::*` rather than `bevy::core::*`. - If you were adding `CorePlugin` manually, you'll also want to add `TimePlugin` from `bevy::time`. - The `bevy::core::CorePlugin::Time` system label is replaced with `bevy::time::TimeSystem`. Co-authored-by: Carter Anderson <[email protected]>
1 parent d15727c commit 71c4365

24 files changed

+123
-84
lines changed

crates/bevy_animation/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bevy_asset = { path = "../bevy_asset", version = "0.8.0-dev" }
1515
bevy_core = { path = "../bevy_core", version = "0.8.0-dev" }
1616
bevy_math = { path = "../bevy_math", version = "0.8.0-dev" }
1717
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
18+
bevy_time = { path = "../bevy_time", version = "0.8.0-dev" }
1819
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
1920
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev" }
2021
bevy_transform = { path = "../bevy_transform", version = "0.8.0-dev" }

crates/bevy_animation/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::ops::Deref;
66

77
use bevy_app::{App, CoreStage, Plugin};
88
use bevy_asset::{AddAsset, Assets, Handle};
9-
use bevy_core::{Name, Time};
9+
use bevy_core::Name;
1010
use bevy_ecs::{
1111
change_detection::DetectChanges,
1212
entity::Entity,
@@ -18,6 +18,7 @@ use bevy_ecs::{
1818
use bevy_hierarchy::{Children, HierarchySystem};
1919
use bevy_math::{Quat, Vec3};
2020
use bevy_reflect::{Reflect, TypeUuid};
21+
use bevy_time::Time;
2122
use bevy_transform::{prelude::Transform, TransformSystem};
2223
use bevy_utils::{tracing::warn, HashMap};
2324

crates/bevy_core/src/lib.rs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,26 @@
33
44
mod name;
55
mod task_pool_options;
6-
mod time;
76

87
pub use bytemuck::{bytes_of, cast_slice, Pod, Zeroable};
98
pub use name::*;
109
pub use task_pool_options::*;
11-
pub use time::*;
1210

1311
pub mod prelude {
1412
//! The Bevy Core Prelude.
1513
#[doc(hidden)]
16-
pub use crate::{DefaultTaskPoolOptions, Name, Time, Timer};
14+
pub use crate::{DefaultTaskPoolOptions, Name};
1715
}
1816

1917
use bevy_app::prelude::*;
20-
use bevy_ecs::{
21-
entity::Entity,
22-
schedule::{ExclusiveSystemDescriptorCoercion, SystemLabel},
23-
system::IntoExclusiveSystem,
24-
};
18+
use bevy_ecs::entity::Entity;
2519
use bevy_utils::HashSet;
2620
use std::ops::Range;
2721

2822
/// Adds core functionality to Apps.
2923
#[derive(Default)]
3024
pub struct CorePlugin;
3125

32-
/// A `SystemLabel` enum for ordering systems relative to core Bevy systems.
33-
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemLabel)]
34-
pub enum CoreSystem {
35-
/// Updates the elapsed time. Any system that interacts with [Time] component should run after
36-
/// this.
37-
Time,
38-
}
39-
4026
impl Plugin for CorePlugin {
4127
fn build(&self, app: &mut App) {
4228
// Setup the default bevy task pools
@@ -46,20 +32,11 @@ impl Plugin for CorePlugin {
4632
.unwrap_or_default()
4733
.create_default_pools(&mut app.world);
4834

49-
app.init_resource::<Time>()
50-
.init_resource::<FixedTimesteps>()
51-
.register_type::<HashSet<String>>()
35+
app.register_type::<HashSet<String>>()
5236
.register_type::<Option<String>>()
5337
.register_type::<Entity>()
5438
.register_type::<Name>()
55-
.register_type::<Range<f32>>()
56-
.register_type::<Timer>()
57-
// time system is added as an "exclusive system" to ensure it runs before other systems
58-
// in CoreStage::First
59-
.add_system_to_stage(
60-
CoreStage::First,
61-
time_system.exclusive_system().label(CoreSystem::Time),
62-
);
39+
.register_type::<Range<f32>>();
6340

6441
register_rust_types(app);
6542
register_math_types(app);

crates/bevy_core/src/time/mod.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

crates/bevy_diagnostic/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ keywords = ["bevy"]
1212
[dependencies]
1313
# bevy
1414
bevy_app = { path = "../bevy_app", version = "0.8.0-dev" }
15-
bevy_core = { path = "../bevy_core", version = "0.8.0-dev" }
1615
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev" }
1716
bevy_log = { path = "../bevy_log", version = "0.8.0-dev" }
17+
bevy_time = { path = "../bevy_time", version = "0.8.0-dev" }
1818
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }

crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{Diagnostic, DiagnosticId, Diagnostics};
22
use bevy_app::prelude::*;
3-
use bevy_core::Time;
43
use bevy_ecs::system::{Res, ResMut};
4+
use bevy_time::Time;
55

66
/// Adds "frame time" diagnostic to an App, specifically "frame time", "fps" and "frame count"
77
#[derive(Default)]

crates/bevy_diagnostic/src/log_diagnostics_plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::{Diagnostic, DiagnosticId, Diagnostics};
22
use bevy_app::prelude::*;
3-
use bevy_core::{Time, Timer};
43
use bevy_ecs::system::{Res, ResMut};
54
use bevy_log::{debug, info};
5+
use bevy_time::{Time, Timer};
66
use bevy_utils::Duration;
77

88
/// An App Plugin that logs diagnostics to the console

crates/bevy_internal/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ bevy_math = { path = "../bevy_math", version = "0.8.0-dev" }
7878
bevy_ptr = { path = "../bevy_ptr", version = "0.8.0-dev" }
7979
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
8080
bevy_scene = { path = "../bevy_scene", version = "0.8.0-dev" }
81+
bevy_time = { path = "../bevy_time", version = "0.8.0-dev" }
8182
bevy_transform = { path = "../bevy_transform", version = "0.8.0-dev" }
8283
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
8384
bevy_window = { path = "../bevy_window", version = "0.8.0-dev" }

crates/bevy_internal/src/default_plugins.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use bevy_app::{PluginGroup, PluginGroupBuilder};
33
/// This plugin group will add all the default plugins:
44
/// * [`LogPlugin`](bevy_log::LogPlugin)
55
/// * [`CorePlugin`](bevy_core::CorePlugin)
6+
/// * [`TimePlugin`](bevy_time::TimePlugin)
67
/// * [`TransformPlugin`](bevy_transform::TransformPlugin)
78
/// * [`HierarchyPlugin`](bevy_hierarchy::HierarchyPlugin)
89
/// * [`DiagnosticsPlugin`](bevy_diagnostic::DiagnosticsPlugin)
@@ -27,6 +28,7 @@ impl PluginGroup for DefaultPlugins {
2728
fn build(&mut self, group: &mut PluginGroupBuilder) {
2829
group.add(bevy_log::LogPlugin::default());
2930
group.add(bevy_core::CorePlugin::default());
31+
group.add(bevy_time::TimePlugin::default());
3032
group.add(bevy_transform::TransformPlugin::default());
3133
group.add(bevy_hierarchy::HierarchyPlugin::default());
3234
group.add(bevy_diagnostic::DiagnosticsPlugin::default());
@@ -76,6 +78,7 @@ impl PluginGroup for DefaultPlugins {
7678

7779
/// Minimal plugin group that will add the following plugins:
7880
/// * [`CorePlugin`](bevy_core::CorePlugin)
81+
/// * [`TimePlugin`](bevy_time::TimePlugin)
7982
/// * [`ScheduleRunnerPlugin`](bevy_app::ScheduleRunnerPlugin)
8083
///
8184
/// See also [`DefaultPlugins`] for a more complete set of plugins
@@ -84,6 +87,7 @@ pub struct MinimalPlugins;
8487
impl PluginGroup for MinimalPlugins {
8588
fn build(&mut self, group: &mut PluginGroupBuilder) {
8689
group.add(bevy_core::CorePlugin::default());
90+
group.add(bevy_time::TimePlugin::default());
8791
group.add(bevy_app::ScheduleRunnerPlugin::default());
8892
}
8993
}

crates/bevy_internal/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod asset {
1818
}
1919

2020
pub mod core {
21-
//! Contains core plugins and utilities for time.
21+
//! Contains core plugins.
2222
pub use bevy_core::*;
2323
}
2424

@@ -70,6 +70,11 @@ pub mod tasks {
7070
pub use bevy_tasks::*;
7171
}
7272

73+
pub mod time {
74+
//! Contains time utilities.
75+
pub use bevy_time::*;
76+
}
77+
7378
pub mod hierarchy {
7479
//! Entity hierarchies and property inheritance
7580
pub use bevy_hierarchy::*;

crates/bevy_internal/src/prelude.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
pub use crate::{
33
app::prelude::*, asset::prelude::*, core::prelude::*, ecs::prelude::*, hierarchy::prelude::*,
44
input::prelude::*, log::prelude::*, math::prelude::*, reflect::prelude::*, scene::prelude::*,
5-
transform::prelude::*, utils::prelude::*, window::prelude::*, DefaultPlugins, MinimalPlugins,
5+
time::prelude::*, transform::prelude::*, utils::prelude::*, window::prelude::*, DefaultPlugins,
6+
MinimalPlugins,
67
};
78

89
pub use bevy_derive::{bevy_main, Deref, DerefMut};

crates/bevy_time/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "bevy_time"
3+
version = "0.8.0-dev"
4+
edition = "2021"
5+
description = "Provides time functionality for Bevy Engine"
6+
homepage = "https://bevyengine.org"
7+
repository = "https://github.com/bevyengine/bevy"
8+
license = "MIT OR Apache-2.0"
9+
keywords = ["bevy"]
10+
11+
12+
[dependencies]
13+
# bevy
14+
bevy_app = { path = "../bevy_app", version = "0.8.0-dev" }
15+
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev", features = ["bevy_reflect"] }
16+
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
17+
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }

crates/bevy_time/src/lib.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
mod fixed_timestep;
2+
mod stopwatch;
3+
#[allow(clippy::module_inception)]
4+
mod time;
5+
mod timer;
6+
7+
pub use fixed_timestep::*;
8+
pub use stopwatch::*;
9+
pub use time::*;
10+
pub use timer::*;
11+
12+
pub mod prelude {
13+
//! The Bevy Time Prelude.
14+
#[doc(hidden)]
15+
pub use crate::{Time, Timer};
16+
}
17+
18+
use bevy_app::prelude::*;
19+
use bevy_ecs::prelude::*;
20+
21+
/// Adds time functionality to Apps.
22+
#[derive(Default)]
23+
pub struct TimePlugin;
24+
25+
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemLabel)]
26+
/// Updates the elapsed time. Any system that interacts with [Time] component should run after
27+
/// this.
28+
pub struct TimeSystem;
29+
30+
impl Plugin for TimePlugin {
31+
fn build(&self, app: &mut App) {
32+
app.init_resource::<Time>()
33+
.init_resource::<FixedTimesteps>()
34+
.register_type::<Timer>()
35+
// time system is added as an "exclusive system" to ensure it runs before other systems
36+
// in CoreStage::First
37+
.add_system_to_stage(
38+
CoreStage::First,
39+
time_system.exclusive_system().at_start().label(TimeSystem),
40+
);
41+
}
42+
}
43+
44+
fn time_system(mut time: ResMut<Time>) {
45+
time.update();
46+
}

crates/bevy_core/src/time/stopwatch.rs renamed to crates/bevy_time/src/stopwatch.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bevy_utils::Duration;
77
/// # Examples
88
///
99
/// ```
10-
/// # use bevy_core::*;
10+
/// # use bevy_time::*;
1111
/// use std::time::Duration;
1212
/// let mut stopwatch = Stopwatch::new();
1313
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
@@ -35,7 +35,7 @@ impl Stopwatch {
3535
///
3636
/// # Examples
3737
/// ```
38-
/// # use bevy_core::*;
38+
/// # use bevy_time::*;
3939
/// let stopwatch = Stopwatch::new();
4040
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
4141
/// assert_eq!(stopwatch.paused(), false);
@@ -49,7 +49,7 @@ impl Stopwatch {
4949
///
5050
/// # Examples
5151
/// ```
52-
/// # use bevy_core::*;
52+
/// # use bevy_time::*;
5353
/// use std::time::Duration;
5454
/// let mut stopwatch = Stopwatch::new();
5555
/// stopwatch.tick(Duration::from_secs(1));
@@ -69,7 +69,7 @@ impl Stopwatch {
6969
///
7070
/// # Examples
7171
/// ```
72-
/// # use bevy_core::*;
72+
/// # use bevy_time::*;
7373
/// use std::time::Duration;
7474
/// let mut stopwatch = Stopwatch::new();
7575
/// stopwatch.tick(Duration::from_secs(1));
@@ -88,7 +88,7 @@ impl Stopwatch {
8888
///
8989
/// # Examples
9090
/// ```
91-
/// # use bevy_core::*;
91+
/// # use bevy_time::*;
9292
/// use std::time::Duration;
9393
/// let mut stopwatch = Stopwatch::new();
9494
/// stopwatch.set_elapsed(Duration::from_secs_f32(1.0));
@@ -105,7 +105,7 @@ impl Stopwatch {
105105
///
106106
/// # Examples
107107
/// ```
108-
/// # use bevy_core::*;
108+
/// # use bevy_time::*;
109109
/// use std::time::Duration;
110110
/// let mut stopwatch = Stopwatch::new();
111111
/// stopwatch.tick(Duration::from_secs_f32(1.5));
@@ -123,7 +123,7 @@ impl Stopwatch {
123123
///
124124
/// # Examples
125125
/// ```
126-
/// # use bevy_core::*;
126+
/// # use bevy_time::*;
127127
/// use std::time::Duration;
128128
/// let mut stopwatch = Stopwatch::new();
129129
/// stopwatch.pause();
@@ -140,7 +140,7 @@ impl Stopwatch {
140140
///
141141
/// # Examples
142142
/// ```
143-
/// # use bevy_core::*;
143+
/// # use bevy_time::*;
144144
/// use std::time::Duration;
145145
/// let mut stopwatch = Stopwatch::new();
146146
/// stopwatch.pause();
@@ -159,7 +159,7 @@ impl Stopwatch {
159159
///
160160
/// # Examples
161161
/// ```
162-
/// # use bevy_core::*;
162+
/// # use bevy_time::*;
163163
/// let mut stopwatch = Stopwatch::new();
164164
/// assert!(!stopwatch.paused());
165165
/// stopwatch.pause();
@@ -176,7 +176,7 @@ impl Stopwatch {
176176
///
177177
/// # Examples
178178
/// ```
179-
/// # use bevy_core::*;
179+
/// # use bevy_time::*;
180180
/// use std::time::Duration;
181181
/// let mut stopwatch = Stopwatch::new();
182182
/// stopwatch.tick(Duration::from_secs_f32(1.5));

crates/bevy_core/src/time/time.rs renamed to crates/bevy_time/src/time.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use bevy_ecs::system::ResMut;
21
use bevy_utils::{Duration, Instant};
32

43
/// Tracks elapsed time since the last update and since the App has started
@@ -32,7 +31,7 @@ impl Time {
3231
///
3332
/// Calling this method on the [`Time`] resource as part of your app will most likely result in
3433
/// inaccurate timekeeping, as the resource is ordinarily managed by the
35-
/// [`CorePlugin`](crate::CorePlugin).
34+
/// [`TimePlugin`](crate::TimePlugin).
3635
pub fn update(&mut self) {
3736
self.update_with_instant(Instant::now());
3837
}
@@ -41,12 +40,12 @@ impl Time {
4140
///
4241
/// This method is provided for use in tests. Calling this method on the [`Time`] resource as
4342
/// part of your app will most likely result in inaccurate timekeeping, as the resource is
44-
/// ordinarily managed by the [`CorePlugin`](crate::CorePlugin).
43+
/// ordinarily managed by the [`TimePlugin`](crate::TimePlugin).
4544
///
4645
/// # Examples
4746
///
4847
/// ```
49-
/// # use bevy_core::prelude::*;
48+
/// # use bevy_time::prelude::*;
5049
/// # use bevy_ecs::prelude::*;
5150
/// # use bevy_utils::Duration;
5251
/// # fn main () {
@@ -143,10 +142,6 @@ impl Time {
143142
}
144143
}
145144

146-
pub(crate) fn time_system(mut time: ResMut<Time>) {
147-
time.update();
148-
}
149-
150145
#[cfg(test)]
151146
#[allow(clippy::float_cmp)]
152147
mod tests {

0 commit comments

Comments
 (0)