IntervalSpawnSystem{} now works with mob groupsize flags
This commit is contained in:
parent
60fe39c834
commit
75e17f235d
4 changed files with 67 additions and 36 deletions
|
|
@ -31,28 +31,48 @@ pub fn try_spawn_interval(ecs: &mut World) {
|
|||
|
||||
fn spawn_random_mob_in_free_nonvisible_tile(ecs: &mut World) {
|
||||
let map = ecs.fetch::<Map>();
|
||||
let available_tiles = populate_unblocked_nonvisible_tiles(&map);
|
||||
let difficulty = (map.difficulty + gamelog::get_event_count("player_level")) / 2;
|
||||
let mut available_tiles = populate_unblocked_nonvisible(&map);
|
||||
let player_level = gamelog::get_event_count("player_level");
|
||||
rltk::console::log(player_level);
|
||||
let difficulty = (map.difficulty + player_level) / 2;
|
||||
if available_tiles.len() == 0 {
|
||||
if LOG_SPAWNING {
|
||||
rltk::console::log("SPAWNINFO: No free tiles; not spawning anything..");
|
||||
}
|
||||
return;
|
||||
}
|
||||
let mut spawn_locations: Vec<(i32, i32)> = Vec::new();
|
||||
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
||||
let idx = get_random_idx_from_possible_tiles(&mut rng, available_tiles);
|
||||
// Get mob type
|
||||
let key = spawner::mob_table(difficulty).roll(&mut rng);
|
||||
let x = idx as i32 % map.width;
|
||||
let y = idx as i32 / map.width;
|
||||
// Check if it spawns in a group, and roll for how many to spawn accordingly.
|
||||
let spawn_type = raws::get_mob_spawn_type(&raws::RAWS.lock().unwrap(), &key);
|
||||
let roll = raws::get_mob_spawn_amount(&mut rng, &spawn_type, player_level);
|
||||
// Get that many idxs, and push them to the spawn list.
|
||||
for _i in 0..roll {
|
||||
let idx = get_random_idx_from_tiles(&mut rng, &mut available_tiles);
|
||||
spawn_locations.push((idx as i32 % map.width, idx as i32 / map.width));
|
||||
}
|
||||
// Dropping resources for borrow-checker.
|
||||
std::mem::drop(map);
|
||||
std::mem::drop(rng);
|
||||
if LOG_SPAWNING {
|
||||
rltk::console::log(format!("SPAWNINFO: Spawning {} at {}, {}.", key, x, y));
|
||||
// For every idx in the spawn list, spawn mob.
|
||||
for idx in spawn_locations {
|
||||
if LOG_SPAWNING {
|
||||
rltk::console::log(format!("SPAWNINFO: Spawning {} at {}, {}.", key, idx.0, idx.1));
|
||||
}
|
||||
raws::spawn_named_entity(
|
||||
&raws::RAWS.lock().unwrap(),
|
||||
ecs,
|
||||
&key,
|
||||
raws::SpawnType::AtPosition { x: idx.0, y: idx.1 },
|
||||
difficulty,
|
||||
);
|
||||
}
|
||||
raws::spawn_named_entity(&raws::RAWS.lock().unwrap(), ecs, &key, raws::SpawnType::AtPosition { x, y }, difficulty);
|
||||
}
|
||||
|
||||
fn populate_unblocked_nonvisible_tiles(map: &Map) -> Vec<usize> {
|
||||
/// Returns a Vec<usize> of every tile that is not blocked, and is not currently in the player's view.
|
||||
fn populate_unblocked_nonvisible(map: &Map) -> Vec<usize> {
|
||||
let mut tiles: Vec<usize> = Vec::new();
|
||||
for (i, _tile) in map.tiles.iter().enumerate() {
|
||||
if !map.blocked[i] && !map.visible_tiles[i] {
|
||||
|
|
@ -62,7 +82,9 @@ fn populate_unblocked_nonvisible_tiles(map: &Map) -> Vec<usize> {
|
|||
return tiles;
|
||||
}
|
||||
|
||||
fn get_random_idx_from_possible_tiles(rng: &mut rltk::RandomNumberGenerator, area: Vec<usize>) -> usize {
|
||||
/// Picks a random index from a vector of indexes, and removes it from the vector.
|
||||
fn get_random_idx_from_tiles(rng: &mut rltk::RandomNumberGenerator, area: &mut Vec<usize>) -> usize {
|
||||
let idx = if area.len() == 1 { 0usize } else { (rng.roll_dice(1, area.len() as i32) - 1) as usize };
|
||||
area.remove(idx);
|
||||
return area[idx];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue