roguelike-game/src/spawner.rs

151 lines
4.1 KiB
Rust
Raw Normal View History

2021-12-10 20:16:48 -05:00
use std::collections::HashMap;
use rltk::{RandomNumberGenerator, RGB};
use specs::prelude::*;
2021-11-08 13:58:40 -05:00
use specs::saveload::{MarkedBuilder, SimpleMarker};
2021-12-10 20:16:48 -05:00
use crate::components::*;
use crate::random_table::RandomTable;
use crate::raws::{spawn_named_entity, SpawnType, RAWS};
2021-12-10 20:16:48 -05:00
use crate::{Map, Rect, TileType};
/// Spawns the player and returns their entity object
pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
ecs.create_entity()
.with(Position {
x: player_x,
y: player_y,
})
.with(Renderable {
glyph: rltk::to_cp437('@'),
fg: RGB::named(rltk::YELLOW),
bg: RGB::named(rltk::BLACK),
2021-11-04 09:54:38 -04:00
render_order: 0,
})
.with(Player {})
2021-11-05 10:41:47 -04:00
.with(Viewshed::default())
.with(Name::from("Player"))
.with(CombatStats {
max_hp: 30,
hp: 30,
defense: 2,
power: 5,
})
2021-11-17 16:23:01 -05:00
.with(HungerClock {
state: HungerState::WellFed,
duration: 20,
})
2021-11-08 13:58:40 -05:00
.marked::<SimpleMarker<SerializeMe>>()
.build()
}
const MAX_MONSTERS: i32 = 4;
2021-11-12 14:12:15 -05:00
fn room_table(map_depth: i32) -> RandomTable {
RandomTable::new()
.add("Goblin", 10)
2021-11-12 14:12:15 -05:00
.add("Orc", 1 + map_depth)
.add("Health Potion", 7)
2021-11-12 14:12:15 -05:00
.add("Fireball Scroll", 2 + map_depth)
.add("Confusion Scroll", 2 + map_depth)
.add("Magic Missile Scroll", 4)
2021-11-15 09:19:22 -05:00
.add("Dagger", 3)
.add("Shield", 3)
2021-11-15 11:48:01 -05:00
.add("Longsword", map_depth - 1)
.add("Tower Shield", map_depth - 1)
.add("Rations", 10)
.add("Magic Mapping Scroll", 2)
2021-11-29 16:00:07 -05:00
.add("Bear Trap", 2)
}
/// fills a room with stuff!
#[allow(clippy::map_entry)]
pub fn spawn_room(
map: &Map,
rng: &mut RandomNumberGenerator,
room: &Rect,
map_depth: i32,
spawn_list: &mut Vec<(usize, String)>,
) {
let mut possible_targets: Vec<usize> = Vec::new();
// Borrow scope - to keep access to the map separated
{
for y in room.y1 + 1..room.y2 {
for x in room.x1 + 1..room.x2 {
let idx = map.xy_idx(x, y);
if map.tiles[idx] == TileType::Floor {
possible_targets.push(idx);
}
}
}
}
spawn_region(map, rng, &possible_targets, map_depth, spawn_list);
}
pub fn spawn_region(
2021-12-14 14:15:22 -05:00
_map: &Map,
rng: &mut RandomNumberGenerator,
area: &[usize],
map_depth: i32,
spawn_list: &mut Vec<(usize, String)>,
) {
2021-11-12 14:12:15 -05:00
let spawn_table = room_table(map_depth);
let mut spawn_points: HashMap<usize, String> = HashMap::new();
let mut areas: Vec<usize> = Vec::from(area);
// Scope to keep the borrow checker happy
{
let num_spawns = i32::min(
areas.len() as i32,
rng.roll_dice(1, MAX_MONSTERS + 3) + (map_depth - 1) - 3,
);
if num_spawns == 0 {
return;
}
for _i in 0..num_spawns {
let array_index = if areas.len() == 1 {
0_usize
} else {
(rng.roll_dice(1, areas.len() as i32) - 1) as usize
};
let map_idx = areas[array_index];
spawn_points.insert(map_idx, spawn_table.roll(rng));
areas.remove(array_index);
}
}
// Actually spawn the monsters
for spawn in spawn_points.iter() {
spawn_list.push((*spawn.0, spawn.1.to_string()));
}
}
/// Spawns a named entity (name in tuple.1) at the location in (tuple.0)
2021-12-10 14:29:03 -05:00
pub fn spawn_entity(ecs: &mut World, spawn: &(&usize, &String)) {
let map = ecs.fetch::<Map>();
let width = map.width as usize;
let x = (*spawn.0 % width) as i32;
let y = (*spawn.0 / width) as i32;
// Drop this map reference to make the borrow checker happy
std::mem::drop(map);
let item_result = spawn_named_entity(
2021-12-23 11:38:37 -05:00
&RAWS.lock().unwrap(),
ecs.create_entity(),
spawn.1,
2021-12-23 11:38:37 -05:00
SpawnType::AtPosition { x, y },
);
if item_result.is_some() {
return;
}
rltk::console::log(format!(
"WARNING: We don't know how to spawn [{}]!",
spawn.1
));
2021-12-17 13:53:14 -05:00
}