mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 22:50:19 +00:00
73605f43b6
As mentioned in #2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation. This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from #2683, into one struct. Signed-off-by: Lena Milizé <me@lvmn.org> # Objective Fixes #2926. ## Solution Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`. --- ## Changelog ### Added - `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating` ### Changed - Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode` - Change `repeating: bool` field of `Timer` with `mode: TimerMode` ## Migration Guide - Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`. - Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`. - Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`. - Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`. - Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
48 lines
1.5 KiB
Rust
48 lines
1.5 KiB
Rust
//! Demonstrates the creation and registration of a custom plugin.
|
|
//!
|
|
//! Plugins are the foundation of Bevy. They are scoped sets of components, resources, and systems
|
|
//! that provide a specific piece of functionality (generally the smaller the scope, the better).
|
|
//! This example illustrates how to create a simple plugin that prints out a message.
|
|
|
|
use bevy::{prelude::*, utils::Duration};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
// plugins are registered as part of the "app building" process
|
|
.add_plugin(PrintMessagePlugin {
|
|
wait_duration: Duration::from_secs(1),
|
|
message: "This is an example plugin".to_string(),
|
|
})
|
|
.run();
|
|
}
|
|
|
|
// This "print message plugin" prints a `message` every `wait_duration`
|
|
pub struct PrintMessagePlugin {
|
|
// Put your plugin configuration here
|
|
wait_duration: Duration,
|
|
message: String,
|
|
}
|
|
|
|
impl Plugin for PrintMessagePlugin {
|
|
// this is where we set up our plugin
|
|
fn build(&self, app: &mut App) {
|
|
let state = PrintMessageState {
|
|
message: self.message.clone(),
|
|
timer: Timer::new(self.wait_duration, TimerMode::Repeating),
|
|
};
|
|
app.insert_resource(state).add_system(print_message_system);
|
|
}
|
|
}
|
|
|
|
#[derive(Resource)]
|
|
struct PrintMessageState {
|
|
message: String,
|
|
timer: Timer,
|
|
}
|
|
|
|
fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
|
|
if state.timer.tick(time.delta()).finished() {
|
|
info!("{}", state.message);
|
|
}
|
|
}
|