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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::marker::PhantomData;
use hecs::{
Bundle, CommandBuffer as CommandBufferInternal, Component, DynamicBundle, Entity, World,
};
pub trait WriteCmd: Component {
fn execute(&mut self, world: &mut World);
}
struct RemoveCmd<C: Component> {
entity: Entity,
marker: PhantomData<C>,
}
impl<C: Component> RemoveCmd<C> {
fn new(entity: Entity) -> Self {
Self {
entity,
marker: PhantomData,
}
}
}
impl<C: Component + Bundle> WriteCmd for RemoveCmd<C> {
fn execute(&mut self, world: &mut World) {
world
.remove::<C>(self.entity)
.expect("Failed to remove components from entity");
}
}
impl<F: FnMut(&mut World) + Component> WriteCmd for F {
fn execute(&mut self, world: &mut World) {
(self)(world)
}
}
#[derive(Default)]
pub struct CommandBuffer {
components: CommandBufferInternal,
despawns: Vec<Entity>,
writes: Vec<Box<dyn WriteCmd>>,
}
impl CommandBuffer {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, entity: Entity, components: impl DynamicBundle) {
self.components.insert(entity, components)
}
pub fn insert_one(&mut self, entity: Entity, component: impl Component) {
self.components.insert(entity, (component,))
}
pub fn spawn(&mut self, components: impl DynamicBundle) {
self.components.spawn(components)
}
pub fn despawn(&mut self, entity: Entity) {
self.despawns.push(entity)
}
pub fn remove<C: Component + Bundle>(&mut self, entity: Entity) {
self.writes.push(Box::new(RemoveCmd::<C>::new(entity)))
}
pub fn remove_one<C: Component>(&mut self, entity: Entity) {
self.writes.push(Box::new(RemoveCmd::<(C,)>::new(entity)))
}
pub fn execute(&mut self, world: &mut World) {
self.components.run_on(world);
self.writes.drain(..).for_each(|mut cmd| cmd.execute(world));
self.despawns
.drain(..)
.for_each(|e| world.despawn(e).expect("Failed to despawn entity"));
}
pub fn write(&mut self, cmd: impl WriteCmd) {
self.writes.push(Box::new(cmd))
}
pub fn clear(&mut self) {
self.despawns.clear();
self.writes.clear();
self.components.clear();
}
}
impl WriteCmd for CommandBuffer {
fn execute(&mut self, world: &mut World) {
self.execute(world)
}
}