random map regeneration
This commit is contained in:
+41
-16
@@ -1,10 +1,13 @@
|
||||
use crate::collision::Collider;
|
||||
use crate::{collision::Collider, player::reset_player};
|
||||
use bevy::prelude::*;
|
||||
use rand::RngExt;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Obstacle;
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct RegenerateObstacles(pub bool);
|
||||
|
||||
fn spawn_obstacle(commands: &mut Commands, position: Vec2, size: Vec2) {
|
||||
commands.spawn((
|
||||
Obstacle,
|
||||
@@ -15,32 +18,54 @@ fn spawn_obstacle(commands: &mut Commands, position: Vec2, size: Vec2) {
|
||||
}
|
||||
|
||||
// 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)),
|
||||
];
|
||||
|
||||
fn spawn_random_obstacles(commands: &mut Commands) {
|
||||
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);
|
||||
let min_x = -960.0;
|
||||
let max_x = 960.0;
|
||||
|
||||
for (position, size) in obstacles {
|
||||
let position = position + Vec2::new(x_offset, y_offset);
|
||||
let mut y = -1000.0;
|
||||
|
||||
spawn_obstacle(&mut commands, position, size);
|
||||
for _ in 0..100 {
|
||||
let obstacle_count = rng.random_range(1..=3);
|
||||
|
||||
for _ in 0..obstacle_count {
|
||||
let width = rng.random_range(100.0..=400.0);
|
||||
let height = rng.random_range(20.0..=200.0);
|
||||
|
||||
let x = rng.random_range((min_x + width / 2.0)..=(max_x - width / 2.0));
|
||||
|
||||
spawn_obstacle(commands, Vec2::new(x, y), Vec2::new(width, height));
|
||||
}
|
||||
|
||||
y -= rng.random_range(80.0..=160.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn regenerate_obstacles(
|
||||
mut commands: Commands,
|
||||
obstacles: Query<Entity, With<Obstacle>>,
|
||||
mut regenerate_obstacles: ResMut<RegenerateObstacles>,
|
||||
) {
|
||||
if regenerate_obstacles.0 {
|
||||
for obstacle in obstacles {
|
||||
commands.entity(obstacle).despawn();
|
||||
}
|
||||
spawn_random_obstacles(&mut commands);
|
||||
regenerate_obstacles.0 = false;
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_obstacles(mut commands: Commands) {
|
||||
spawn_random_obstacles(&mut commands);
|
||||
}
|
||||
|
||||
pub struct ObstaclePlugin;
|
||||
|
||||
impl Plugin for ObstaclePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, debug_spawn);
|
||||
app.add_systems(Startup, spawn_obstacles)
|
||||
.init_resource::<RegenerateObstacles>()
|
||||
.add_systems(Update, regenerate_obstacles.after(reset_player));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user