test du programme de l'IA Claude pour comprendre le paradigme ECS de bevy
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
5538
Cargo.lock
generated
Normal file
5538
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "ecs-example"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.17.2"
|
||||
fastrand = "2.3.0"
|
||||
54
src/main.rs
Normal file
54
src/main.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, spawn_disk_on_space)
|
||||
.run();
|
||||
}
|
||||
|
||||
// COMPONENT: Marqueur pour identifier nos disques
|
||||
#[derive(Component)]
|
||||
struct Disk;
|
||||
|
||||
// SYSTEM: Configuration initiale
|
||||
fn setup(mut commands: Commands) {
|
||||
// Créer une caméra (ENTITY avec des COMPONENTS)
|
||||
commands.spawn(Camera2d);
|
||||
}
|
||||
|
||||
// SYSTEM: Gère la création de disques quand on appuie sur espace
|
||||
fn spawn_disk_on_space(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
keyboard: Res<ButtonInput<KeyCode>>,
|
||||
) {
|
||||
// Détecte l'appui sur la barre espace
|
||||
if keyboard.just_pressed(KeyCode::Space) {
|
||||
// Position aléatoire
|
||||
let x = (fastrand::f32() * 800.0) - 400.0;
|
||||
let y = (fastrand::f32() * 600.0) - 300.0;
|
||||
|
||||
// Couleur aléatoire (rouge, vert ou bleu)
|
||||
let color = match fastrand::usize(0..3) {
|
||||
0 => Color::srgb(1.0, 0.0, 0.0), // Rouge
|
||||
1 => Color::srgb(0.0, 1.0, 0.0), // Vert
|
||||
_ => Color::srgb(0.0, 0.0, 1.0), // Bleu
|
||||
};
|
||||
|
||||
// Créer un mesh circulaire (disque)
|
||||
let circle = Circle::new(25.0);
|
||||
|
||||
// Créer une nouvelle ENTITY avec des COMPONENTS
|
||||
commands.spawn((
|
||||
Disk, // Notre component marqueur
|
||||
Mesh2d(meshes.add(circle)),
|
||||
MeshMaterial2d(materials.add(ColorMaterial::from(color))),
|
||||
Transform::from_xyz(x, y, 0.0),
|
||||
));
|
||||
|
||||
println!("Disque créé à ({:.0}, {:.0}) - couleur: {:?}", x, y, color);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user