From cb2839733b6c18106c932b400338eba6f9f88e90 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 17 Dec 2021 16:54:36 -0500 Subject: [PATCH] Complete section 4.19 --- src/camera.rs | 43 +++++++++++++- src/gui.rs | 2 +- src/main.rs | 6 +- src/map.rs | 95 +----------------------------- src/map_builders/door_placement.rs | 6 +- 5 files changed, 50 insertions(+), 102 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 2f945c0..acc5939 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -3,7 +3,7 @@ use specs::prelude::*; use crate::{Hidden, Map, Position, Renderable, TileType}; -const SHOW_BOUNDARIES: bool = true; +const SHOW_BOUNDARIES: bool = false; pub fn get_screen_bounds(ecs: &World, ctx: &mut Rltk) -> (i32, i32, i32, i32) { let player_pos = ecs.fetch::(); @@ -85,6 +85,47 @@ pub fn render_camera(ecs: &World, ctx: &mut Rltk) { } } +pub fn render_debug_map(map: &Map, ctx: &mut Rltk) { + let player_pos = Point::new(map.width / 2, map.height / 2); + let (x_chars, y_chars) = ctx.get_char_size(); + + let center_x = (x_chars / 2) as i32; + let center_y = (y_chars / 2) as i32; + + let min_x = player_pos.x - center_x; + let max_x = min_x + x_chars as i32; + let min_y = player_pos.y - center_y; + let max_y = min_y + y_chars as i32; + + let map_width = map.width - 1; + let map_height = map.height - 1; + + let mut y = 0; + #[allow(clippy::explicit_counter_loop)] + for ty in min_y..max_y { + let mut x = 0; + for tx in min_x..max_x { + if tx > 0 && tx < map_width && ty > 0 && ty < map_height { + let idx = map.xy_idx(tx, ty); + if map.revealed_tiles[idx] { + let (glyph, fg, bg) = get_tile_glyph(idx, &*map); + ctx.set(x, y, fg, bg, glyph); + } + } else if SHOW_BOUNDARIES { + ctx.set( + x, + y, + RGB::named(rltk::GRAY), + RGB::named(rltk::BLACK), + rltk::to_cp437('·'), + ); + } + x += 1; + } + y += 1; + } +} + fn get_tile_glyph(idx: usize, map: &Map) -> (rltk::FontCharType, RGB, RGB) { let glyph; let mut fg; diff --git a/src/gui.rs b/src/gui.rs index 0372b8e..5c4f8b4 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -64,7 +64,7 @@ pub fn draw_ui(ecs: &World, ctx: &mut Rltk) { 42, RGB::named(rltk::RED), RGB::named(rltk::BLACK), - "Well Fed", + "Starving", ), } } diff --git a/src/main.rs b/src/main.rs index b1b8ed7..2b151f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -162,7 +162,9 @@ impl GameState for State { newrunstate = self.mapgen_next_state.unwrap(); } ctx.cls(); - draw_map(&self.mapgen_history[self.mapgen_index], ctx); + if self.mapgen_index < self.mapgen_history.len() { + camera::render_debug_map(&self.mapgen_history[self.mapgen_index], ctx); + } self.mapgen_timer += ctx.frame_time_ms; if self.mapgen_timer > 300.0 { @@ -445,7 +447,7 @@ impl State { self.mapgen_history.clear(); let mut rng = self.ecs.write_resource::(); - let mut builder = map_builders::random_builder(new_depth, &mut rng, 64, 64); + let mut builder = map_builders::random_builder(new_depth, &mut rng, 80, 50); builder.build_map(&mut rng); std::mem::drop(rng); diff --git a/src/map.rs b/src/map.rs index 8d3758d..fe063d3 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,6 +1,6 @@ use std::collections::HashSet; -use rltk::{Algorithm2D, BaseMap, Point, Rltk, SmallVec, RGB}; +use rltk::{Algorithm2D, BaseMap, Point, SmallVec}; use serde::{Deserialize, Serialize}; use specs::prelude::*; @@ -130,96 +130,3 @@ impl Algorithm2D for Map { Point::new(self.width, self.height) } } - -pub fn draw_map(map: &Map, ctx: &mut Rltk) { - let mut y = 0; - let mut x = 0; - - for (idx, tile) in map.tiles.iter().enumerate() { - // Render a tile depending on the tile type - if map.revealed_tiles[idx] { - let glyph; - let mut fg; - let mut bg = RGB::from_f32(0., 0., 0.); - - match tile { - TileType::Floor => { - glyph = rltk::to_cp437('.'); - fg = RGB::from_f32(0., 0.5, 0.5); - } - TileType::Wall => { - glyph = wall_glyph(&*map, x, y); - fg = RGB::from_f32(0., 1.0, 0.); - } - TileType::DownStairs => { - glyph = rltk::to_cp437('>'); - fg = RGB::from_f32(0., 1.0, 1.0); - } - } - - if map.bloodstains.contains(&idx) { - bg = RGB::from_f32(0.75, 0., 0.); - } - - if !map.visible_tiles[idx] { - fg = fg.to_greyscale(); - bg = RGB::from_f32(0., 0., 0.); - } - - ctx.set(x, y, fg, bg, glyph); - } - - // Move to the next set of coordinates - x += 1; - if x > (map.width * map.height) as i32 - 1 { - x = 0; - y += 1; - } - } -} - -fn wall_glyph(map: &Map, x: i32, y: i32) -> rltk::FontCharType { - if x < 1 || x > map.width - 2 || y < 1 || y > map.height - 2 { - return 35; - } - - let mut mask = 0u8; - - if is_revealed_and_wall(map, x, y - 1) { - mask += 1; - } - if is_revealed_and_wall(map, x, y + 1) { - mask += 2; - } - if is_revealed_and_wall(map, x - 1, y) { - mask += 4; - } - if is_revealed_and_wall(map, x + 1, y) { - mask += 8; - } - - match mask { - 0 => 9, // Pillar because we can't see neighbors - 1 => 186, // Wall only to the north - 2 => 186, // Wall only to the south - 3 => 186, // Wall to the north and south - 4 => 205, // Wall only to the west - 5 => 188, // Wall to the north and west - 6 => 187, // Wall to the south and west - 7 => 185, // Wall to the north, south, and west - 8 => 205, // Wall only to the east - 9 => 200, // Wall to the north and east - 10 => 201, // Wall to the sound and east - 11 => 204, // Wall to the north, south, and east - 12 => 205, // Wall to the east and west - 13 => 202, // Wall to the east, west, and south - 14 => 203, // Wall to the east, west, and north - 15 => 206, // ╬ Wall on all sides - _ => 35, // We missed one? - } -} - -fn is_revealed_and_wall(map: &Map, x: i32, y: i32) -> bool { - let idx = map.xy_idx(x, y); - map.tiles[idx] == TileType::Wall && map.revealed_tiles[idx] -} diff --git a/src/map_builders/door_placement.rs b/src/map_builders/door_placement.rs index a61abcd..43f2580 100644 --- a/src/map_builders/door_placement.rs +++ b/src/map_builders/door_placement.rs @@ -23,10 +23,8 @@ impl DoorPlacement { let halls = halls_original.clone(); for hall in halls.iter() { // We aren't interested in tiny corridors - if hall.len() > 2 { - if self.door_possible(build_data, hall[0]) { - build_data.spawn_list.push((hall[0], "Door".to_string())); - } + if hall.len() > 2 && self.door_possible(build_data, hall[0]) { + build_data.spawn_list.push((hall[0], "Door".to_string())); } } } else {