2020-11-22 00:38:24 +00:00
|
|
|
use bevy::{prelude::*, utils::Duration};
|
2020-05-03 08:30:10 +00:00
|
|
|
|
2020-07-28 20:43:07 +00:00
|
|
|
/// 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.
|
2020-05-03 08:30:10 +00:00
|
|
|
fn main() {
|
2021-07-27 20:21:06 +00:00
|
|
|
App::new()
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2020-05-03 08:30:10 +00:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:27:30 +00:00
|
|
|
// This "print message plugin" prints a `message` every `wait_duration`
|
2020-05-03 08:30:10 +00:00
|
|
|
pub struct PrintMessagePlugin {
|
|
|
|
// Put your plugin configuration here
|
|
|
|
wait_duration: Duration,
|
|
|
|
message: String,
|
|
|
|
}
|
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
impl Plugin for PrintMessagePlugin {
|
2020-05-03 08:30:10 +00:00
|
|
|
// this is where we set up our plugin
|
2021-07-27 20:21:06 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2020-05-03 08:30:10 +00:00
|
|
|
let state = PrintMessageState {
|
|
|
|
message: self.message.clone(),
|
2020-08-21 21:57:25 +00:00
|
|
|
timer: Timer::new(self.wait_duration, true),
|
2020-05-03 08:30:10 +00:00
|
|
|
};
|
2021-07-27 23:42:36 +00:00
|
|
|
app.insert_resource(state).add_system(print_message_system);
|
2020-05-03 08:30:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PrintMessageState {
|
|
|
|
message: String,
|
2020-06-04 02:53:41 +00:00
|
|
|
timer: Timer,
|
2020-05-03 08:30:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 02:53:41 +00:00
|
|
|
fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
|
2021-03-05 19:59:14 +00:00
|
|
|
if state.timer.tick(time.delta()).finished() {
|
2021-04-22 23:30:48 +00:00
|
|
|
info!("{}", state.message);
|
2020-05-03 08:30:10 +00:00
|
|
|
}
|
|
|
|
}
|