add app builder shorthand for system function

This commit is contained in:
Carter Anderson 2020-04-28 02:52:26 -07:00
parent 092f3888ca
commit 92182060a9
3 changed files with 49 additions and 8 deletions

View file

@ -4,7 +4,9 @@ use crate::{
stage, App, AppExit, Events,
};
use legion::prelude::{Resources, Universe, World};
use legion::prelude::{Resources, Universe, World, IntoQuery, ResourceSet, into_system, into_resource_system};
use legion::query::{View, DefaultFilter};
use legion::filter::EntityFilter;
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.";
@ -237,4 +239,46 @@ impl AppBuilder {
plugin.build(self);
self
}
pub fn add_system_fn<'a, Q, F, R, X>(&mut self, name: &'static str, system: F) -> &mut Self
where
Q: IntoQuery + DefaultFilter<Filter = R>,
<Q as View<'a>>::Iter: Iterator<Item = Q> + 'a,
F: FnMut(&mut X, Q) + Send + Sync + 'static,
R: EntityFilter + Sync + 'static,
X: ResourceSet<PreparedResources = X> + 'static,
{
self.add_system_fn_to_stage(stage::UPDATE, name, system)
}
pub fn add_system_fn_to_stage<'a, Q, F, R, X>(&mut self, stage_name: &str, name: &'static str, system: F) -> &mut Self
where
Q: IntoQuery + DefaultFilter<Filter = R>,
<Q as View<'a>>::Iter: Iterator<Item = Q> + 'a,
F: FnMut(&mut X, Q) + Send + Sync + 'static,
R: EntityFilter + Sync + 'static,
X: ResourceSet<PreparedResources = X> + 'static,
{
let system = into_system(name, system);
self.add_system_to_stage(stage_name, system);
self
}
pub fn add_resource_system_fn<'a, F, X>(&mut self, name: &'static str, system: F) -> &mut Self
where
F: FnMut(&mut X) + Send + Sync + 'static,
X: ResourceSet<PreparedResources = X> + 'static,
{
self.add_resource_system_fn_to_stage(stage::UPDATE, name, system)
}
pub fn add_resource_system_fn_to_stage<'a, F, X>(&mut self, stage_name: &str, name: &'static str, system: F) -> &mut Self
where
F: FnMut(&mut X) + Send + Sync + 'static,
X: ResourceSet<PreparedResources = X> + 'static,
{
let system = into_resource_system(name, system);
self.add_system_to_stage(stage_name, system);
self
}
}

View file

@ -6,11 +6,8 @@ fn main() {
.add_event::<MyEvent>()
.add_resource(EventTriggerState::default())
.add_resource_init::<EventListenerState>()
.add_system(into_resource_system("event_trigger", event_trigger_system))
.add_system(into_resource_system(
"event_listener",
event_listener_system,
))
.add_resource_system_fn("event_trigger", event_trigger_system)
.add_resource_system_fn("event_listener", event_listener_system)
.run();
}
@ -60,4 +57,4 @@ fn event_listener_system(
for my_event in my_events.iter(&mut state.my_event_reader) {
println!("{}", my_event.message);
}
}
}

View file

@ -9,7 +9,7 @@ fn main() {
..Default::default()
})
.add_startup_system(setup)
.add_system(into_system("move", move_system))
.add_system_fn("move", move_system)
.run();
}