refactors entity spawning

This commit is contained in:
Llywelwyn 2023-07-21 10:58:58 +01:00
parent e72d1bd694
commit 8664a8e066
13 changed files with 286 additions and 83 deletions

View file

@ -4,12 +4,14 @@ use super::{
use rltk::RandomNumberGenerator;
use specs::prelude::*;
mod prefab_levels;
mod prefab_sections;
#[allow(dead_code)]
#[derive(PartialEq, Clone)]
pub enum PrefabMode {
RexLevel { template: &'static str },
Constant { level: prefab_levels::PrefabLevel },
Sectional { section: prefab_sections::PrefabSection },
}
pub struct PrefabBuilder {
@ -19,6 +21,7 @@ pub struct PrefabBuilder {
history: Vec<Map>,
mode: PrefabMode,
spawns: Vec<(usize, String)>,
previous_builder: Option<Box<dyn MapBuilder>>,
}
impl MapBuilder for PrefabBuilder {
@ -54,14 +57,15 @@ impl MapBuilder for PrefabBuilder {
impl PrefabBuilder {
#[allow(dead_code)]
pub fn new(new_depth: i32) -> PrefabBuilder {
pub fn new(new_depth: i32, previous_builder: Option<Box<dyn MapBuilder>>) -> PrefabBuilder {
PrefabBuilder {
map: Map::new(new_depth),
starting_position: Position { x: 0, y: 0 },
depth: new_depth,
history: Vec::new(),
mode: PrefabMode::Constant { level: prefab_levels::WFC_POPULATED },
mode: PrefabMode::Sectional { section: prefab_sections::UNDERGROUND_FORT },
spawns: Vec::new(),
previous_builder,
}
}
@ -69,6 +73,7 @@ impl PrefabBuilder {
match self.mode {
PrefabMode::RexLevel { template } => self.load_rex_map(&template),
PrefabMode::Constant { level } => self.load_ascii_map(&level),
PrefabMode::Sectional { section } => self.apply_sectional(&section, rng),
}
self.take_snapshot();
@ -92,6 +97,47 @@ impl PrefabBuilder {
}
}
pub fn apply_sectional(&mut self, section: &prefab_sections::PrefabSection, rng: &mut RandomNumberGenerator) {
// Build the map
let prev_builder = self.previous_builder.as_mut().unwrap();
prev_builder.build_map(rng);
self.starting_position = prev_builder.get_starting_pos();
self.map = prev_builder.get_map().clone();
self.take_snapshot();
use prefab_sections::*;
let string_vec = PrefabBuilder::read_ascii_to_vec(section.template);
// Place the new section
let chunk_x;
match section.placement.0 {
HorizontalPlacement::Left => chunk_x = 0,
HorizontalPlacement::Center => chunk_x = (self.map.width / 2) - (section.width as i32 / 2),
HorizontalPlacement::Right => chunk_x = (self.map.width - 1) - section.width as i32,
}
let chunk_y;
match section.placement.1 {
VerticalPlacement::Top => chunk_y = 0,
VerticalPlacement::Center => chunk_y = (self.map.height / 2) - (section.height as i32 / 2),
VerticalPlacement::Bottom => chunk_y = (self.map.height - 1) - section.height as i32,
}
println!("{},{}", chunk_x, chunk_y);
let mut i = 0;
for ty in 0..section.height {
for tx in 0..section.width {
if tx < self.map.width as usize && ty < self.map.height as usize {
let idx = self.map.xy_idx(tx as i32 + chunk_x, ty as i32 + chunk_y);
self.char_to_map(string_vec[i], idx);
}
i += 1;
}
}
self.take_snapshot();
}
fn char_to_map(&mut self, ch: char, idx: usize) {
match ch {
' ' => self.map.tiles[idx] = TileType::Floor,
@ -105,23 +151,23 @@ impl PrefabBuilder {
}
'g' => {
self.map.tiles[idx] = TileType::Floor;
self.spawns.push((idx, "Goblin".to_string()));
self.spawns.push((idx, "goblin".to_string()));
}
'o' => {
self.map.tiles[idx] = TileType::Floor;
self.spawns.push((idx, "Orc".to_string()));
self.spawns.push((idx, "orc".to_string()));
}
'^' => {
self.map.tiles[idx] = TileType::Floor;
self.spawns.push((idx, "Bear Trap".to_string()));
self.spawns.push((idx, "bear trap".to_string()));
}
'%' => {
self.map.tiles[idx] = TileType::Floor;
self.spawns.push((idx, "Rations".to_string()));
self.spawns.push((idx, "rations".to_string()));
}
'!' => {
self.map.tiles[idx] = TileType::Floor;
self.spawns.push((idx, "Health Potion".to_string()));
self.spawns.push((idx, "health potion".to_string()));
}
_ => {
rltk::console::log(format!("Unknown glyph loading map: {}", (ch as u8) as char));