fun with layers, and inventory display refactor

This commit is contained in:
Llywelwyn 2023-07-23 15:50:56 +01:00
parent 50e3cb50bc
commit b7b2061228
7 changed files with 122 additions and 129 deletions

View file

@ -1,4 +1,4 @@
use super::{BuilderMap, InitialMapBuilder, TileType};
use super::{BuilderMap, InitialMapBuilder, MetaMapBuilder, TileType};
use rltk::RandomNumberGenerator;
pub struct CellularAutomataBuilder {}
@ -10,6 +10,13 @@ impl InitialMapBuilder for CellularAutomataBuilder {
}
}
impl MetaMapBuilder for CellularAutomataBuilder {
#[allow(dead_code)]
fn build_map(&mut self, _rng: &mut rltk::RandomNumberGenerator, build_data: &mut BuilderMap) {
self.apply_iteration(build_data);
}
}
impl CellularAutomataBuilder {
#[allow(dead_code)]
pub fn new() -> Box<CellularAutomataBuilder> {
@ -34,47 +41,51 @@ impl CellularAutomataBuilder {
// Now we iteratively apply cellular automata rules
for _i in 0..15 {
let mut newtiles = build_data.map.tiles.clone();
for y in 1..build_data.map.height - 1 {
for x in 1..build_data.map.width - 1 {
let idx = build_data.map.xy_idx(x, y);
let mut neighbors = 0;
if build_data.map.tiles[idx - 1] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + 1] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - build_data.map.width as usize] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + build_data.map.width as usize] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - (build_data.map.width as usize - 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - (build_data.map.width as usize + 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + (build_data.map.width as usize - 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + (build_data.map.width as usize + 1)] == TileType::Wall {
neighbors += 1;
}
if neighbors > 4 || neighbors == 0 {
newtiles[idx] = TileType::Wall;
} else {
newtiles[idx] = TileType::Floor;
}
}
}
build_data.map.tiles = newtiles.clone();
build_data.take_snapshot();
self.apply_iteration(build_data);
}
}
fn apply_iteration(&mut self, build_data: &mut BuilderMap) {
let mut newtiles = build_data.map.tiles.clone();
for y in 1..build_data.map.height - 1 {
for x in 1..build_data.map.width - 1 {
let idx = build_data.map.xy_idx(x, y);
let mut neighbors = 0;
if build_data.map.tiles[idx - 1] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + 1] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - build_data.map.width as usize] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + build_data.map.width as usize] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - (build_data.map.width as usize - 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - (build_data.map.width as usize + 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + (build_data.map.width as usize - 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + (build_data.map.width as usize + 1)] == TileType::Wall {
neighbors += 1;
}
if neighbors > 4 || neighbors == 0 {
newtiles[idx] = TileType::Wall;
} else {
newtiles[idx] = TileType::Floor;
}
}
}
build_data.map.tiles = newtiles.clone();
build_data.take_snapshot();
}
}