Skip to content

Commit 52d57fa

Browse files
committed
More idiomatic timer handling
Signed-off-by: Alex Saveau <[email protected]>
1 parent bf008cb commit 52d57fa

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

examples/2d/contributors.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ struct ContributorSelection {
2828
idx: usize,
2929
}
3030

31-
#[derive(Component)]
32-
struct SelectTimer;
31+
struct SelectTimerState {
32+
timer: Timer,
33+
has_triggered: bool,
34+
}
3335

3436
#[derive(Component)]
3537
struct ContributorDisplay;
@@ -122,7 +124,10 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
122124
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
123125
commands.spawn_bundle(UiCameraBundle::default());
124126

125-
commands.spawn_bundle((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true)));
127+
commands.insert_resource(SelectTimerState {
128+
timer: Timer::from_seconds(SHOWCASE_TIMER_SECS, true),
129+
has_triggered: false,
130+
});
126131

127132
commands
128133
.spawn()
@@ -161,21 +166,19 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
161166
fn select_system(
162167
mut contributor_selection: ResMut<ContributorSelection>,
163168
mut text_query: Query<&mut Text, With<ContributorDisplay>>,
164-
mut timer_query: Query<&mut Timer, With<SelectTimer>>,
165169
mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>,
170+
mut timer_state: ResMut<SelectTimerState>,
166171
time: Res<Time>,
167-
mut has_first_tick_occurred: Local<bool>,
168172
) {
169-
let mut timer = timer_query.single_mut();
170-
if !timer.tick(time.delta()).just_finished() {
173+
if !timer_state.timer.tick(time.delta()).just_finished() {
171174
return;
172175
}
173-
if !*has_first_tick_occurred {
176+
if !timer_state.has_triggered {
174177
let mut text = text_query.single_mut();
175178
text.sections[0].value.clear();
176179
text.sections[0].value.push_str("Contributor: ");
177180

178-
*has_first_tick_occurred = true;
181+
timer_state.has_triggered = true;
179182
}
180183

181184
{

0 commit comments

Comments
 (0)