mirror of
https://github.com/bevyengine/bevy
synced 2024-11-25 06:00:20 +00:00
add event example
This commit is contained in:
parent
5eb789ff77
commit
13da707aee
3 changed files with 61 additions and 7 deletions
43
examples/event.rs
Normal file
43
examples/event.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
struct MyEvent {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::build()
|
||||
.add_defaults()
|
||||
.add_event::<MyEvent>()
|
||||
.add_system(event_trigger_system())
|
||||
.build_system(event_listener_system)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn event_trigger_system() -> Box<dyn Schedulable> {
|
||||
let mut elapsed = 0.0;
|
||||
SystemBuilder::new("EventTrigger")
|
||||
.read_resource::<Time>()
|
||||
.write_resource::<Event<MyEvent>>()
|
||||
.build(move |_, _, (time, my_event), _| {
|
||||
elapsed += time.delta_seconds;
|
||||
if elapsed > 1.0 {
|
||||
my_event.raise(MyEvent {
|
||||
message: "Hello World".to_string(),
|
||||
});
|
||||
|
||||
elapsed = 0.0;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn event_listener_system(resources: &mut Resources) -> Box<dyn Schedulable> {
|
||||
let my_event = resources.get::<Event<MyEvent>>().unwrap();
|
||||
let mut my_event_handle = my_event.get_handle();
|
||||
SystemBuilder::new("EventListener")
|
||||
.read_resource::<Event<MyEvent>>()
|
||||
.build(move |_, _, my_events, _| {
|
||||
for my_event in my_events.iter(&mut my_event_handle) {
|
||||
println!("{}", my_event.message);
|
||||
}
|
||||
})
|
||||
}
|
|
@ -119,12 +119,17 @@ impl AppBuilder {
|
|||
pub fn add_system(self, system: Box<dyn Schedulable>) -> Self {
|
||||
self.add_system_to_stage(system_stage::UPDATE, system)
|
||||
}
|
||||
|
||||
|
||||
pub fn add_setup_system(mut self, system: Box<dyn Schedulable>) -> Self {
|
||||
self.setup_systems.push(system);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build_system<F>(mut self, build: F) -> Self where F: Fn(&mut Resources) -> Box<dyn Schedulable>{
|
||||
let system = build(&mut self.resources);
|
||||
self.add_system(system)
|
||||
}
|
||||
|
||||
pub fn add_system_to_stage(mut self, stage_name: &str, system: Box<dyn Schedulable>) -> Self {
|
||||
if let None = self.system_stages.get(stage_name) {
|
||||
self.system_stages
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
pub use crate::{
|
||||
app::{App, AppBuilder, plugin::AppPlugin},
|
||||
asset::{Asset, AssetStorage, Handle},
|
||||
core::{Time, Window},
|
||||
core::{Time, Window, Event, EventHandle},
|
||||
ecs,
|
||||
ecs::{
|
||||
default_archetypes::*, CommandBufferBuilderSource, EntityArchetype, WorldBuilder,
|
||||
WorldBuilderSource,
|
||||
WorldBuilderSource
|
||||
},
|
||||
render::{
|
||||
mesh::{Mesh, MeshType},
|
||||
|
@ -23,11 +23,17 @@ pub use bevy_derive::*;
|
|||
pub use bevy_transform::prelude::*;
|
||||
pub use glam as math;
|
||||
pub use legion::{
|
||||
prelude::*,
|
||||
prelude::Event as LegionEvent,
|
||||
command::CommandBuffer,
|
||||
entity::Entity,
|
||||
event::Event as LegionEvent,
|
||||
filter::filter_fns::*,
|
||||
query::{IntoQuery, Query, Read, Tagged, TryRead, TryWrite, Write},
|
||||
world::{Universe, World},
|
||||
systems::{
|
||||
schedule::{Builder, Schedulable},
|
||||
SubWorld, SystemBuilder,
|
||||
bit_set::BitSet,
|
||||
resource::{ResourceSet, Resources},
|
||||
schedule::{Executor, Runnable, Schedulable, Schedule},
|
||||
System, SystemBuilder, SubWorld
|
||||
},
|
||||
};
|
||||
pub use math::{Mat3, Mat4, Quat, Vec2, Vec3, Vec4};
|
||||
|
|
Loading…
Reference in a new issue