Use a more lenient asteroid spawning strategy

Two large asteroids are created to start.
New asteroids are only created when the total asteroid area is below 12.
Large asteroids have 4 area, medium asteroids have 2, and small
asteroids have 1.
Asteroid true sizes are based on multiples of a base number.
This commit is contained in:
Leonora Tindall 2023-04-03 22:36:06 -05:00
parent 154517654e
commit 36da3483ae
2 changed files with 26 additions and 8 deletions

View File

@ -1,6 +1,6 @@
use crate::{HEIGHT, WIDTH};
use macroquad::{prelude::*, rand::gen_range};
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq)]
pub enum AsteroidSize {
Large,
Medium,
@ -19,12 +19,14 @@ pub struct Asteroid {
pub alive: bool,
}
const BASE_SIZE: f32 = 20.;
impl Asteroid {
pub fn new(size: AsteroidSize) -> Self {
let (sides, radius) = match size {
AsteroidSize::Large => (gen_range(6, 10), gen_range(50., 65.)),
AsteroidSize::Medium => (gen_range(5, 6), gen_range(35., 50.)),
AsteroidSize::Small => (gen_range(3, 5), 25.),
AsteroidSize::Large => (gen_range(6, 10), BASE_SIZE * 4.),
AsteroidSize::Medium => (gen_range(5, 6), BASE_SIZE * 2.),
AsteroidSize::Small => (gen_range(3, 5), BASE_SIZE),
};
let mut r = vec2(
if gen_range(0., 1.) > 0.5 { -1. } else { 1. },

View File

@ -29,9 +29,6 @@ impl World {
asteroids: vec![
Asteroid::new_to(vec2(0., 0.), 1.5, AsteroidSize::Large),
Asteroid::new(AsteroidSize::Large),
Asteroid::new(AsteroidSize::Large),
Asteroid::new(AsteroidSize::Large),
Asteroid::new(AsteroidSize::Large),
],
..Default::default()
}
@ -115,7 +112,26 @@ impl World {
// }) < self.max_asteroids
// || self.player.lifespan % 200 == 0
// {
if self.player.lifespan % 200 == 0 {
let num_small: usize = self
.asteroids
.iter()
.filter(|a| a.size == AsteroidSize::Small)
.map(|_| 1)
.sum();
let num_medium: usize = self
.asteroids
.iter()
.filter(|a| a.size == AsteroidSize::Medium)
.map(|_| 1)
.sum();
let num_large: usize = self
.asteroids
.iter()
.filter(|a| a.size == AsteroidSize::Large)
.map(|_| 1)
.sum();
let area = num_small + num_medium * 2 + num_large * 4;
if self.player.lifespan % 200 == 0 && area < 12 {
self.asteroids
.push(Asteroid::new_to(self.player.pos, 1.5, AsteroidSize::Large));
}