Skip to content

Commit

Permalink
Split time functionality into bevy_time (bevyengine#4187)
Browse files Browse the repository at this point in the history
# 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]>
  • Loading branch information
2 people authored and ItsDoot committed Feb 1, 2023
1 parent d15727c commit 71c4365
Show file tree
Hide file tree
Showing 24 changed files with 123 additions and 84 deletions.
1 change: 1 addition & 0 deletions crates/bevy_animation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ bevy_asset = { path = "../bevy_asset", version = "0.8.0-dev" }
bevy_core = { path = "../bevy_core", version = "0.8.0-dev" }
bevy_math = { path = "../bevy_math", version = "0.8.0-dev" }
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
bevy_time = { path = "../bevy_time", version = "0.8.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev" }
bevy_transform = { path = "../bevy_transform", version = "0.8.0-dev" }
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::Deref;

use bevy_app::{App, CoreStage, Plugin};
use bevy_asset::{AddAsset, Assets, Handle};
use bevy_core::{Name, Time};
use bevy_core::Name;
use bevy_ecs::{
change_detection::DetectChanges,
entity::Entity,
Expand All @@ -18,6 +18,7 @@ use bevy_ecs::{
use bevy_hierarchy::{Children, HierarchySystem};
use bevy_math::{Quat, Vec3};
use bevy_reflect::{Reflect, TypeUuid};
use bevy_time::Time;
use bevy_transform::{prelude::Transform, TransformSystem};
use bevy_utils::{tracing::warn, HashMap};

Expand Down
31 changes: 4 additions & 27 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,26 @@
mod name;
mod task_pool_options;
mod time;

pub use bytemuck::{bytes_of, cast_slice, Pod, Zeroable};
pub use name::*;
pub use task_pool_options::*;
pub use time::*;

pub mod prelude {
//! The Bevy Core Prelude.
#[doc(hidden)]
pub use crate::{DefaultTaskPoolOptions, Name, Time, Timer};
pub use crate::{DefaultTaskPoolOptions, Name};
}

use bevy_app::prelude::*;
use bevy_ecs::{
entity::Entity,
schedule::{ExclusiveSystemDescriptorCoercion, SystemLabel},
system::IntoExclusiveSystem,
};
use bevy_ecs::entity::Entity;
use bevy_utils::HashSet;
use std::ops::Range;

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

/// A `SystemLabel` enum for ordering systems relative to core Bevy systems.
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemLabel)]
pub enum CoreSystem {
/// Updates the elapsed time. Any system that interacts with [Time] component should run after
/// this.
Time,
}

impl Plugin for CorePlugin {
fn build(&self, app: &mut App) {
// Setup the default bevy task pools
Expand All @@ -46,20 +32,11 @@ impl Plugin for CorePlugin {
.unwrap_or_default()
.create_default_pools(&mut app.world);

app.init_resource::<Time>()
.init_resource::<FixedTimesteps>()
.register_type::<HashSet<String>>()
app.register_type::<HashSet<String>>()
.register_type::<Option<String>>()
.register_type::<Entity>()
.register_type::<Name>()
.register_type::<Range<f32>>()
.register_type::<Timer>()
// time system is added as an "exclusive system" to ensure it runs before other systems
// in CoreStage::First
.add_system_to_stage(
CoreStage::First,
time_system.exclusive_system().label(CoreSystem::Time),
);
.register_type::<Range<f32>>();

register_rust_types(app);
register_math_types(app);
Expand Down
10 changes: 0 additions & 10 deletions crates/bevy_core/src/time/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/bevy_diagnostic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = ["bevy"]
[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.8.0-dev" }
bevy_core = { path = "../bevy_core", version = "0.8.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev" }
bevy_log = { path = "../bevy_log", version = "0.8.0-dev" }
bevy_time = { path = "../bevy_time", version = "0.8.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_app::prelude::*;
use bevy_core::Time;
use bevy_ecs::system::{Res, ResMut};
use bevy_time::Time;

/// Adds "frame time" diagnostic to an App, specifically "frame time", "fps" and "frame count"
#[derive(Default)]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_diagnostic/src/log_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_app::prelude::*;
use bevy_core::{Time, Timer};
use bevy_ecs::system::{Res, ResMut};
use bevy_log::{debug, info};
use bevy_time::{Time, Timer};
use bevy_utils::Duration;

/// An App Plugin that logs diagnostics to the console
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ bevy_math = { path = "../bevy_math", version = "0.8.0-dev" }
bevy_ptr = { path = "../bevy_ptr", version = "0.8.0-dev" }
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
bevy_scene = { path = "../bevy_scene", version = "0.8.0-dev" }
bevy_time = { path = "../bevy_time", version = "0.8.0-dev" }
bevy_transform = { path = "../bevy_transform", version = "0.8.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
bevy_window = { path = "../bevy_window", version = "0.8.0-dev" }
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_internal/src/default_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy_app::{PluginGroup, PluginGroupBuilder};
/// This plugin group will add all the default plugins:
/// * [`LogPlugin`](bevy_log::LogPlugin)
/// * [`CorePlugin`](bevy_core::CorePlugin)
/// * [`TimePlugin`](bevy_time::TimePlugin)
/// * [`TransformPlugin`](bevy_transform::TransformPlugin)
/// * [`HierarchyPlugin`](bevy_hierarchy::HierarchyPlugin)
/// * [`DiagnosticsPlugin`](bevy_diagnostic::DiagnosticsPlugin)
Expand All @@ -27,6 +28,7 @@ impl PluginGroup for DefaultPlugins {
fn build(&mut self, group: &mut PluginGroupBuilder) {
group.add(bevy_log::LogPlugin::default());
group.add(bevy_core::CorePlugin::default());
group.add(bevy_time::TimePlugin::default());
group.add(bevy_transform::TransformPlugin::default());
group.add(bevy_hierarchy::HierarchyPlugin::default());
group.add(bevy_diagnostic::DiagnosticsPlugin::default());
Expand Down Expand Up @@ -76,6 +78,7 @@ impl PluginGroup for DefaultPlugins {

/// Minimal plugin group that will add the following plugins:
/// * [`CorePlugin`](bevy_core::CorePlugin)
/// * [`TimePlugin`](bevy_time::TimePlugin)
/// * [`ScheduleRunnerPlugin`](bevy_app::ScheduleRunnerPlugin)
///
/// See also [`DefaultPlugins`] for a more complete set of plugins
Expand All @@ -84,6 +87,7 @@ pub struct MinimalPlugins;
impl PluginGroup for MinimalPlugins {
fn build(&mut self, group: &mut PluginGroupBuilder) {
group.add(bevy_core::CorePlugin::default());
group.add(bevy_time::TimePlugin::default());
group.add(bevy_app::ScheduleRunnerPlugin::default());
}
}
7 changes: 6 additions & 1 deletion crates/bevy_internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod asset {
}

pub mod core {
//! Contains core plugins and utilities for time.
//! Contains core plugins.
pub use bevy_core::*;
}

Expand Down Expand Up @@ -70,6 +70,11 @@ pub mod tasks {
pub use bevy_tasks::*;
}

pub mod time {
//! Contains time utilities.
pub use bevy_time::*;
}

pub mod hierarchy {
//! Entity hierarchies and property inheritance
pub use bevy_hierarchy::*;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_internal/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
pub use crate::{
app::prelude::*, asset::prelude::*, core::prelude::*, ecs::prelude::*, hierarchy::prelude::*,
input::prelude::*, log::prelude::*, math::prelude::*, reflect::prelude::*, scene::prelude::*,
transform::prelude::*, utils::prelude::*, window::prelude::*, DefaultPlugins, MinimalPlugins,
time::prelude::*, transform::prelude::*, utils::prelude::*, window::prelude::*, DefaultPlugins,
MinimalPlugins,
};

pub use bevy_derive::{bevy_main, Deref, DerefMut};
Expand Down
17 changes: 17 additions & 0 deletions crates/bevy_time/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "bevy_time"
version = "0.8.0-dev"
edition = "2021"
description = "Provides time functionality for Bevy Engine"
homepage = "https://bevyengine.org"
repository = "https://github.com/bevyengine/bevy"
license = "MIT OR Apache-2.0"
keywords = ["bevy"]


[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.8.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev", features = ["bevy_reflect"] }
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
File renamed without changes.
46 changes: 46 additions & 0 deletions crates/bevy_time/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
mod fixed_timestep;
mod stopwatch;
#[allow(clippy::module_inception)]
mod time;
mod timer;

pub use fixed_timestep::*;
pub use stopwatch::*;
pub use time::*;
pub use timer::*;

pub mod prelude {
//! The Bevy Time Prelude.
#[doc(hidden)]
pub use crate::{Time, Timer};
}

use bevy_app::prelude::*;
use bevy_ecs::prelude::*;

/// Adds time functionality to Apps.
#[derive(Default)]
pub struct TimePlugin;

#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemLabel)]
/// Updates the elapsed time. Any system that interacts with [Time] component should run after
/// this.
pub struct TimeSystem;

impl Plugin for TimePlugin {
fn build(&self, app: &mut App) {
app.init_resource::<Time>()
.init_resource::<FixedTimesteps>()
.register_type::<Timer>()
// time system is added as an "exclusive system" to ensure it runs before other systems
// in CoreStage::First
.add_system_to_stage(
CoreStage::First,
time_system.exclusive_system().at_start().label(TimeSystem),
);
}
}

fn time_system(mut time: ResMut<Time>) {
time.update();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy_utils::Duration;
/// # Examples
///
/// ```
/// # use bevy_core::*;
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut stopwatch = Stopwatch::new();
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Stopwatch {
///
/// # Examples
/// ```
/// # use bevy_core::*;
/// # use bevy_time::*;
/// let stopwatch = Stopwatch::new();
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
/// assert_eq!(stopwatch.paused(), false);
Expand All @@ -49,7 +49,7 @@ impl Stopwatch {
///
/// # Examples
/// ```
/// # use bevy_core::*;
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut stopwatch = Stopwatch::new();
/// stopwatch.tick(Duration::from_secs(1));
Expand All @@ -69,7 +69,7 @@ impl Stopwatch {
///
/// # Examples
/// ```
/// # use bevy_core::*;
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut stopwatch = Stopwatch::new();
/// stopwatch.tick(Duration::from_secs(1));
Expand All @@ -88,7 +88,7 @@ impl Stopwatch {
///
/// # Examples
/// ```
/// # use bevy_core::*;
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut stopwatch = Stopwatch::new();
/// stopwatch.set_elapsed(Duration::from_secs_f32(1.0));
Expand All @@ -105,7 +105,7 @@ impl Stopwatch {
///
/// # Examples
/// ```
/// # use bevy_core::*;
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut stopwatch = Stopwatch::new();
/// stopwatch.tick(Duration::from_secs_f32(1.5));
Expand All @@ -123,7 +123,7 @@ impl Stopwatch {
///
/// # Examples
/// ```
/// # use bevy_core::*;
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut stopwatch = Stopwatch::new();
/// stopwatch.pause();
Expand All @@ -140,7 +140,7 @@ impl Stopwatch {
///
/// # Examples
/// ```
/// # use bevy_core::*;
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut stopwatch = Stopwatch::new();
/// stopwatch.pause();
Expand All @@ -159,7 +159,7 @@ impl Stopwatch {
///
/// # Examples
/// ```
/// # use bevy_core::*;
/// # use bevy_time::*;
/// let mut stopwatch = Stopwatch::new();
/// assert!(!stopwatch.paused());
/// stopwatch.pause();
Expand All @@ -176,7 +176,7 @@ impl Stopwatch {
///
/// # Examples
/// ```
/// # use bevy_core::*;
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut stopwatch = Stopwatch::new();
/// stopwatch.tick(Duration::from_secs_f32(1.5));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bevy_ecs::system::ResMut;
use bevy_utils::{Duration, Instant};

/// Tracks elapsed time since the last update and since the App has started
Expand Down Expand Up @@ -32,7 +31,7 @@ impl Time {
///
/// Calling this method on the [`Time`] resource as part of your app will most likely result in
/// inaccurate timekeeping, as the resource is ordinarily managed by the
/// [`CorePlugin`](crate::CorePlugin).
/// [`TimePlugin`](crate::TimePlugin).
pub fn update(&mut self) {
self.update_with_instant(Instant::now());
}
Expand All @@ -41,12 +40,12 @@ impl Time {
///
/// This method is provided for use in tests. Calling this method on the [`Time`] resource as
/// part of your app will most likely result in inaccurate timekeeping, as the resource is
/// ordinarily managed by the [`CorePlugin`](crate::CorePlugin).
/// ordinarily managed by the [`TimePlugin`](crate::TimePlugin).
///
/// # Examples
///
/// ```
/// # use bevy_core::prelude::*;
/// # use bevy_time::prelude::*;
/// # use bevy_ecs::prelude::*;
/// # use bevy_utils::Duration;
/// # fn main () {
Expand Down Expand Up @@ -143,10 +142,6 @@ impl Time {
}
}

pub(crate) fn time_system(mut time: ResMut<Time>) {
time.update();
}

#[cfg(test)]
#[allow(clippy::float_cmp)]
mod tests {
Expand Down
Loading

0 comments on commit 71c4365

Please sign in to comment.