Finish chapter 2.4

This commit is contained in:
Timothy Warren 2021-10-26 14:23:08 -04:00
parent 3df0d8592b
commit 1345248b15
5 changed files with 41 additions and 27 deletions

View File

@ -22,4 +22,5 @@ pub struct Player {}
pub struct Viewshed { pub struct Viewshed {
pub visible_tiles: Vec<rltk::Point>, pub visible_tiles: Vec<rltk::Point>,
pub range: i32, pub range: i32,
pub dirty: bool,
} }

View File

@ -78,6 +78,7 @@ fn main() -> rltk::BError {
.with(Viewshed { .with(Viewshed {
visible_tiles: Vec::new(), visible_tiles: Vec::new(),
range: 8, range: 8,
dirty: true,
}) })
.build(); .build();

View File

@ -15,6 +15,7 @@ pub struct Map {
pub width: i32, pub width: i32,
pub height: i32, pub height: i32,
pub revealed_tiles: Vec<bool>, pub revealed_tiles: Vec<bool>,
pub visible_tiles: Vec<bool>,
} }
impl Map { impl Map {
@ -60,6 +61,7 @@ impl Map {
width: 80, width: 80,
height: 50, height: 50,
revealed_tiles: vec![false; 80 * 50], revealed_tiles: vec![false; 80 * 50],
visible_tiles: vec![false; 80 * 50],
}; };
const MAX_ROOMS: i32 = 30; const MAX_ROOMS: i32 = 30;
@ -128,26 +130,25 @@ pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
for (idx, tile) in map.tiles.iter().enumerate() { for (idx, tile) in map.tiles.iter().enumerate() {
// Render a tile depending on the tile type // Render a tile depending on the tile type
if map.revealed_tiles[idx] { if map.revealed_tiles[idx] {
let glyph;
let mut fg;
match tile { match tile {
TileType::Floor => { TileType::Floor => {
ctx.set( glyph = rltk::to_cp437('.');
x, fg = RGB::from_f32(0., 0.5, 0.5);
y,
RGB::from_f32(0.5, 0.5, 0.5),
RGB::from_f32(0., 0., 0.),
rltk::to_cp437('.'),
);
} }
TileType::Wall => { TileType::Wall => {
ctx.set( glyph = rltk::to_cp437('#');
x, fg = RGB::from_f32(0., 1.0, 0.);
y,
RGB::from_f32(0.0, 1.0, 0.0),
RGB::from_f32(0., 0., 0.),
rltk::to_cp437('#'),
);
} }
} }
if !map.visible_tiles[idx] {
fg = fg.to_greyscale();
}
ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
} }
// Move to the next set of coordinates // Move to the next set of coordinates

View File

@ -1,4 +1,4 @@
use super::{Map, Player, Position, State, TileType}; use super::{Map, Player, Position, State, TileType, Viewshed};
use rltk::{Rltk, VirtualKeyCode}; use rltk::{Rltk, VirtualKeyCode};
use specs::prelude::*; use specs::prelude::*;
use std::cmp::{max, min}; use std::cmp::{max, min};
@ -6,13 +6,16 @@ use std::cmp::{max, min};
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
let mut positions = ecs.write_storage::<Position>(); let mut positions = ecs.write_storage::<Position>();
let mut players = ecs.write_storage::<Player>(); let mut players = ecs.write_storage::<Player>();
let mut viewsheds = ecs.write_storage::<Viewshed>();
let map = ecs.fetch::<Map>(); let map = ecs.fetch::<Map>();
for (_player, pos) in (&mut players, &mut positions).join() { for (_player, pos, viewshed) in (&mut players, &mut positions, &mut viewsheds).join() {
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y); let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
if map.tiles[destination_idx] != TileType::Wall { if map.tiles[destination_idx] != TileType::Wall {
pos.x = min(79, max(0, pos.x + delta_x)); pos.x = min(79, max(0, pos.x + delta_x));
pos.y = min(49, max(0, pos.y + delta_y)); pos.y = min(49, max(0, pos.y + delta_y));
viewshed.dirty = true;
} }
} }
} }

View File

@ -17,18 +17,26 @@ impl<'a> System<'a> for VisibilitySystem {
let (mut map, entities, mut viewshed, pos, player) = data; let (mut map, entities, mut viewshed, pos, player) = data;
for (ent, viewshed, pos) in (&entities, &mut viewshed, &pos).join() { for (ent, viewshed, pos) in (&entities, &mut viewshed, &pos).join() {
viewshed.visible_tiles.clear(); if viewshed.dirty {
viewshed.visible_tiles = field_of_view(Point::new(pos.x, pos.y), viewshed.range, &*map); viewshed.dirty = false;
viewshed viewshed.visible_tiles.clear();
.visible_tiles viewshed.visible_tiles =
.retain(|p| p.x >= 0 && p.x < map.width && p.y >= 0 && p.y < map.height); field_of_view(Point::new(pos.x, pos.y), viewshed.range, &*map);
viewshed
.visible_tiles
.retain(|p| p.x >= 0 && p.x < map.width && p.y >= 0 && p.y < map.height);
// if this is the player, reveal what they can see // if this is the player, reveal what they can see
let p: Option<&Player> = player.get(ent); let _p: Option<&Player> = player.get(ent);
if let Some(p) = p { if let Some(_p) = _p {
for vis in viewshed.visible_tiles.iter() { for t in map.visible_tiles.iter_mut() {
let idx = map.xy_idx(vis.x, vis.y); *t = false
map.revealed_tiles[idx] = true; }
for vis in viewshed.visible_tiles.iter() {
let idx = map.xy_idx(vis.x, vis.y);
map.revealed_tiles[idx] = true;
map.visible_tiles[idx] = true;
}
} }
} }
} }