forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rs
47 lines (42 loc) · 1.45 KB
/
plugin.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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).
/// This example illustrates how to create a simple plugin that prints out a message.
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();
}
// 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 AppBuilder) {
let state = PrintMessageState {
message: self.message.clone(),
timer: Timer::new(self.wait_duration, true),
};
app.add_resource(state)
.add_system(print_message_system.system());
}
}
struct PrintMessageState {
message: String,
timer: Timer,
}
fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
state.timer.tick(time.delta_seconds);
if state.timer.finished {
println!("{}", state.message);
}
}