2020-03-30 06:51:13 +00:00
|
|
|
use bevy::prelude::*;
|
|
|
|
|
|
|
|
struct MyEvent {
|
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::build()
|
2020-04-04 19:40:32 +00:00
|
|
|
.add_default_plugins()
|
2020-03-30 06:51:13 +00:00
|
|
|
.add_event::<MyEvent>()
|
|
|
|
.add_system(event_trigger_system())
|
2020-04-16 02:40:24 +00:00
|
|
|
.add_system_init(event_listener_system)
|
2020-03-30 06:51:13 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2020-04-07 04:32:19 +00:00
|
|
|
// sends MyEvent every second
|
2020-03-30 06:51:13 +00:00
|
|
|
fn event_trigger_system() -> Box<dyn Schedulable> {
|
|
|
|
let mut elapsed = 0.0;
|
2020-03-30 22:51:41 +00:00
|
|
|
SystemBuilder::new("event_trigger")
|
2020-03-30 06:51:13 +00:00
|
|
|
.read_resource::<Time>()
|
2020-04-01 01:04:54 +00:00
|
|
|
.write_resource::<Events<MyEvent>>()
|
|
|
|
.build(move |_, _, (time, my_events), _| {
|
2020-03-30 06:51:13 +00:00
|
|
|
elapsed += time.delta_seconds;
|
|
|
|
if elapsed > 1.0 {
|
2020-04-01 01:04:54 +00:00
|
|
|
my_events.send(MyEvent {
|
2020-03-30 06:51:13 +00:00
|
|
|
message: "Hello World".to_string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
elapsed = 0.0;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-07 04:32:19 +00:00
|
|
|
// prints events as they come in
|
2020-03-30 06:51:13 +00:00
|
|
|
fn event_listener_system(resources: &mut Resources) -> Box<dyn Schedulable> {
|
2020-04-01 01:04:54 +00:00
|
|
|
let mut my_event_reader = resources.get_event_reader::<MyEvent>();
|
2020-03-30 22:51:41 +00:00
|
|
|
SystemBuilder::new("event_listener")
|
2020-04-01 01:04:54 +00:00
|
|
|
.read_resource::<Events<MyEvent>>()
|
2020-03-30 06:51:13 +00:00
|
|
|
.build(move |_, _, my_events, _| {
|
2020-04-01 01:04:54 +00:00
|
|
|
for my_event in my_events.iter(&mut my_event_reader) {
|
2020-03-30 06:51:13 +00:00
|
|
|
println!("{}", my_event.message);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|