hecs_schedule/
commandbuffer.rs1use hecs::{
2 Bundle, CommandBuffer as CommandBufferInternal, Component, DynamicBundle, Entity, World,
3};
4
5#[derive(Default)]
6pub struct CommandBuffer {
13 components: CommandBufferInternal,
15 despawns: Vec<Entity>,
16 writes: Vec<Box<dyn FnOnce(&mut World) + Send + Sync>>,
17}
18
19impl CommandBuffer {
20 pub fn new() -> Self {
22 Self::default()
23 }
24
25 pub fn insert(&mut self, entity: Entity, components: impl DynamicBundle) {
27 self.components.insert(entity, components)
28 }
29
30 pub fn insert_one(&mut self, entity: Entity, component: impl Component) {
32 self.components.insert(entity, (component,))
33 }
34
35 pub fn spawn(&mut self, components: impl DynamicBundle) {
38 self.components.spawn(components)
39 }
40
41 pub fn despawn(&mut self, entity: Entity) {
43 self.despawns.push(entity)
44 }
45
46 pub fn remove<C: Component + Bundle>(&mut self, entity: Entity) {
48 self.writes.push(Box::new(move |w| {
49 let _ = w.remove::<C>(entity);
50 }))
51 }
52
53 pub fn remove_one<C: Component>(&mut self, entity: Entity) {
55 self.writes.push(Box::new(move |w| {
56 let _ = w.remove_one::<C>(entity);
57 }))
58 }
59
60 pub fn execute(&mut self, world: &mut World) {
62 self.components.run_on(world);
63
64 self.writes.drain(..).for_each(|cmd| (cmd)(world));
65
66 self.despawns
67 .drain(..)
68 .for_each(|e| world.despawn(e).expect("Failed to despawn entity"));
69 }
70
71 pub fn append(&mut self, mut other: Self) {
73 self.write(move |w| other.execute(w))
74 }
75
76 pub fn write(&mut self, cmd: impl FnOnce(&mut World) + Component) {
78 self.writes.push(Box::new(cmd))
79 }
80
81 pub fn clear(&mut self) {
83 self.despawns.clear();
84 self.writes.clear();
85 self.components.clear();
86 }
87}
88