limnus_system_runner/
lib.rs

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
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/limnus
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
mod schedule;

use crate::schedule::Schedule;
use std::collections::HashMap;
use limnus_system::{IntoSystem, SystemParam};
use limnus_system_state::State;

#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
pub enum UpdatePhase {
    First,
    PreUpdate,
    Update,
    PostUpdate,
}

#[derive(Default)]
pub struct Runner {
    schedules: HashMap<UpdatePhase, Schedule>,
}

impl Runner {}

impl Runner {
    pub fn new() -> Self {
        let phases_in_order = [
            UpdatePhase::First,
            UpdatePhase::PreUpdate,
            UpdatePhase::Update,
            UpdatePhase::PostUpdate,
        ];

        let mut schedules = HashMap::default();

        for phase in phases_in_order {
            schedules.insert(phase, Schedule::new());
        }

        Self { schedules }
    }

    pub fn add_system<F, Params>(&mut self, update_phase: UpdatePhase, system: F)
    where
        F: IntoSystem<Params>,
        Params: SystemParam,
    {
        self.schedules
            .get_mut(&update_phase)
            .expect("tried to add to unknown phase")
            .add_system(system);
    }

    pub fn run_systems(&mut self, state: &mut State) {
        let phases_in_order = [
            UpdatePhase::First,
            UpdatePhase::PreUpdate,
            UpdatePhase::Update,
            UpdatePhase::PostUpdate,
        ];

        for phase in &phases_in_order {
            let schedule = self.schedules.get(phase).unwrap();

            schedule.run_systems(state);
        }
    }
}