Finish chapter 2.4
This commit is contained in:
parent
3df0d8592b
commit
1345248b15
@ -22,4 +22,5 @@ pub struct Player {}
|
||||
pub struct Viewshed {
|
||||
pub visible_tiles: Vec<rltk::Point>,
|
||||
pub range: i32,
|
||||
pub dirty: bool,
|
||||
}
|
||||
|
@ -78,6 +78,7 @@ fn main() -> rltk::BError {
|
||||
.with(Viewshed {
|
||||
visible_tiles: Vec::new(),
|
||||
range: 8,
|
||||
dirty: true,
|
||||
})
|
||||
.build();
|
||||
|
||||
|
29
src/map.rs
29
src/map.rs
@ -15,6 +15,7 @@ pub struct Map {
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub revealed_tiles: Vec<bool>,
|
||||
pub visible_tiles: Vec<bool>,
|
||||
}
|
||||
|
||||
impl Map {
|
||||
@ -60,6 +61,7 @@ impl Map {
|
||||
width: 80,
|
||||
height: 50,
|
||||
revealed_tiles: vec![false; 80 * 50],
|
||||
visible_tiles: vec![false; 80 * 50],
|
||||
};
|
||||
|
||||
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() {
|
||||
// Render a tile depending on the tile type
|
||||
if map.revealed_tiles[idx] {
|
||||
let glyph;
|
||||
let mut fg;
|
||||
|
||||
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('.'),
|
||||
);
|
||||
glyph = rltk::to_cp437('.');
|
||||
fg = RGB::from_f32(0., 0.5, 0.5);
|
||||
}
|
||||
TileType::Wall => {
|
||||
ctx.set(
|
||||
x,
|
||||
y,
|
||||
RGB::from_f32(0.0, 1.0, 0.0),
|
||||
RGB::from_f32(0., 0., 0.),
|
||||
rltk::to_cp437('#'),
|
||||
);
|
||||
glyph = rltk::to_cp437('#');
|
||||
fg = RGB::from_f32(0., 1.0, 0.);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -1,4 +1,4 @@
|
||||
use super::{Map, Player, Position, State, TileType};
|
||||
use super::{Map, Player, Position, State, TileType, Viewshed};
|
||||
use rltk::{Rltk, VirtualKeyCode};
|
||||
use specs::prelude::*;
|
||||
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) {
|
||||
let mut positions = ecs.write_storage::<Position>();
|
||||
let mut players = ecs.write_storage::<Player>();
|
||||
let mut viewsheds = ecs.write_storage::<Viewshed>();
|
||||
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);
|
||||
if map.tiles[destination_idx] != TileType::Wall {
|
||||
pos.x = min(79, max(0, pos.x + delta_x));
|
||||
pos.y = min(49, max(0, pos.y + delta_y));
|
||||
|
||||
viewshed.dirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,18 +17,26 @@ impl<'a> System<'a> for VisibilitySystem {
|
||||
let (mut map, entities, mut viewshed, pos, player) = data;
|
||||
|
||||
for (ent, viewshed, pos) in (&entities, &mut viewshed, &pos).join() {
|
||||
viewshed.visible_tiles.clear();
|
||||
viewshed.visible_tiles = 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 viewshed.dirty {
|
||||
viewshed.dirty = false;
|
||||
viewshed.visible_tiles.clear();
|
||||
viewshed.visible_tiles =
|
||||
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
|
||||
let p: Option<&Player> = player.get(ent);
|
||||
if let Some(p) = p {
|
||||
for vis in viewshed.visible_tiles.iter() {
|
||||
let idx = map.xy_idx(vis.x, vis.y);
|
||||
map.revealed_tiles[idx] = true;
|
||||
// if this is the player, reveal what they can see
|
||||
let _p: Option<&Player> = player.get(ent);
|
||||
if let Some(_p) = _p {
|
||||
for t in map.visible_tiles.iter_mut() {
|
||||
*t = false
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user