From 6f111136b9f2ebd5ab0975389f4c810242d1645d Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Thu, 3 Feb 2022 23:56:57 +0000 Subject: [PATCH] Cleanup some things which shouldn't be components (#2982) # Objective - Using `Stopwatch` and `Timer` as raw components is a footgun. ## Solution - Stop them from being components --- crates/bevy_core/src/time/stopwatch.rs | 4 +--- crates/bevy_core/src/time/timer.rs | 4 +--- examples/2d/contributors.rs | 19 ++++--------------- examples/2d/many_sprites.rs | 24 +++++++++++++++--------- examples/2d/sprite_sheet.rs | 19 +++++++++++++------ examples/ecs/timers.rs | 21 +++++++++++++-------- 6 files changed, 47 insertions(+), 44 deletions(-) diff --git a/crates/bevy_core/src/time/stopwatch.rs b/crates/bevy_core/src/time/stopwatch.rs index 9fa84cba07a6b..c13f2773918a8 100644 --- a/crates/bevy_core/src/time/stopwatch.rs +++ b/crates/bevy_core/src/time/stopwatch.rs @@ -1,4 +1,3 @@ -use bevy_ecs::{component::Component, reflect::ReflectComponent}; use bevy_reflect::Reflect; use bevy_utils::Duration; @@ -23,8 +22,7 @@ use bevy_utils::Duration; /// assert!(stopwatch.paused()); /// assert_eq!(stopwatch.elapsed_secs(), 0.0); /// ``` -#[derive(Component, Clone, Debug, Default, Reflect)] -#[reflect(Component)] +#[derive(Clone, Debug, Default, Reflect)] pub struct Stopwatch { elapsed: Duration, paused: bool, diff --git a/crates/bevy_core/src/time/timer.rs b/crates/bevy_core/src/time/timer.rs index fb01966901104..6cc4fc9d9817f 100644 --- a/crates/bevy_core/src/time/timer.rs +++ b/crates/bevy_core/src/time/timer.rs @@ -1,5 +1,4 @@ use crate::Stopwatch; -use bevy_ecs::{component::Component, reflect::ReflectComponent}; use bevy_reflect::Reflect; use bevy_utils::Duration; @@ -10,8 +9,7 @@ use bevy_utils::Duration; /// exceeded, and can still be reset at any given point. /// /// Paused timers will not have elapsed time increased. -#[derive(Component, Clone, Debug, Default, Reflect)] -#[reflect(Component)] +#[derive(Clone, Debug, Default, Reflect)] pub struct Timer { stopwatch: Stopwatch, duration: Duration, diff --git a/examples/2d/contributors.rs b/examples/2d/contributors.rs index cefce614588a7..cc5bd3c42c34a 100644 --- a/examples/2d/contributors.rs +++ b/examples/2d/contributors.rs @@ -15,6 +15,7 @@ fn main() { .add_system(move_system) .add_system(collision_system) .add_system(select_system) + .insert_resource(SelectTimer(Timer::from_seconds(SHOWCASE_TIMER_SECS, true))) .run(); } @@ -26,8 +27,7 @@ struct ContributorSelection { idx: usize, } -#[derive(Component)] -struct SelectTimer; +struct SelectTimer(Timer); #[derive(Component)] struct ContributorDisplay; @@ -120,8 +120,6 @@ fn setup(mut commands: Commands, asset_server: Res) { commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands.spawn_bundle(UiCameraBundle::default()); - commands.spawn_bundle((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true))); - commands .spawn() .insert(ContributorDisplay) @@ -157,22 +155,13 @@ fn setup(mut commands: Commands, asset_server: Res) { /// Finds the next contributor to display and selects the entity fn select_system( + mut timer: ResMut, mut contributor_selection: ResMut, mut text_query: Query<&mut Text, With>, - mut timer_query: Query<&mut Timer, With>, mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>, time: Res