bevy/examples/app/plugin.rs

48 lines
1.4 KiB
Rust
Raw Normal View History

2020-05-03 08:30:10 +00:00
use bevy::prelude::*;
use std::time::Duration;
// 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).
fn main() {
App::build()
.add_default_plugins()
// 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,
}
impl AppPlugin for PrintMessagePlugin {
// this is where we set up our plugin
fn build(&self, app: &mut AppBuilder) {
let state = PrintMessageState {
message: self.message.clone(),
2020-06-04 02:53:41 +00:00
timer: Timer::new(self.wait_duration),
2020-05-03 08:30:10 +00:00
};
app.add_resource(state)
.add_system(print_message_system.system());
}
}
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>) {
state.timer.tick(time.delta_seconds);
if state.timer.finished {
2020-05-03 08:30:10 +00:00
println!("{}", state.message);
2020-06-04 02:53:41 +00:00
state.timer.reset();
2020-05-03 08:30:10 +00:00
}
}