handling visibility, also fixed corridor issue

This commit is contained in:
2020-03-29 15:48:52 -04:00
parent 6a5514d6bb
commit bafe3fcbc9
3 changed files with 89 additions and 49 deletions

View File

@@ -1,5 +1,5 @@
use super::rect::*;
use rltk::{Console, RandomNumberGenerator, Rltk, RGB};
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator};
use std::cmp::{max, min};
#[derive(PartialEq, Copy, Clone)]
@@ -7,12 +7,13 @@ pub enum TileType {
Wall,
Floor,
}
#[derive(Default)]
pub struct Map {
pub tiles: Vec<TileType>,
pub rooms: Vec<Rect>,
pub width: i32,
pub height: i32,
pub revealed_tiles: Vec<bool>,
}
impl Map {
@@ -20,14 +21,14 @@ impl Map {
((y * self.width) + x) as usize
}
pub fn tile_at(&self, x: i32, y:i32) -> TileType {
pub fn tile_at(&self, x: i32, y: i32) -> TileType {
self.tiles[self.xy_idx(x, y)]
}
fn apply_room_to_map(&mut self, room: &Rect) {
for x in room.x1..room.x2 {
for y in room.y1..room.y2 {
let idx = self.xy_idx(x,y);
let idx = self.xy_idx(x, y);
self.tiles[idx] = TileType::Floor
}
}
@@ -35,14 +36,14 @@ impl Map {
fn apply_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
for x in min(x1, x2)..=max(x1, x2) {
let idx=self.xy_idx(x, y);
let idx = self.xy_idx(x, y);
self.tiles[idx] = TileType::Floor
}
}
fn apply_vertical_tunnel(&mut self, x: i32, y1: i32, y2: i32) {
for y in min(y1, y2)..=max(y1, y2) {
let idx=self.xy_idx(x, y);
let idx = self.xy_idx(x, y);
self.tiles[idx] = TileType::Floor
}
}
@@ -53,6 +54,7 @@ impl Map {
rooms: Vec::new(),
width: 80,
height: 50,
revealed_tiles: vec![false; 80 * 50],
};
const MAX_ROOMS: i32 = 30;
@@ -81,7 +83,7 @@ impl Map {
if rng.range(0, 2) == 1 {
map.apply_horizontal_tunnel(r1_center.0, r2_center.0, r1_center.1);
map.apply_vertical_tunnel(
max(r1_center.0, r2_center.0),
r2_center.0,
r1_center.1,
r2_center.1,
);
@@ -90,7 +92,7 @@ impl Map {
map.apply_horizontal_tunnel(
r1_center.0,
r2_center.0,
max(r1_center.1, r2_center.1),
r2_center.1,
);
}
}
@@ -100,39 +102,16 @@ impl Map {
map
}
}
pub fn draw_map(&self, ctx: &mut Rltk) {
let mut y = 0;
let mut x = 0;
for tile in self.tiles.iter() {
// Render a tile depending upon the tile type
match tile {
TileType::Floor => {
ctx.set(
x,
y,
RGB::from_f32(0.5, 0.5, 0.5),
RGB::from_f32(0., 0., 0.),
rltk::to_cp437('.'),
);
}
TileType::Wall => {
ctx.set(
x,
y,
RGB::from_f32(0.0, 1.0, 0.0),
RGB::from_f32(0., 0., 0.),
rltk::to_cp437('#'),
);
}
}
// Move the coordinates
x += 1;
if x > 79 {
x = 0;
y += 1;
}
}
impl BaseMap for Map {
fn is_opaque(&self, idx: usize) -> bool {
self.tiles[idx as usize] == TileType::Wall
}
}
impl Algorithm2D for Map {
fn dimensions(&self) -> Point {
Point::new(self.width, self.height)
}
}