Add chapter: 16

This commit is contained in:
Kasper Juul Hermansen 2022-01-28 14:12:38 +01:00
parent aad1402f81
commit 743bf7abde
Signed by: kjuulh
GPG Key ID: DCD9397082D97069
2 changed files with 38 additions and 4 deletions

View File

@ -1,6 +1,6 @@
use std::cmp::{max, min}; use std::cmp::{max, min};
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, RGB}; use rltk::{Algorithm2D, BaseMap, FontCharType, Point, RandomNumberGenerator, RGB, Rltk};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use specs::prelude::*; use specs::prelude::*;
@ -17,7 +17,6 @@ pub const MAP_WIDTH: usize = 80;
pub const MAP_HEIGHT: usize = 43; pub const MAP_HEIGHT: usize = 43;
pub const MAP_COUNT: usize = MAP_HEIGHT * MAP_WIDTH; pub const MAP_COUNT: usize = MAP_HEIGHT * MAP_WIDTH;
pub const MAX_MONSTER: i32 = 4; pub const MAX_MONSTER: i32 = 4;
pub const MAX_ITEMS: i32 = 2;
#[derive(Default, Serialize, Deserialize, Clone)] #[derive(Default, Serialize, Deserialize, Clone)]
pub struct Map { pub struct Map {
@ -216,7 +215,7 @@ pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
TileType::Wall => { TileType::Wall => {
fg = RGB::from_f32(0., 1.0, 0.); fg = RGB::from_f32(0., 1.0, 0.);
glyph = rltk::to_cp437('#'); glyph = wall_glyph(&*map, x, y);
} }
TileType::DownStairs => { TileType::DownStairs => {
@ -237,3 +236,38 @@ pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
} }
} }
} }
fn wall_glyph(map: &Map, x: i32, y: i32) -> FontCharType {
if x < 1 || x > map.width - 2 || y < 1 || y > map.height - 2 as i32 { return 35; }
let mut mask: u8 = 0;
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 south 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]
}

View File

@ -9,7 +9,7 @@ use crate::rect::Rect;
use crate::{ use crate::{
AreaOfEffect, BlocksTile, CombatStats, Confusion, Consumable, DefenseBonus, EquipmentSlot, AreaOfEffect, BlocksTile, CombatStats, Confusion, Consumable, DefenseBonus, EquipmentSlot,
Equippable, InflictsDamage, Item, MeleePowerBonus, Monster, Name, Player, Position, Equippable, InflictsDamage, Item, MeleePowerBonus, Monster, Name, Player, Position,
ProvidesHealing, Ranged, Renderable, SerializeMe, Viewshed, MAP_WIDTH, MAX_ITEMS, MAX_MONSTER, ProvidesHealing, Ranged, Renderable, SerializeMe, Viewshed, MAP_WIDTH, MAX_MONSTER,
}; };
pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity { pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {