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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use std::collections::VecDeque;
use crate::prelude::*;
pub struct SystemStages {
pub stages: Vec<Box<dyn SystemStage>>,
}
impl SystemStages {
pub fn initialize_systems(&mut self, world: &mut World) {
for stage in &mut self.stages {
stage.initialize(world);
}
}
pub fn run(&mut self, world: &mut World) -> SystemResult {
for stage in &mut self.stages {
stage.run(world)?;
}
Ok(())
}
pub fn with_core_stages() -> Self {
Self {
stages: vec![
Box::new(SimpleSystemStage::new(CoreStage::First)),
Box::new(SimpleSystemStage::new(CoreStage::PreUpdate)),
Box::new(SimpleSystemStage::new(CoreStage::Update)),
Box::new(SimpleSystemStage::new(CoreStage::PostUpdate)),
Box::new(SimpleSystemStage::new(CoreStage::Last)),
],
}
}
pub fn add_system_to_stage<Args, S: IntoSystem<Args, ()>, L: StageLabel>(
&mut self,
label: L,
system: S,
) -> &mut Self {
let name = label.name();
let id = label.id();
let mut stage = None;
for st in &mut self.stages {
if st.id() == id {
stage = Some(st);
}
}
let Some(stage) = stage else {
panic!("Stage with label `{}` ( {} ) doesn't exist.", name, id);
};
stage.add_system(system.system());
self
}
#[track_caller]
pub fn insert_stage_before<L: StageLabel, S: SystemStage + 'static>(
&mut self,
label: L,
stage: S,
) {
let stage_idx = self
.stages
.iter()
.position(|x| x.id() == CoreStage::PreUpdate.id())
.unwrap_or_else(|| panic!("Could not find stage with label `{}`", label.name()));
self.stages.insert(stage_idx, Box::new(stage));
}
#[track_caller]
pub fn insert_stage_after<L: StageLabel, S: SystemStage + 'static>(
&mut self,
label: L,
stage: S,
) {
let stage_idx = self
.stages
.iter()
.position(|x| x.id() == CoreStage::PreUpdate.id())
.unwrap_or_else(|| panic!("Could not find stage with label `{}`", label.name()));
self.stages.insert(stage_idx + 1, Box::new(stage));
}
}
pub trait SystemStage: Sync + Send {
fn id(&self) -> Ulid;
fn name(&self) -> String;
fn run(&mut self, world: &mut World) -> SystemResult;
fn initialize(&mut self, world: &mut World);
fn add_system(&mut self, system: System<()>);
}
pub struct SimpleSystemStage {
pub id: Ulid,
pub name: String,
pub systems: Vec<System<()>>,
}
impl SimpleSystemStage {
pub fn new<L: StageLabel>(label: L) -> Self {
Self {
id: label.id(),
name: label.name(),
systems: Default::default(),
}
}
}
impl SystemStage for SimpleSystemStage {
fn id(&self) -> Ulid {
self.id
}
fn name(&self) -> String {
self.name.clone()
}
fn run(&mut self, world: &mut World) -> SystemResult {
for system in &mut self.systems {
system.run(world)?;
}
{
let command_queue = world.resources.get::<CommandQueue>();
let mut command_queue = command_queue.borrow_mut();
for mut system in command_queue.queue.drain(..) {
system.initialize(world);
system.run(world).unwrap();
}
}
Ok(())
}
fn initialize(&mut self, world: &mut World) {
world.resources.init::<CommandQueue>();
for system in &mut self.systems {
system.initialize(world);
}
}
fn add_system(&mut self, system: System<()>) {
self.systems.push(system);
}
}
pub trait StageLabel {
fn name(&self) -> String;
fn id(&self) -> Ulid;
}
#[derive(Copy, Clone, Debug)]
pub enum CoreStage {
First,
PreUpdate,
Update,
PostUpdate,
Last,
}
impl StageLabel for CoreStage {
fn name(&self) -> String {
format!("{:?}", self)
}
fn id(&self) -> Ulid {
match self {
CoreStage::First => Ulid(2021715391084198804812356024998495966),
CoreStage::PreUpdate => Ulid(2021715401330719559452824437611089988),
CoreStage::Update => Ulid(2021715410160177201728645950400543948),
CoreStage::PostUpdate => Ulid(2021715423103233646561968734173322317),
CoreStage::Last => Ulid(2021715433398666914977687392909851554),
}
}
}
#[derive(Debug, TypeUlid, Default)]
#[ulid = "01GPY3KPT0CDNCM23HTKAKN0NJ"]
pub struct CommandQueue {
pub queue: VecDeque<System>,
}
impl Clone for CommandQueue {
fn clone(&self) -> Self {
if self.queue.is_empty() {
Self {
queue: VecDeque::with_capacity(self.queue.capacity()),
}
} else {
panic!(
"Cannot clone CommandQueue. This probably happened because you are \
trying to clone a World while a system stage is still executing."
)
}
}
}
impl CommandQueue {
pub fn add<Args, S: IntoSystem<Args, ()>>(&mut self, system: S) {
self.queue.push_back(system.system());
}
}
#[derive(Deref, DerefMut)]
pub struct Commands<'a>(AtomicRefMut<'a, CommandQueue>);
impl<'a> SystemParam for Commands<'a> {
type State = AtomicResource<CommandQueue>;
type Param<'s> = Commands<'s>;
fn initialize(_world: &mut World) {}
fn get_state(world: &World) -> Self::State {
world.resources.get::<CommandQueue>()
}
fn borrow(state: &mut Self::State) -> Self::Param<'_> {
Commands(state.borrow_mut())
}
}