Obstacle with collision

This commit is contained in:
2026-09-17 19:42:23 +02:00
parent 89c565334c
commit 6d666f2a3c
+42
View File
@@ -1,4 +1,46 @@
use crate::collision::Collider;
use bevy::prelude::*;
use rand::RngExt;
#[derive(Component)]
pub struct Obstacle;
fn spawn_obstacle(commands: &mut Commands, position: Vec2, size: Vec2) {
commands.spawn((
Obstacle,
Collider { size },
Transform::from_xyz(position.x, position.y, 1.0),
Sprite::from_color(Color::srgb(1.0, 0.5, 0.1), size),
));
}
// Temporary fonction for display random obstacles
fn debug_spawn(mut commands: Commands) {
let obstacles = [
(Vec2::new(-400.0, -950.0), Vec2::new(200.0, 32.0)),
(Vec2::new(100.0, -1050.0), Vec2::new(300.0, 32.0)),
(Vec2::new(-200.0, -1200.0), Vec2::new(100.0, 100.0)),
(Vec2::new(300.0, -1350.0), Vec2::new(250.0, 32.0)),
];
let mut rng = rand::rng();
for i in 0..20 {
let y_offset = i as f32 * -300.0;
let x_offset = rng.random_range(-400.0..=400.0);
for (position, size) in obstacles {
let position = position + Vec2::new(x_offset, y_offset);
spawn_obstacle(&mut commands, position, size);
}
}
}
pub struct ObstaclePlugin;
impl Plugin for ObstaclePlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, debug_spawn);
}
}