use crate::zombies::{Zombie, ZombieType};
use crate::entities::sun::{Sun, SunType};
use crate::mechanics::level_controller::{LevelController, ZombieSpawnInfo};
use rand::Rng;
pub struct EntityManager {
pub level_controller: LevelController,
fallen_sun_count: u32,
next_sun_countdown: u32,
}
impl EntityManager {
pub fn new() -> Self {
let initial_countdown = 425 + rand::thread_rng().gen_range(0..=275);
EntityManager {
level_controller: LevelController::new(),
fallen_sun_count: 0,
next_sun_countdown: initial_countdown,
}
}
pub fn update(&mut self, dt: u64, zombies: &[Zombie]) -> Vec<ZombieSpawnInfo> {
self.level_controller.update(dt, zombies)
}
pub fn check_natural_sun_spawn(&mut self, dt: u64) -> bool {
let dt_centiseconds = dt * 100 / 1000;
self.should_spawn_natural_sun(dt_centiseconds)
}
pub fn spawn_zombie(&self, zombie_type: ZombieType, row: usize) -> Zombie {
Zombie::new(zombie_type, row)
}
pub fn spawn_natural_sun(&self) -> Sun {
let x = rand::thread_rng().gen_range(50.0..750.0);
let y = rand::thread_rng().gen_range(30.0..100.0);
Sun::new(x, y, SunType::NaturalGeneration)
}
pub fn should_spawn_natural_sun(&mut self, dt: u64) -> bool {
if self.next_sun_countdown <= dt as u32 {
self.fallen_sun_count += 1;
let base_time = std::cmp::min(self.fallen_sun_count * 10 + 425, 950);
let random_addition = rand::thread_rng().gen_range(0..=275);
self.next_sun_countdown = base_time + random_addition;
true
} else {
self.next_sun_countdown -= dt as u32;
false
}
}
}