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