shadowengine2d 2.0.0

A comprehensive 2D game engine built in Rust with ECS, rendering, audio, assets, animations, and scene management
Documentation
/*
 * MIT License
 * 
 * Copyright (c) 2025 ShadowEngine2D
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

use crate::{engine::Engine, resources::Resources, EngineResult};

pub trait System {
    fn run(&mut self, engine: &mut Engine, resources: &mut Resources) -> EngineResult<()>;
}

pub enum SystemSet {
    Startup(Vec<Box<dyn System>>),
    Update(Vec<Box<dyn System>>),
    Render(Vec<Box<dyn System>>),
}

impl<F> System for F
where
    F: FnMut(&mut Engine, &mut Resources) -> EngineResult<()>,
{
    fn run(&mut self, engine: &mut Engine, resources: &mut Resources) -> EngineResult<()> {
        self(engine, resources)
    }
}

#[macro_export]
macro_rules! system_set {
    (startup: [$($system:expr),* $(,)?]) => {
        $crate::systems::SystemSet::Startup(vec![$(Box::new($system)),*])
    };
    (update: [$($system:expr),* $(,)?]) => {
        $crate::systems::SystemSet::Update(vec![$(Box::new($system)),*])
    };
    (render: [$($system:expr),* $(,)?]) => {
        $crate::systems::SystemSet::Render(vec![$(Box::new($system)),*])
    };
}

pub struct PhysicsSystem;

impl System for PhysicsSystem {
    fn run(&mut self, engine: &mut Engine, _resources: &mut Resources) -> EngineResult<()> {

        let delta_time = engine.time.delta_time();
        engine.entities.update_physics(delta_time);
        Ok(())
    }
}

pub struct ClearScreenSystem {
    pub color: crate::math::Color,
}

impl ClearScreenSystem {
    pub fn new(color: crate::math::Color) -> Self {
        Self { color }
    }
}

impl System for ClearScreenSystem {
    fn run(&mut self, engine: &mut Engine, _resources: &mut Resources) -> EngineResult<()> {
        if let Some(renderer) = engine.renderer_mut() {
            renderer.set_clear_color(self.color);
        }
        Ok(())
    }
}

impl Default for PhysicsSystem {
    fn default() -> Self {
        Self
    }
}