limnus_system_runner/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/limnus
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5mod schedule;
6
7use crate::schedule::Schedule;
8use limnus_system::{IntoSystem, SystemParam};
9use limnus_system_state::State;
10use std::collections::HashMap;
11
12#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
13pub enum UpdatePhase {
14    First,
15    PreUpdate,
16    Update,
17    PostUpdate,
18}
19
20#[derive(Default)]
21pub struct Runner {
22    schedules: HashMap<UpdatePhase, Schedule>,
23}
24
25impl Runner {}
26
27impl Runner {
28    #[must_use]
29    pub fn new() -> Self {
30        let phases_in_order = [
31            UpdatePhase::First,
32            UpdatePhase::PreUpdate,
33            UpdatePhase::Update,
34            UpdatePhase::PostUpdate,
35        ];
36
37        let mut schedules = HashMap::default();
38
39        for phase in phases_in_order {
40            schedules.insert(phase, Schedule::new());
41        }
42
43        Self { schedules }
44    }
45
46    pub fn add_system<F, Params>(&mut self, update_phase: UpdatePhase, system: F)
47    where
48        F: IntoSystem<Params>,
49        Params: SystemParam,
50    {
51        self.schedules
52            .get_mut(&update_phase)
53            .expect("tried to add to unknown phase")
54            .add_system(system);
55    }
56
57    pub fn run_systems(&mut self, state: &mut State) {
58        let phases_in_order = [
59            UpdatePhase::First,
60            UpdatePhase::PreUpdate,
61            UpdatePhase::Update,
62            UpdatePhase::PostUpdate,
63        ];
64
65        for phase in &phases_in_order {
66            let schedule = self.schedules.get(phase).unwrap();
67
68            schedule.run_systems(state);
69        }
70    }
71}