shadowengine2d/
systems.rs

1/*
2 * MIT License
3 * 
4 * Copyright (c) 2025 ShadowEngine2D
5 * 
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 * 
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 * 
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25use crate::{engine::Engine, resources::Resources, EngineResult};
26
27pub trait System {
28    fn run(&mut self, engine: &mut Engine, resources: &mut Resources) -> EngineResult<()>;
29}
30
31pub enum SystemSet {
32    Startup(Vec<Box<dyn System>>),
33    Update(Vec<Box<dyn System>>),
34    Render(Vec<Box<dyn System>>),
35}
36
37impl<F> System for F
38where
39    F: FnMut(&mut Engine, &mut Resources) -> EngineResult<()>,
40{
41    fn run(&mut self, engine: &mut Engine, resources: &mut Resources) -> EngineResult<()> {
42        self(engine, resources)
43    }
44}
45
46#[macro_export]
47macro_rules! system_set {
48    (startup: [$($system:expr),* $(,)?]) => {
49        $crate::systems::SystemSet::Startup(vec![$(Box::new($system)),*])
50    };
51    (update: [$($system:expr),* $(,)?]) => {
52        $crate::systems::SystemSet::Update(vec![$(Box::new($system)),*])
53    };
54    (render: [$($system:expr),* $(,)?]) => {
55        $crate::systems::SystemSet::Render(vec![$(Box::new($system)),*])
56    };
57}
58
59pub struct PhysicsSystem;
60
61impl System for PhysicsSystem {
62    fn run(&mut self, engine: &mut Engine, _resources: &mut Resources) -> EngineResult<()> {
63
64        let delta_time = engine.time.delta_time();
65        engine.entities.update_physics(delta_time);
66        Ok(())
67    }
68}
69
70pub struct ClearScreenSystem {
71    pub color: crate::math::Color,
72}
73
74impl ClearScreenSystem {
75    pub fn new(color: crate::math::Color) -> Self {
76        Self { color }
77    }
78}
79
80impl System for ClearScreenSystem {
81    fn run(&mut self, engine: &mut Engine, _resources: &mut Resources) -> EngineResult<()> {
82        if let Some(renderer) = engine.renderer_mut() {
83            renderer.set_clear_color(self.color);
84        }
85        Ok(())
86    }
87}
88
89impl Default for PhysicsSystem {
90    fn default() -> Self {
91        Self
92    }
93}