55 lines
1.6 KiB
Rust
55 lines
1.6 KiB
Rust
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);
|
|
}
|
|
}
|