FillEdges map builder, to fill walkable edges with overmap transition

This commit is contained in:
Llywelwyn 2023-08-27 16:36:33 +01:00
parent 8e3ed5cead
commit 2890c16a3c
2 changed files with 20 additions and 8 deletions

View file

@ -1,8 +1,10 @@
use super::{ BuilderMap, MetaMapBuilder, TileType }; use super::{ BuilderMap, MetaMapBuilder, TileType };
use crate::tile_walkable;
use rltk::RandomNumberGenerator; use rltk::RandomNumberGenerator;
pub struct FillEdges { pub struct FillEdges {
fill_with: TileType, fill_with: TileType,
only_walkable: bool,
} }
impl MetaMapBuilder for FillEdges { impl MetaMapBuilder for FillEdges {
@ -15,21 +17,32 @@ impl MetaMapBuilder for FillEdges {
impl FillEdges { impl FillEdges {
#[allow(dead_code)] #[allow(dead_code)]
pub fn wall() -> Box<FillEdges> { pub fn wall() -> Box<FillEdges> {
return Box::new(FillEdges { fill_with: TileType::Wall }); return Box::new(FillEdges { fill_with: TileType::Wall, only_walkable: false });
}
pub fn overmap_transition(id: i32) -> Box<FillEdges> {
return Box::new(FillEdges { fill_with: TileType::ToOvermap(id), only_walkable: true });
} }
fn fill_edges(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { fn fill_edges(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
// Get map edges as possible points to fill
let mut possible_idxs: Vec<usize> = Vec::new();
for x in 0..build_data.map.width { for x in 0..build_data.map.width {
let mut idx = build_data.map.xy_idx(x, 0); let mut idx = build_data.map.xy_idx(x, 0);
build_data.map.tiles[idx] = self.fill_with; possible_idxs.push(idx);
idx = build_data.map.xy_idx(x, build_data.map.height - 1); idx = build_data.map.xy_idx(x, build_data.map.height - 1);
build_data.map.tiles[idx] = self.fill_with; possible_idxs.push(idx);
} }
for y in 0..build_data.map.height { for y in 0..build_data.map.height {
let mut idx = build_data.map.xy_idx(0, y); let mut idx = build_data.map.xy_idx(0, y);
build_data.map.tiles[idx] = self.fill_with; possible_idxs.push(idx);
idx = build_data.map.xy_idx(build_data.map.width - 1, y); idx = build_data.map.xy_idx(build_data.map.width - 1, y);
possible_idxs.push(idx);
}
// For every possible point, first check if we only want to fill walkable tiles (and if its walkable if so)
for idx in possible_idxs {
if !self.only_walkable || tile_walkable(build_data.map.tiles[idx]) {
build_data.map.tiles[idx] = self.fill_with; build_data.map.tiles[idx] = self.fill_with;
} }
} }
} }
}

View file

@ -1,4 +1,4 @@
use super::{ BuilderChain, BuilderMap, InitialMapBuilder, Position, TileType }; use super::{ BuilderChain, BuilderMap, InitialMapBuilder, Position, TileType, FillEdges };
use std::collections::HashSet; use std::collections::HashSet;
use crate::data::names::*; use crate::data::names::*;
@ -21,6 +21,7 @@ pub fn town_builder(
initial_player_level initial_player_level
); );
chain.start_with(TownBuilder::new()); chain.start_with(TownBuilder::new());
chain.with(FillEdges::overmap_transition(new_id));
return chain; return chain;
} }
@ -74,8 +75,6 @@ impl TownBuilder {
x: build_data.width - 2, x: build_data.width - 2,
y: wall_gap_y, y: wall_gap_y,
}); });
let overmap_entrance = build_data.map.xy_idx(build_data.width - 2, wall_gap_y);
build_data.map.tiles[overmap_entrance] = TileType::ToOvermap(build_data.map.id);
build_data.take_snapshot(); build_data.take_snapshot();
} }