#timer #bevy #observer

bevy_mod_observable_timer

An observer-based timer for bevy

2 releases

0.1.1 Oct 23, 2024
0.1.0 Oct 22, 2024

#363 in Game dev

41 downloads per month

MIT/Apache

19KB
294 lines

bevy_mod_observable_timer

This crate provides an observer-based timer system for bevy entities. Any entity may have an ObservableTimer component attached to it, which will produce observable lifetime cycle triggers. Each timer is given an interval duration, and may run for one or more intervals (including indefinitely).

  • TimerStarted is triggered immediately after inserting a new ObservableTimer (including when overwriting an old one).
  • TimerInterval is triggered after each elapsed interval.
  • TimerFinished is triggered after the final interval elapses, or when the ObservableTimer component is removed/despawned.

When a timer finishes it will automatically perform some behavior. By default this is despawning its attached entity. See TimerFinishBehavior for more information.

Basic Example

use bevy::{log::LogPlugin, prelude::*};
use bevy_mod_observable_timer::*;

fn main() {
    App::new()
        .add_plugins((
            MinimalPlugins,
            LogPlugin::default(),
            ObservableTimerPlugin::default(),
        ))
        .add_systems(Startup, startup)
        .run();
}

fn startup(mut commands: Commands) {
    commands
        .spawn(ObservableTimer::from_seconds(5, 1.0))
        .observe(|_: Trigger<TimerStarted>| {
            info!("Timer started");
        })
        .observe(|trigger: Trigger<TimerInterval>| {
            info!("Interval #{}", trigger.event().count());
        })
        .observe(|_: Trigger<TimerFinished>, mut app_exit: EventWriter<AppExit>| {
            info!("Timer finished");
            app_exit.send_default();
        });
}

Output:

[t=0] Timer started
[t=1] Interval #1
[t=2] Interval #2
[t=3] Interval #3
[t=4] Interval #4
[t=5] Interval #5
[t=5] Timer finished

Dependencies

~24MB
~455K SLoC