عقلية ECS
الكيانات والمكونات والأنظمة
عقلية ECS درس مجاني في Learn Rust Coding على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Learn Rust Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What ECS Solves
Bevy is built on the Entity Component System (ECS) pattern. Instead of deep inheritance hierarchies, you compose behavior from small, independent pieces of data.
This data-oriented design keeps memory layout cache-friendly and lets the engine parallelize work across CPU cores almost for free.
Entities Are Just IDs
An entity is nothing but a lightweight identifier — a generational index. It owns no data and has no behavior on its own.
Think of it as a key in a database row. Components attached to that key give the entity meaning, like "Player" or "Bullet".
// An Entity is a tiny handle
struct Entity {
index: u32,
generation: u32,
}Components Are Pure Data
Components are plain Rust structs that derive Component. They carry state but contain no logic.
Keep them small and focused. A position, a velocity, and a health value are three separate components rather than one monolithic struct.
use bevy::prelude::*;
#[derive(Component)]
struct Position { x: f32, y: f32 }
#[derive(Component)]
struct Velocity { x: f32, y: f32 }Systems Hold the Logic
Systems are ordinary functions that operate on components. They are where all your game logic lives.
Bevy inspects each system's parameters to figure out what data it touches, then schedules systems that don't conflict to run in parallel.
fn move_system(mut query: Query<(&mut Position, &Velocity)>) {
for (mut pos, vel) in &mut query {
pos.x += vel.x;
pos.y += vel.y;
}
}Queries: Asking for Data
A Query declaratively requests entities that have a specific set of components. Bevy returns only matching entities.
You can request shared access with &T or exclusive access with &mut T. This borrow information drives the parallel scheduler.
// Entities with BOTH Position and Velocity
fn report(query: Query<(&Position, &Velocity)>) {
for (pos, vel) in &query {
info!("at {},{} moving {},{}", pos.x, pos.y, vel.x, vel.y);
}
}Filtering Queries
The second type parameter of a Query is a filter. Use With and Without to narrow results without borrowing that component's data.
This is ideal for tag components — empty structs that mark an entity's role.
#[derive(Component)]
struct Player;
fn player_only(query: Query<&Position, With<Player>>) {
for pos in &query {
info!("player at {},{}", pos.x, pos.y);
}
}Resources: Global State
Some data is unique to the whole game — a score, the elapsed time, or game settings. These live as resources rather than components.
Access them in systems with Res<T> for read access or ResMut<T> for write access.
#[derive(Resource)]
struct Score(u32);
fn show_score(score: Res<Score>) {
info!("Score: {}", score.0);
}The App Builder
Everything is wired together in the App. You add plugins, insert resources, and register systems against a schedule.
DefaultPlugins brings windowing, rendering, input, and time so you can focus on gameplay.
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(Score(0))
.add_systems(Update, show_score)
.run();
}Schedules: Startup vs Update
Systems run inside schedules. Startup runs once when the app launches — perfect for spawning the world.
Update runs every frame and is where movement, input, and logic belong. Bevy also offers FixedUpdate for deterministic physics steps.
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, move_system)
.run();Why Composition Wins
Want a flying enemy that can be poisoned? Just attach Flying and Poisoned components. No new class, no inheritance chain.
Behavior emerges from which systems match which component combinations. This makes features additive and refactors cheap.
// Same systems, new combinations
commands.spawn((Position{x:0.0,y:0.0}, Velocity{x:1.0,y:0.0}, Flying, Poisoned));Parallelism for Free
Because each system declares its data access, Bevy's scheduler knows which systems conflict. Non-conflicting systems run on different threads automatically.
You write single-threaded-looking code, and the engine extracts parallelism from your borrow declarations. No locks, no manual threading.
Quick Check
Test your understanding of ECS roles.
Recap: The ECS Mindset
Entities are IDs, components are data, systems are logic, and resources are global state. The App ties them together through schedules.
By composing small components and letting systems match them, you get flexible, parallel, cache-friendly games. Next we'll spawn entities and make them move.
الأسئلة الشائعة
هل درس «عقلية ECS» مجاني؟
نعم — نص درس «عقلية ECS» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Learn Rust Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.
ماذا ستتعلم في «عقلية ECS»؟
الكيانات والمكونات والأنظمة تتمرن على Learn Rust Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟
لا تُشترط خبرة سابقة. Learn Rust Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «عقلية ECS»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Learn Rust Coding هذا؟
نعم. كل درس في Learn Rust Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.