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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
//! Implementation of stage abstraction for running collections of systems over a [`World`].
use std::collections::VecDeque;
use crate::prelude::*;
/// Resource that is automatically added to the world while a system stage is being run
/// that specifies the unique ID of the stage that being run.
///
/// If the stage is `Ulid(0)`, the default ID, then that means the startup stage is being run.
#[derive(Deref, DerefMut, Clone, Copy, HasSchema, Default)]
pub struct CurrentSystemStage(pub Ulid);
/// An ordered collection of [`SystemStage`]s.
pub struct SystemStages {
/// The stages in the collection, in the order that they will be run.
pub stages: Vec<Box<dyn SystemStage>>,
/// Whether or not the startup systems have been run yet.
pub has_started: bool,
/// The systems that should run at startup.
pub startup_systems: Vec<StaticSystem<(), ()>>,
/// Systems that are continously run until they succeed(return Some). These run before all stages. Uses Option to allow for easy usage of `?`.
pub single_success_systems: Vec<StaticSystem<(), Option<()>>>,
}
impl std::fmt::Debug for SystemStages {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SystemStages")
// TODO: Add list of stages to the debug render for `SystemStages`.
// We can at least list the names of each stage for `SystemStages` debug
// implementation.
.finish()
}
}
impl Default for SystemStages {
fn default() -> Self {
Self::with_core_stages()
}
}
impl SystemStages {
/// Execute the systems on the given `world`.
pub fn run(&mut self, world: &mut World) {
// If we haven't run our startup systems yet
if !self.has_started {
// Set the current stage resource
world.insert_resource(CurrentSystemStage(Ulid(0)));
// For each startup system
for system in &mut self.startup_systems {
// Run the system
system.run(world, ());
}
// Don't run startup systems again
self.has_started = true;
}
// Run single success systems
self.single_success_systems.retain_mut(|system| {
let result = system.run(world, ());
result.is_none() // Keep the system if it didn't succeed (returned None)
});
// Run each stage
for stage in &mut self.stages {
// Set the current stage resource
world.insert_resource(CurrentSystemStage(stage.id()));
// Run the stage
stage.run(world);
}
// Cleanup killed entities
world.maintain();
// Remove the current system stage resource
world.resources.remove::<CurrentSystemStage>();
}
/// Create a [`SystemStages`] collection, initialized with a stage for each [`CoreStage`].
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)),
],
has_started: false,
startup_systems: default(),
single_success_systems: Vec::new(),
}
}
/// Add a system that will run only once, before all of the other non-startup systems.
pub fn add_startup_system<Args, S>(&mut self, system: S) -> &mut Self
where
S: IntoSystem<Args, (), (), Sys = StaticSystem<(), ()>>,
{
self.startup_systems.push(system.system());
self
}
/// Add a system that will run each frame until it succeeds (returns Some). Runs before all stages. Uses Option to allow for easy usage of `?`.
pub fn add_single_success_system<Args, S>(&mut self, system: S) -> &mut Self
where
S: IntoSystem<Args, (), Option<()>, Sys = StaticSystem<(), Option<()>>>,
{
self.single_success_systems.push(system.system());
self
}
/// Add a [`System`] to the stage with the given label.
pub fn add_system_to_stage<Args, S>(&mut self, label: impl StageLabel, system: S) -> &mut Self
where
S: IntoSystem<Args, (), (), Sys = StaticSystem<(), ()>>,
{
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
}
/// Insert a new stage, before another existing stage
#[track_caller]
pub fn insert_stage_before<L: StageLabel, S: SystemStage + 'static>(
&mut self,
label: L,
stage: S,
) -> &mut Self {
let stage_idx = self
.stages
.iter()
.position(|x| x.id() == label.id())
.unwrap_or_else(|| panic!("Could not find stage with label `{}`", label.name()));
self.stages.insert(stage_idx, Box::new(stage));
self
}
/// Insert a new stage, after another existing stage
#[track_caller]
pub fn insert_stage_after<L: StageLabel, S: SystemStage + 'static>(
&mut self,
label: L,
stage: S,
) -> &mut Self {
let stage_idx = self
.stages
.iter()
.position(|x| x.id() == label.id())
.unwrap_or_else(|| panic!("Could not find stage with label `{}`", label.name()));
self.stages.insert(stage_idx + 1, Box::new(stage));
self
}
/// Remove all systems from all stages, including startup and single success systems. Resets has_started as well, allowing for startup systems to run once again.
pub fn reset_remove_all_systems(&mut self) {
// Reset the has_started flag
self.has_started = false;
self.remove_all_systems();
}
/// Remove all systems from all stages, including startup and single success systems. Does not reset has_started.
pub fn remove_all_systems(&mut self) {
// Clear startup systems
self.startup_systems.clear();
// Clear single success systems
self.single_success_systems.clear();
// Clear systems from each stage
for stage in &mut self.stages {
stage.remove_all_systems();
}
}
}
/// Trait for system stages. A stage is a
pub trait SystemStage: Sync + Send {
/// The unique identifier for the stage.
fn id(&self) -> Ulid;
/// The human-readable name for the stage, used for error messages when something goes wrong.
fn name(&self) -> String;
/// Execute the systems on the given `world`.
fn run(&mut self, world: &World);
/// Add a system to this stage.
fn add_system(&mut self, system: StaticSystem<(), ()>);
/// Remove all systems from this stage.
fn remove_all_systems(&mut self);
}
/// A collection of systems that will be run in order.
pub struct SimpleSystemStage {
/// The unique identifier for the stage.
pub id: Ulid,
/// The human-readable name for the stage, used for error messages when something goes wrong.
pub name: String,
/// The list of systems in the stage.
///
/// Each system will be run in the order that they are in in this list.
pub systems: Vec<StaticSystem<(), ()>>,
}
impl SimpleSystemStage {
/// Create a new, empty stage, for the given label.
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: &World) {
// Run the systems
for system in &mut self.systems {
system.run(world, ());
}
// Drain the command queue
let queue = world.resources.get_mut::<CommandQueue>();
if let Some(mut command_queue) = queue {
for mut system in command_queue.queue.drain(..) {
system.run(world, ());
}
}
}
fn add_system(&mut self, system: StaticSystem<(), ()>) {
self.systems.push(system);
}
fn remove_all_systems(&mut self) {
self.systems.clear();
}
}
/// Trait for things that may be used to identify a system stage.
pub trait StageLabel {
/// Returns the human-readable name of the label, used in error messages.
fn name(&self) -> String;
/// Returns a unique identifier for the stage.
fn id(&self) -> Ulid;
}
/// A [`StageLabel`] for the 5 core stages.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum CoreStage {
/// The first stage
First,
/// The second stage
PreUpdate,
/// The third stage
Update,
/// The fourth stage
PostUpdate,
/// The fifth stage
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),
}
}
}
/// A resource containing the [`Commands`] command queue.
///
/// You can use [`Commands`] as a [`SystemParam`] as a shortcut to [`ResMut<CommandQueue>`].
#[derive(HasSchema, Default)]
pub struct CommandQueue {
/// The system queue that will be run at the end of the stage
pub queue: VecDeque<StaticSystem<(), ()>>,
}
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 {
/// Add a system to be run at the end of the stage.
pub fn add<Args, S>(&mut self, system: S)
where
S: IntoSystem<Args, (), (), Sys = StaticSystem<(), ()>>,
{
self.queue.push_back(system.system());
}
}
/// A [`SystemParam`] that can be used to schedule systems that will be run at the end of the
/// current [`SystemStage`].
///
/// This is a shortcut for [`ResMut<CommandQueue>`].
#[derive(Deref, DerefMut)]
pub struct Commands<'a>(RefMut<'a, CommandQueue>);
impl<'a> SystemParam for Commands<'a> {
type State = AtomicResource<CommandQueue>;
type Param<'s> = Commands<'s>;
fn get_state(world: &World) -> Self::State {
let cell = world.resources.get_cell::<CommandQueue>();
cell.init(world);
cell
}
fn borrow<'s>(_world: &'s World, state: &'s mut Self::State) -> Self::Param<'s> {
Commands(state.borrow_mut().unwrap())
}
}