port event_update to system function

This commit is contained in:
Carter Anderson 2020-04-30 16:19:28 -07:00
parent 37b4dff172
commit e5a99fde4f
2 changed files with 14 additions and 11 deletions

View File

@ -1,10 +1,10 @@
use crate::{
plugin::{load_plugin, AppPlugin},
schedule_plan::SchedulePlan,
stage, App, AppExit, Events, System, FromResources,
stage, App, AppExit, Events, FromResources, System,
};
use legion::prelude::{Resources, Universe, World};
use legion::prelude::{IntoSystem, Resources, Universe, World};
static APP_MISSING_MESSAGE: &str = "This AppBuilder no longer has an App. Check to see if you already called run(). A call to app_builder.run() consumes the AppBuilder's App.";
@ -196,7 +196,11 @@ impl AppBuilder {
T: Send + Sync + 'static,
{
self.add_resource(Events::<T>::default())
.add_system_to_stage(stage::EVENT_UPDATE, Events::<T>::build_update_system())
.add_system_to_stage(
stage::EVENT_UPDATE,
Events::<T>::update_system
.system_id(format!("events_update::{}", std::any::type_name::<T>()).into()),
)
}
pub fn add_resource<T>(&mut self, resource: T) -> &mut Self

View File

@ -1,4 +1,4 @@
use legion::prelude::{Resources, Schedulable, SystemBuilder};
use legion::prelude::{Resources, ResourceMut};
use std::marker::PhantomData;
struct EventInstance<T> {
@ -12,8 +12,8 @@ enum State {
}
/// An event collection that represents the events that occurred within the last two [Events::update] calls. Events can be cheaply read using
/// an [EventReader]. This collection is meant to be paired with a system that calls [Events::update] exactly once per update/frame. [Events::build_update_system]
/// will produce a system that does this. [EventReader]s are expected to read events from this collection at least once per update/frame. If events are not handled
/// an [EventReader]. This collection is meant to be paired with a system that calls [Events::update] exactly once per update/frame. [Events::update_system]
/// is a system that does this. [EventReader]s are expected to read events from this collection at least once per update/frame. If events are not handled
/// within one frame/update, they will be dropped.
///
/// # Example
@ -219,14 +219,13 @@ where
}
}
/// Builds a system that calls [Events::update] once per frame.
pub fn build_update_system() -> Box<dyn Schedulable> {
SystemBuilder::new(format!("events_update::{}", std::any::type_name::<T>()))
.write_resource::<Self>()
.build(|_, _, events, _| events.update())
/// A system that calls [Events::update] once per frame.
pub fn update_system(mut events: ResourceMut<Self>) {
events.update();
}
}
pub trait GetEventReader {
/// returns an [EventReader] of the given type
fn get_event_reader<T>(&self) -> EventReader<T>