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
This commit is contained in:
parent
1f99363de9
commit
6f111136b9
@ -1,4 +1,3 @@
|
|||||||
use bevy_ecs::{component::Component, reflect::ReflectComponent};
|
|
||||||
use bevy_reflect::Reflect;
|
use bevy_reflect::Reflect;
|
||||||
use bevy_utils::Duration;
|
use bevy_utils::Duration;
|
||||||
|
|
||||||
@ -23,8 +22,7 @@ use bevy_utils::Duration;
|
|||||||
/// assert!(stopwatch.paused());
|
/// assert!(stopwatch.paused());
|
||||||
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
|
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Component, Clone, Debug, Default, Reflect)]
|
#[derive(Clone, Debug, Default, Reflect)]
|
||||||
#[reflect(Component)]
|
|
||||||
pub struct Stopwatch {
|
pub struct Stopwatch {
|
||||||
elapsed: Duration,
|
elapsed: Duration,
|
||||||
paused: bool,
|
paused: bool,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use crate::Stopwatch;
|
use crate::Stopwatch;
|
||||||
use bevy_ecs::{component::Component, reflect::ReflectComponent};
|
|
||||||
use bevy_reflect::Reflect;
|
use bevy_reflect::Reflect;
|
||||||
use bevy_utils::Duration;
|
use bevy_utils::Duration;
|
||||||
|
|
||||||
@ -10,8 +9,7 @@ use bevy_utils::Duration;
|
|||||||
/// exceeded, and can still be reset at any given point.
|
/// exceeded, and can still be reset at any given point.
|
||||||
///
|
///
|
||||||
/// Paused timers will not have elapsed time increased.
|
/// Paused timers will not have elapsed time increased.
|
||||||
#[derive(Component, Clone, Debug, Default, Reflect)]
|
#[derive(Clone, Debug, Default, Reflect)]
|
||||||
#[reflect(Component)]
|
|
||||||
pub struct Timer {
|
pub struct Timer {
|
||||||
stopwatch: Stopwatch,
|
stopwatch: Stopwatch,
|
||||||
duration: Duration,
|
duration: Duration,
|
||||||
|
@ -15,6 +15,7 @@ fn main() {
|
|||||||
.add_system(move_system)
|
.add_system(move_system)
|
||||||
.add_system(collision_system)
|
.add_system(collision_system)
|
||||||
.add_system(select_system)
|
.add_system(select_system)
|
||||||
|
.insert_resource(SelectTimer(Timer::from_seconds(SHOWCASE_TIMER_SECS, true)))
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,8 +27,7 @@ struct ContributorSelection {
|
|||||||
idx: usize,
|
idx: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
struct SelectTimer(Timer);
|
||||||
struct SelectTimer;
|
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct ContributorDisplay;
|
struct ContributorDisplay;
|
||||||
@ -120,8 +120,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||||||
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
|
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
|
||||||
commands.spawn_bundle(UiCameraBundle::default());
|
commands.spawn_bundle(UiCameraBundle::default());
|
||||||
|
|
||||||
commands.spawn_bundle((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true)));
|
|
||||||
|
|
||||||
commands
|
commands
|
||||||
.spawn()
|
.spawn()
|
||||||
.insert(ContributorDisplay)
|
.insert(ContributorDisplay)
|
||||||
@ -157,22 +155,13 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||||||
|
|
||||||
/// Finds the next contributor to display and selects the entity
|
/// Finds the next contributor to display and selects the entity
|
||||||
fn select_system(
|
fn select_system(
|
||||||
|
mut timer: ResMut<SelectTimer>,
|
||||||
mut contributor_selection: ResMut<ContributorSelection>,
|
mut contributor_selection: ResMut<ContributorSelection>,
|
||||||
mut text_query: Query<&mut Text, With<ContributorDisplay>>,
|
mut text_query: Query<&mut Text, With<ContributorDisplay>>,
|
||||||
mut timer_query: Query<&mut Timer, With<SelectTimer>>,
|
|
||||||
mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>,
|
mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
let mut timer_fired = false;
|
if !timer.0.tick(time.delta()).just_finished() {
|
||||||
for mut timer in timer_query.iter_mut() {
|
|
||||||
if !timer.tick(time.delta()).just_finished() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
timer.reset();
|
|
||||||
timer_fired = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if !timer_fired {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,8 +17,8 @@ fn main() {
|
|||||||
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.add_startup_system(setup)
|
.add_startup_system(setup)
|
||||||
.add_system(tick_system.label("Tick"))
|
.add_system(print_sprite_count.label("Tick"))
|
||||||
.add_system(move_camera_system.after("Tick"))
|
.add_system(move_camera.after("Tick"))
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +37,6 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
|
|||||||
commands
|
commands
|
||||||
.spawn()
|
.spawn()
|
||||||
.insert_bundle(OrthographicCameraBundle::new_2d())
|
.insert_bundle(OrthographicCameraBundle::new_2d())
|
||||||
.insert(Timer::from_seconds(1.0, true))
|
|
||||||
.insert(Transform::from_xyz(0.0, 0.0, 1000.0));
|
.insert(Transform::from_xyz(0.0, 0.0, 1000.0));
|
||||||
|
|
||||||
// Builds and spawns the sprites
|
// Builds and spawns the sprites
|
||||||
@ -68,19 +67,26 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// System for rotating and translating the camera
|
// System for rotating and translating the camera
|
||||||
fn move_camera_system(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
|
fn move_camera(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
|
||||||
let mut camera_transform = camera_query.single_mut();
|
let mut camera_transform = camera_query.single_mut();
|
||||||
camera_transform.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
|
camera_transform.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
|
||||||
*camera_transform = *camera_transform
|
*camera_transform = *camera_transform
|
||||||
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
|
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
// System for printing the number of sprites on every tick of the timer
|
struct PrintingTimer(Timer);
|
||||||
fn tick_system(time: Res<Time>, sprites_query: Query<&Sprite>, mut timer_query: Query<&mut Timer>) {
|
|
||||||
let mut timer = timer_query.single_mut();
|
|
||||||
timer.tick(time.delta());
|
|
||||||
|
|
||||||
if timer.just_finished() {
|
impl Default for PrintingTimer {
|
||||||
info!("Sprites: {}", sprites_query.iter().count(),);
|
fn default() -> Self {
|
||||||
|
Self(Timer::from_seconds(1.0, true))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// System for printing the number of sprites on every tick of the timer
|
||||||
|
fn print_sprite_count(time: Res<Time>, mut timer: Local<PrintingTimer>, sprites: Query<&Sprite>) {
|
||||||
|
timer.0.tick(time.delta());
|
||||||
|
|
||||||
|
if timer.0.just_finished() {
|
||||||
|
info!("Sprites: {}", sprites.iter().count(),);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,18 +4,25 @@ fn main() {
|
|||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.add_startup_system(setup)
|
.add_startup_system(setup)
|
||||||
.add_system(animate_sprite_system)
|
.add_system(animate_sprite)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn animate_sprite_system(
|
#[derive(Component)]
|
||||||
|
struct AnimationTimer(Timer);
|
||||||
|
|
||||||
|
fn animate_sprite(
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
texture_atlases: Res<Assets<TextureAtlas>>,
|
texture_atlases: Res<Assets<TextureAtlas>>,
|
||||||
mut query: Query<(&mut Timer, &mut TextureAtlasSprite, &Handle<TextureAtlas>)>,
|
mut query: Query<(
|
||||||
|
&mut AnimationTimer,
|
||||||
|
&mut TextureAtlasSprite,
|
||||||
|
&Handle<TextureAtlas>,
|
||||||
|
)>,
|
||||||
) {
|
) {
|
||||||
for (mut timer, mut sprite, texture_atlas_handle) in query.iter_mut() {
|
for (mut timer, mut sprite, texture_atlas_handle) in query.iter_mut() {
|
||||||
timer.tick(time.delta());
|
timer.0.tick(time.delta());
|
||||||
if timer.finished() {
|
if timer.0.just_finished() {
|
||||||
let texture_atlas = texture_atlases.get(texture_atlas_handle).unwrap();
|
let texture_atlas = texture_atlases.get(texture_atlas_handle).unwrap();
|
||||||
sprite.index = (sprite.index + 1) % texture_atlas.textures.len();
|
sprite.index = (sprite.index + 1) % texture_atlas.textures.len();
|
||||||
}
|
}
|
||||||
@ -37,5 +44,5 @@ fn setup(
|
|||||||
transform: Transform::from_scale(Vec3::splat(6.0)),
|
transform: Transform::from_scale(Vec3::splat(6.0)),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.insert(Timer::from_seconds(0.1, true));
|
.insert(AnimationTimer(Timer::from_seconds(0.1, true)));
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,15 @@ fn main() {
|
|||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.init_resource::<Countdown>()
|
.init_resource::<Countdown>()
|
||||||
.add_startup_system(setup_system)
|
.add_startup_system(setup)
|
||||||
.add_system(countdown_system)
|
.add_system(countdown)
|
||||||
.add_system(timer_system)
|
.add_system(print_when_completed)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct PrintOnCompletionTimer(Timer);
|
||||||
|
|
||||||
pub struct Countdown {
|
pub struct Countdown {
|
||||||
pub percent_trigger: Timer,
|
pub percent_trigger: Timer,
|
||||||
pub main_timer: Timer,
|
pub main_timer: Timer,
|
||||||
@ -30,16 +33,18 @@ impl Default for Countdown {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup_system(mut commands: Commands) {
|
fn setup(mut commands: Commands) {
|
||||||
// Add an entity to the world with a timer
|
// Add an entity to the world with a timer
|
||||||
commands.spawn().insert(Timer::from_seconds(5.0, false));
|
commands
|
||||||
|
.spawn()
|
||||||
|
.insert(PrintOnCompletionTimer(Timer::from_seconds(5.0, false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This system ticks all the `Timer` components on entities within the scene
|
/// This system ticks all the `Timer` components on entities within the scene
|
||||||
/// using bevy's `Time` resource to get the delta between each update.
|
/// using bevy's `Time` resource to get the delta between each update.
|
||||||
fn timer_system(time: Res<Time>, mut query: Query<&mut Timer>) {
|
fn print_when_completed(time: Res<Time>, mut query: Query<&mut PrintOnCompletionTimer>) {
|
||||||
for mut timer in query.iter_mut() {
|
for mut timer in query.iter_mut() {
|
||||||
if timer.tick(time.delta()).just_finished() {
|
if timer.0.tick(time.delta()).just_finished() {
|
||||||
info!("Entity timer just finished")
|
info!("Entity timer just finished")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,7 +52,7 @@ fn timer_system(time: Res<Time>, mut query: Query<&mut Timer>) {
|
|||||||
|
|
||||||
/// This system controls ticking the timer within the countdown resource and
|
/// This system controls ticking the timer within the countdown resource and
|
||||||
/// handling its state.
|
/// handling its state.
|
||||||
fn countdown_system(time: Res<Time>, mut countdown: ResMut<Countdown>) {
|
fn countdown(time: Res<Time>, mut countdown: ResMut<Countdown>) {
|
||||||
countdown.main_timer.tick(time.delta());
|
countdown.main_timer.tick(time.delta());
|
||||||
|
|
||||||
// The API encourages this kind of timer state checking (if you're only checking for one value)
|
// The API encourages this kind of timer state checking (if you're only checking for one value)
|
||||||
|
Loading…
Reference in New Issue
Block a user