Browse Source

added block types

master
Thomas Johnson 6 months ago
parent
commit
81649a0b88
  1. 7
      src/world/entity/mod.rs
  2. 8
      src/world/gen.rs
  3. 7
      src/world/mod.rs
  4. 18
      src/world/render.rs

7
src/world/entity/mod.rs

@ -11,7 +11,12 @@ pub trait Visible {
}
impl Visible for Tile {
fn is_transparent(&self) -> bool { self.content == Content::Air }
fn is_transparent(&self) -> bool {
match self.content {
Content::Air | Content::Window => true,
Content::Stone | Content::Fog => false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

8
src/world/gen.rs

@ -382,7 +382,7 @@ fn maybe_carve_between(seed: u64, coords: V2i, rs1: &RoomShape, rs2: &RoomShape,
}
}
fn carve_paths<T: Iterator<Item=Path>>(map: &mut [Tile], paths: T)
fn carve_paths<T: Iterator<Item=Path>>(map: &mut [Tile], paths: T, content: Content)
{
use Path::*;
let gbox = R2i::origin_dim(V2i(0, 0), V2i(GRID_SIZE, GRID_SIZE));
@ -392,7 +392,7 @@ fn carve_paths<T: Iterator<Item=Path>>(map: &mut [Tile], paths: T)
{
Piecewise(v) =>
{
carve_paths(map, v.into_iter());
carve_paths(map, v.into_iter(), content.clone());
},
Line(p1, p2) =>
{
@ -401,7 +401,7 @@ fn carve_paths<T: Iterator<Item=Path>>(map: &mut [Tile], paths: T)
{
if gbox.contains(tile)
{
map[(tile.1 * GRID_SIZE + tile.0) as usize].content = Content::Air;
map[(tile.1 * GRID_SIZE + tile.0) as usize].content = content.clone();
}
}
}
@ -415,7 +415,7 @@ pub fn carve(seed: u64, grid: &mut Grid<Tile>, coords: V2i)
carve_rooms(grid.array_mut(), rooms.iter().map(|c| c.clone()));
let cpi = rooms.iter().map(|a| rooms.iter().map(move |b| (a, b))).flatten().map(|(a, b)| (a, b));
let paths = cpi.map(|(a, b)| maybe_carve_between(seed, coords, a, b, 1)).filter_map(|o| o);
carve_paths(grid.array_mut(), paths);
carve_paths(grid.array_mut(), paths, Content::Air);
}
#[cfg(test)]

7
src/world/mod.rs

@ -18,6 +18,8 @@ use entity::*;
pub enum Content {
Air,
Stone,
Fog,
Window,
}
impl Default for Content {
@ -41,7 +43,10 @@ impl Default for Tile {
impl Traversable for Tile {
fn can_pass(&self) -> bool {
self.content == Content::Air
match self.content {
Content::Air | Content::Fog => true,
Content::Stone | Content::Window => false,
}
}
}

18
src/world/render.rs

@ -53,6 +53,8 @@ impl Renderer for BasicRenderer {
fn render<'a, 's: 'a>(&'s mut self, neighbors: Neighbors<'a, Tile>) -> u8 {
match neighbors.this.map(|t| t.content) {
Some(Content::Air) => b'.',
Some(Content::Window) => b'=',
Some(Content::Fog) => b':',
Some(Content::Stone) | None => b' ',
}
}
@ -67,6 +69,8 @@ impl Renderer for InvertRenderer {
match neighbors.this.map(|t| t.content) {
Some(Content::Air) => b' ',
Some(Content::Stone) | None => b'#',
Some(Content::Fog) => b':',
Some(Content::Window) | None => b'=',
}
}
}
@ -80,16 +84,28 @@ impl Renderer for EdgeRenderer {
match neighbors.this.map(|t| t.content) {
Some(Content::Air) => b'.',
None => b' ',
Some(Content::Fog) => b':',
Some(Content::Stone) => {
let mut air_count = 0;
neighbors.for_each(
|t| if let Some(Tile { content: Content::Air, .. }) = t { air_count += 1; });
|t| if let Some(Tile { content: Content::Stone, .. }) = t {} else { air_count += 1; });
if air_count > 0 {
b'#'
} else {
b' '
}
},
Some(Content::Window) => {
let mut window_count = 0;
neighbors.for_each(
|t| if t.map(|t| t.content == Content::Window) == Some(true) { window_count += 1; });
if window_count == 8 {
b' '
} else {
b'='
}
},
Some(Content::Fog) => b':',
}
}
}

Loading…
Cancel
Save