Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roll over any extra frame time in timers #165

Merged
merged 1 commit into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions crates/bevy_core/src/time/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,53 @@ use bevy_property::Properties;
use std::time::Duration;

/// Tracks elapsed time. Enters the finished state once `duration` is reached.
///
/// Non repeating timers will stop tracking and stay in the finished state until reset.
/// Repeating timers will only be in the finished state on each tick `duration` is reached or exceeded, and can still be reset at any given point.
#[derive(Clone, Debug, Default, Properties)]
pub struct Timer {
pub elapsed: f32,
pub duration: f32,
pub finished: bool,
/// Will only be true on the tick `duration` is reached or exceeded.
pub just_finished: bool,
pub repeating: bool,
}

impl Timer {
pub fn new(duration: Duration) -> Self {
pub fn new(duration: Duration, repeating: bool) -> Self {
Timer {
duration: duration.as_secs_f32(),
repeating,
..Default::default()
}
}

pub fn from_seconds(seconds: f32) -> Self {
pub fn from_seconds(seconds: f32, repeating: bool) -> Self {
Timer {
duration: seconds,
repeating,
..Default::default()
}
}

pub fn tick(&mut self, delta: f32) {
self.elapsed = (self.elapsed + delta).min(self.duration);
if self.elapsed >= self.duration {
self.finished = true;
let prev_finished = self.elapsed >= self.duration;
if !prev_finished {
self.elapsed += delta;
}

self.finished = self.elapsed >= self.duration;
self.just_finished = !prev_finished && self.finished;

if self.repeating && self.finished {
self.elapsed %= self.duration;
}
}

pub fn reset(&mut self) {
self.finished = false;
self.just_finished = false;
self.elapsed = 0.0;
}
}
Expand Down
6 changes: 1 addition & 5 deletions crates/bevy_diagnostic/src/print_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Default for PrintDiagnosticsPlugin {
impl Plugin for PrintDiagnosticsPlugin {
fn build(&self, app: &mut bevy_app::AppBuilder) {
app.add_resource(PrintDiagnosticsState {
timer: Timer::new(self.wait_duration),
timer: Timer::new(self.wait_duration, true),
filter: self.filter.clone(),
});

Expand Down Expand Up @@ -82,8 +82,6 @@ impl PrintDiagnosticsPlugin {
Self::print_diagnostic(diagnostic);
}
}

state.timer.reset();
}
}

Expand All @@ -105,8 +103,6 @@ impl PrintDiagnosticsPlugin {
println!("{:#?}\n", diagnostic);
}
}

state.timer.reset();
}
}
}
5 changes: 2 additions & 3 deletions examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ fn animate_sprite_system(
texture_atlases: Res<Assets<TextureAtlas>>,
mut query: Query<(&mut Timer, &mut TextureAtlasSprite, &Handle<TextureAtlas>)>,
) {
for (mut timer, mut sprite, texture_atlas_handle) in &mut query.iter() {
for (timer, mut sprite, texture_atlas_handle) in &mut query.iter() {
if timer.finished {
let texture_atlas = texture_atlases.get(&texture_atlas_handle).unwrap();
sprite.index = ((sprite.index as usize + 1) % texture_atlas.textures.len()) as u32;
timer.reset();
}
}
}
Expand All @@ -43,5 +42,5 @@ fn setup(
scale: Scale(6.0),
..Default::default()
})
.with(Timer::from_seconds(0.1));
.with(Timer::from_seconds(0.1, true));
}
3 changes: 1 addition & 2 deletions examples/app/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Plugin for PrintMessagePlugin {
fn build(&self, app: &mut AppBuilder) {
let state = PrintMessageState {
message: self.message.clone(),
timer: Timer::new(self.wait_duration),
timer: Timer::new(self.wait_duration, true),
};
app.add_resource(state)
.add_system(print_message_system.system());
Expand All @@ -43,6 +43,5 @@ fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
state.timer.tick(time.delta_seconds);
if state.timer.finished {
println!("{}", state.message);
state.timer.reset();
}
}
4 changes: 1 addition & 3 deletions examples/ecs/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct EventTriggerState {
impl Default for EventTriggerState {
fn default() -> Self {
EventTriggerState {
event_timer: Timer::from_seconds(1.0),
event_timer: Timer::from_seconds(1.0, true),
}
}
}
Expand All @@ -40,8 +40,6 @@ fn event_trigger_system(
my_events.send(MyEvent {
message: "MyEvent just happened!".to_string(),
});

state.event_timer.reset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ui/font_atlas_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Default for State {
Self {
added: false,
handle: Handle::default(),
timer: Timer::from_seconds(0.05),
timer: Timer::from_seconds(0.05, true),
}
}
}
Expand Down