Use trait also for systems
This commit is contained in:
parent
28e833203a
commit
e92cd4e46e
4 changed files with 35 additions and 20 deletions
|
@ -17,7 +17,7 @@ pub use crate::type_map::{
|
|||
};
|
||||
pub use crate::unsafe_component_store::UnsafeComponentStore;
|
||||
pub use crate::updates::*;
|
||||
pub use crate::world::{World, WorldBuilder};
|
||||
pub use crate::world::{AddSystem, World, WorldBuilder};
|
||||
pub use commands::Commands;
|
||||
pub use get_disjoint_mut::GetDisjointMut;
|
||||
pub use update_macros::Resource;
|
||||
|
|
|
@ -292,4 +292,4 @@ impl_update_filter!(Nonuple, [R, r], [S, s], [T, t], [U, u], [V, v], [W, w],
|
|||
#[rustfmt::skip]
|
||||
impl_update_filter!(Decuple, [R, r], [S, s], [T, t], [U, u], [V, v], [W, w], [X, x], [Y, y], [Z, z], [A, a]);
|
||||
|
||||
implement_updates!(5, 5);
|
||||
implement_updates!(5, 6);
|
||||
|
|
|
@ -10,26 +10,20 @@ use utilities::prelude::{remove_life_time, remove_life_time_mut};
|
|||
|
||||
use crate::{entity_object_manager::EntityObjectManager, *};
|
||||
|
||||
pub trait AddSystem<R, Func> {
|
||||
fn add_system(&mut self, priority: u32, f: Func);
|
||||
}
|
||||
|
||||
pub struct WorldBuilder {
|
||||
pub(crate) updates: Updates,
|
||||
pub events: Events,
|
||||
pub resources: Resources,
|
||||
systems: Vec<(
|
||||
pub(crate) systems: Vec<(
|
||||
u32,
|
||||
Box<dyn Fn(&mut World) -> Result<bool> + Send + Sync + 'static>,
|
||||
Box<dyn Fn(&mut World, &mut Commands) -> Result<bool> + Send + Sync + 'static>,
|
||||
)>,
|
||||
}
|
||||
|
||||
impl WorldBuilder {
|
||||
pub fn add_system<F>(&mut self, priority: u32, f: F)
|
||||
where
|
||||
F: Fn(&mut World) -> Result<bool> + Send + Sync + 'static,
|
||||
{
|
||||
self.systems.push((priority, Box::new(f)));
|
||||
self.systems.sort_by_key(|(priority, _)| *priority);
|
||||
}
|
||||
}
|
||||
|
||||
impl WorldBuilder {
|
||||
pub fn build(self) -> World {
|
||||
World {
|
||||
|
@ -72,7 +66,7 @@ pub struct World {
|
|||
|
||||
start_time: Instant,
|
||||
|
||||
systems: Vec<Box<dyn Fn(&mut World) -> Result<bool> + Send + Sync + 'static>>,
|
||||
systems: Vec<Box<dyn Fn(&mut World, &mut Commands) -> Result<bool> + Send + Sync + 'static>>,
|
||||
}
|
||||
|
||||
impl World {
|
||||
|
@ -433,12 +427,16 @@ impl World {
|
|||
|
||||
self.commit_entity_changes()?;
|
||||
|
||||
let mut commands = Commands::new(self.now());
|
||||
|
||||
for system in systems.iter() {
|
||||
if !system(self)? {
|
||||
if !system(self, &mut commands)? {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
commands.apply_deferred(self)?;
|
||||
|
||||
// reset flag
|
||||
self.had_entity_changes = false;
|
||||
}
|
||||
|
|
|
@ -309,7 +309,7 @@ impl Update {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn resource_only_events(&self) -> TokenStream2 {
|
||||
pub fn resource_only_events_and_systems(&self) -> TokenStream2 {
|
||||
let resource_types = self.resource_types();
|
||||
let resource_type_impls = self.resource_type_impls();
|
||||
|
||||
|
@ -329,7 +329,7 @@ impl Update {
|
|||
quote! {
|
||||
impl<P, Func, #resource_type_impls> EventReader<P, (), #resource_types, Func, ()> for Events
|
||||
where
|
||||
Func: Fn(&P, &mut Commands, #resources ) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
Func: Fn(&P, &mut Commands, #resources ) -> Result<()> + Send + Sync + 'static,
|
||||
#reource_requirement
|
||||
P: std::any::Any + Send + Sync,
|
||||
{
|
||||
|
@ -350,6 +350,22 @@ impl Update {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Func, #resource_type_impls> AddSystem<#resource_types, Func> for WorldBuilder
|
||||
where
|
||||
Func: Fn(&mut Commands, #resources ) -> Result<bool> + Send + Sync + 'static,
|
||||
#reource_requirement
|
||||
{
|
||||
fn add_system(&mut self, priority: u32, func: Func) {
|
||||
self.systems.push((priority, Box::new(move |world: &mut World, commands: &mut Commands| {
|
||||
#resource_store
|
||||
func(commands, #( #resource_idents, )*)
|
||||
})));
|
||||
self.systems.sort_by_key(|(priority, _)| *priority);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -495,7 +511,7 @@ impl ToTokens for Update {
|
|||
|
||||
impl<P, Func, #filter_type_impls #query_type_impls #resource_type_impls> EventReader<P, #query_types, #resource_types, Func, #filter_types> for Events
|
||||
where
|
||||
Func: Fn(&P, &mut Commands, #( #queries, )* #resources ) -> Result<()> + Send + Sync + Clone + 'static,
|
||||
Func: Fn(&P, &mut Commands, #( #queries, )* #resources ) -> Result<()> + Send + Sync + 'static,
|
||||
#filter_requirements
|
||||
#component_requirements
|
||||
#reource_requirement
|
||||
|
@ -588,7 +604,8 @@ pub fn update(max_components: usize, max_resources: usize) -> TokenStream {
|
|||
let mut events = Vec::new();
|
||||
|
||||
for resource_count in resources.iter() {
|
||||
let q = Update::new(vec![1].into_iter(), *resource_count).resource_only_events();
|
||||
let q =
|
||||
Update::new(vec![1].into_iter(), *resource_count).resource_only_events_and_systems();
|
||||
|
||||
events.push(q);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue