shadow_engine_2d 2.0.1

A modern, high-performance 2D game engine built in Rust with ECS, physics, particles, audio, and more
Documentation
use shadow_engine_2d::prelude::*;
use winit::keyboard::KeyCode;

fn main() {
    env_logger::init();

    let mut engine = Engine::builder()
        .title("Shadow Engine 2D - Physics Demo")
        .size(1280, 720)
        .build();

    let physics = PhysicsSystem::new();

    // Spawn player with physics
    let player = engine.world_mut().spawn();
    if let Some(entity) = engine.world_mut().entity_mut(player) {
        entity.add(Transform::new(0.0, -200.0));
        entity.add(Sprite::new(50.0, 50.0).with_color(Color::CYAN));
        entity.add(RigidBody::dynamic().with_drag(0.5));
        entity.add(Collider::box_collider(50.0, 50.0));
    }

    // Spawn ground platforms
    let platforms = vec![
        (-400.0, 250.0, 200.0, 30.0),
        (-100.0, 150.0, 200.0, 30.0),
        (200.0, 50.0, 200.0, 30.0),
        (-200.0, -50.0, 600.0, 30.0),
    ];

    for (x, y, w, h) in platforms {
        let platform = engine.world_mut().spawn();
        if let Some(entity) = engine.world_mut().entity_mut(platform) {
            entity.add(Transform::new(x, y));
            entity.add(Sprite::new(w, h).with_color(Color::GREEN));
            entity.add(Collider::box_collider(w, h));
        }
    }

    // Spawn some dynamic boxes
    for i in 0..3 {
        let box_entity = engine.world_mut().spawn();
        if let Some(entity) = engine.world_mut().entity_mut(box_entity) {
            entity.add(Transform::new(-300.0 + i as f32 * 100.0, -300.0));
            entity.add(Sprite::new(40.0, 40.0).with_color(Color::RED));
            entity.add(RigidBody::dynamic().with_mass(2.0).with_drag(0.3));
            entity.add(Collider::box_collider(40.0, 40.0));
        }
    }

    // Spawn walls
    let walls = vec![
        (-640.0, 0.0, 20.0, 720.0), // Left wall
        (640.0, 0.0, 20.0, 720.0),  // Right wall
        (0.0, 360.0, 1280.0, 20.0), // Bottom
    ];

    for (x, y, w, h) in walls {
        let wall = engine.world_mut().spawn();
        if let Some(entity) = engine.world_mut().entity_mut(wall) {
            entity.add(Transform::new(x, y));
            entity.add(Sprite::new(w, h).with_color(Color::rgb(0.3, 0.3, 0.3)));
            entity.add(Collider::box_collider(w, h));
        }
    }

    engine.run(move |world, input, time| {
        let dt = time.delta();
        let move_force = 5000.0;
        let jump_impulse = 400.0;

        // Player controls
        for entity in world.entities_mut() {
            if let Some(sprite) = entity.get::<Sprite>() {
                if sprite.color.b > 0.9 && sprite.color.r < 0.1 { // Cyan player
                    if let Some(body) = entity.get_mut::<RigidBody>() {
                        // Horizontal movement
                        if input.key_pressed(KeyCode::KeyA) || input.key_pressed(KeyCode::ArrowLeft) {
                            body.apply_force(Vec2::new(-move_force, 0.0));
                        }
                        if input.key_pressed(KeyCode::KeyD) || input.key_pressed(KeyCode::ArrowRight) {
                            body.apply_force(Vec2::new(move_force, 0.0));
                        }

                        // Jump
                        if input.key_just_pressed(KeyCode::Space) || input.key_just_pressed(KeyCode::KeyW) {
                            body.apply_impulse(Vec2::new(0.0, -jump_impulse));
                        }
                    }
                }
            }
        }

        // Update physics
        physics.update(world, dt);

        // Detect and resolve collisions
        let collisions = physics.detect_collisions(world);
        physics.resolve_collisions(world, &collisions);
    });
}