Introduction to ECS (Entity Component System) in Rust with Bevy

ECS in Bevy separates game objects into entities (IDs), components (data), and systems (logic) for efficient game development.

ECS (Entity-Component-System) is an architecture where entities are IDs, components are data structs, and systems are functions that operate on entities with specific components. In Bevy, you define components as #[derive(Component)] structs, create entities by spawning them with components, and write systems as functions that query and mutate those components.

use bevy::prelude::*;

#[derive(Component)]
struct Velocity { x: f32, y: f32 }

#[derive(Component)]
struct Position { x: f32, y: f32 }

fn move_system(
    mut query: Query<(&mut Position, &Velocity)>,
) {
    for (mut pos, vel) in query.iter_mut() {
        pos.x += vel.x;
        pos.y += vel.y;
    }
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Update, move_system)
        .run();
}