random map regeneration
This commit is contained in:
+4
-5
@@ -15,7 +15,7 @@ pub struct Collider {
|
|||||||
pub struct HasCollided(pub bool);
|
pub struct HasCollided(pub bool);
|
||||||
|
|
||||||
// Check collision with Player
|
// Check collision with Player
|
||||||
fn check_collision(
|
pub fn check_collision_with_player(
|
||||||
mut player: Single<(&Transform, &Collider, &mut Sprite), With<Player>>,
|
mut player: Single<(&Transform, &Collider, &mut Sprite), With<Player>>,
|
||||||
obstacle: Query<(&Transform, &Collider), With<Obstacle>>,
|
obstacle: Query<(&Transform, &Collider), With<Obstacle>>,
|
||||||
mut has_collided: ResMut<HasCollided>,
|
mut has_collided: ResMut<HasCollided>,
|
||||||
@@ -24,7 +24,7 @@ fn check_collision(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (player_transform, player_collider, mut player_sprite) = player.into_inner();
|
let (player_transform, player_collider, mut _player_sprite) = player.into_inner();
|
||||||
|
|
||||||
// Check whether or not each obstacles overlap the player
|
// Check whether or not each obstacles overlap the player
|
||||||
for (collider_transform, collider_collider) in obstacle {
|
for (collider_transform, collider_collider) in obstacle {
|
||||||
@@ -34,8 +34,7 @@ fn check_collision(
|
|||||||
collider_transform.translation.truncate(),
|
collider_transform.translation.truncate(),
|
||||||
collider_collider.size / 2.0,
|
collider_collider.size / 2.0,
|
||||||
) {
|
) {
|
||||||
// println!("Collision!!!!!!!");
|
// player_sprite.color = Color::srgb(1.0, 0.0, 0.0); // Change sprite color
|
||||||
player_sprite.color = Color::srgb(1.0, 0.0, 0.0);
|
|
||||||
has_collided.0 = true;
|
has_collided.0 = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -54,6 +53,6 @@ pub struct CollisionPlugin;
|
|||||||
impl Plugin for CollisionPlugin {
|
impl Plugin for CollisionPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.init_resource::<HasCollided>()
|
app.init_resource::<HasCollided>()
|
||||||
.add_systems(Update, check_collision.after(move_player));
|
.add_systems(Update, check_collision_with_player.after(move_player));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+41
-16
@@ -1,10 +1,13 @@
|
|||||||
use crate::collision::Collider;
|
use crate::{collision::Collider, player::reset_player};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use rand::RngExt;
|
use rand::RngExt;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Obstacle;
|
pub struct Obstacle;
|
||||||
|
|
||||||
|
#[derive(Resource, Default)]
|
||||||
|
pub struct RegenerateObstacles(pub bool);
|
||||||
|
|
||||||
fn spawn_obstacle(commands: &mut Commands, position: Vec2, size: Vec2) {
|
fn spawn_obstacle(commands: &mut Commands, position: Vec2, size: Vec2) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Obstacle,
|
Obstacle,
|
||||||
@@ -15,32 +18,54 @@ fn spawn_obstacle(commands: &mut Commands, position: Vec2, size: Vec2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Temporary fonction for display random obstacles
|
// Temporary fonction for display random obstacles
|
||||||
fn debug_spawn(mut commands: Commands) {
|
fn spawn_random_obstacles(commands: &mut 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();
|
let mut rng = rand::rng();
|
||||||
|
|
||||||
for i in 0..20 {
|
let min_x = -960.0;
|
||||||
let y_offset = i as f32 * -300.0;
|
let max_x = 960.0;
|
||||||
let x_offset = rng.random_range(-400.0..=400.0);
|
|
||||||
|
|
||||||
for (position, size) in obstacles {
|
let mut y = -1000.0;
|
||||||
let position = position + Vec2::new(x_offset, y_offset);
|
|
||||||
|
|
||||||
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;
|
pub struct ObstaclePlugin;
|
||||||
|
|
||||||
impl Plugin for ObstaclePlugin {
|
impl Plugin for ObstaclePlugin {
|
||||||
fn build(&self, app: &mut App) {
|
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