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::{
    entity::{EntityId, EntityManager, Transform, Sprite, Velocity, Component},
    math::{Position, Size, Color},
};

pub struct EntityBuilder<'a> {
    manager: &'a mut EntityManager,
    entity_id: EntityId,
}

impl<'a> EntityBuilder<'a> {
    pub fn new(manager: &'a mut EntityManager) -> Self {
        let entity_id = manager.create_entity();
        Self { manager, entity_id }
    }

    pub fn with_transform(self, transform: Transform) -> Self {
        self.manager.add_transform(self.entity_id, transform);
        self
    }

    pub fn with_sprite(self, sprite: Sprite) -> Self {
        self.manager.add_sprite(self.entity_id, sprite);
        self
    }

    pub fn with_velocity(self, velocity: Velocity) -> Self {
        self.manager.add_velocity(self.entity_id, velocity);
        self
    }

    pub fn at_position(self, x: f32, y: f32) -> Self {
        let transform = Transform::new(Position::new(x, y));
        self.with_transform(transform)
    }

    pub fn with_colored_square(self, color: Color, size: f32) -> Self {
        let sprite = Sprite::colored_square(color, size);
        self.with_sprite(sprite)
    }

    pub fn with_movement(self, vel_x: f32, vel_y: f32) -> Self {
        let velocity = Velocity::new(vel_x, vel_y);
        self.with_velocity(velocity)
    }

    pub fn build(self) -> EntityId {
        self.entity_id
    }
}

pub trait EntityManagerExt {
    fn spawn(&mut self) -> EntityBuilder;
}

impl EntityManagerExt for EntityManager {
    fn spawn(&mut self) -> EntityBuilder {
        EntityBuilder::new(self)
    }
}

impl<'a> EntityBuilder<'a> {

    pub fn game_object(self, position: Position, sprite: Sprite) -> Self {
        self.with_transform(Transform::new(position))
            .with_sprite(sprite)
    }

    pub fn moving_object(self, position: Position, velocity: Velocity, sprite: Sprite) -> Self {
        self.with_transform(Transform::new(position))
            .with_velocity(velocity)
            .with_sprite(sprite)
    }

    pub fn ball(self, x: f32, y: f32, vel_x: f32, vel_y: f32, color: Color, size: f32) -> Self {
        self.at_position(x, y)
            .with_movement(vel_x, vel_y)
            .with_colored_square(color, size)
    }
}