1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
pub struct L8r<W>(Vec<Box<dyn FnOnce(&mut W)>>); impl<W> L8r<W> { pub fn new() -> Self { L8r(Vec::new()) } pub fn schedule(&mut self, then: Box<dyn FnOnce(&mut W)>) { self.0.push(then); } pub fn l8r<F: 'static + Send + Sync + FnOnce(&mut W)>(&mut self, then: F) { self.0.push(Box::new(then)); } pub fn drain(&mut self) -> Vec<Box<dyn FnOnce(&mut W)>> { self.0.drain(..).collect::<Vec<_>>() } pub fn now(l8rs: Vec<Box<dyn FnOnce(&mut W)>>, world: &mut W) { for l8r in l8rs.into_iter() { l8r(world); } } } impl<W> L8r<W> where W: ContainsHecsWorld { pub fn insert_one<C: hecs::Component>(&mut self, ent: hecs::Entity, component: C) { self.l8r(move |world| world.ecs_mut().insert_one(ent, component).unwrap()) } pub fn remove_one<C: hecs::Component>(&mut self, ent: hecs::Entity) { self.l8r(move |world| drop(world.ecs_mut().remove_one::<C>(ent))) } pub fn insert<C: 'static + Send + Sync + hecs::DynamicBundle>( &mut self, ent: hecs::Entity, components_bundle: C, ) { self.l8r(move |world| world.ecs_mut().insert(ent, components_bundle).unwrap()) } pub fn spawn<C: 'static + Send + Sync + hecs::DynamicBundle>(&mut self, components_bundle: C) { self.l8r(move |world| drop(world.ecs_mut().spawn(components_bundle))) } pub fn despawn(&mut self, entity: hecs::Entity) { self.l8r(move |world| drop(world.ecs_mut().despawn(entity))) } } pub trait ContainsHecsWorld { fn ecs(&self) -> &hecs::World; fn ecs_mut(&mut self) -> &mut hecs::World; }