greyscale persistent mapping
This commit is contained in:
parent
ab050d5c89
commit
2fb49c22fd
|
@ -5,4 +5,5 @@ use specs::prelude::*;
|
|||
pub struct Viewshed {
|
||||
pub visible_tiles: Vec<rltk::Point>,
|
||||
pub range: i32,
|
||||
pub dirty: bool,
|
||||
}
|
||||
|
|
|
@ -49,6 +49,7 @@ fn main() {
|
|||
.with(Viewshed {
|
||||
visible_tiles: vec![],
|
||||
range: 8,
|
||||
dirty: true,
|
||||
})
|
||||
.build();
|
||||
|
||||
|
|
59
src/map.rs
59
src/map.rs
|
@ -6,8 +6,7 @@ use rltk::{
|
|||
};
|
||||
use specs::prelude::*;
|
||||
|
||||
use crate::component::Viewshed;
|
||||
use crate::entity::{Player, TileType};
|
||||
use crate::entity::TileType;
|
||||
use crate::rect::Rect;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
|
@ -17,6 +16,7 @@ pub struct Map {
|
|||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub revealed_tiles: Vec<bool>,
|
||||
pub visible_tiles: Vec<bool>,
|
||||
}
|
||||
|
||||
impl Algorithm2D for Map {
|
||||
|
@ -88,6 +88,7 @@ impl Map {
|
|||
width: 80,
|
||||
height: 50,
|
||||
revealed_tiles: vec![false; 80 * 50],
|
||||
visible_tiles: vec![false; 80 * 50],
|
||||
};
|
||||
|
||||
const MAX_ROOMS: i32 = 30;
|
||||
|
@ -134,36 +135,6 @@ impl Map {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn new_test() -> Vec<TileType> {
|
||||
let mut map = vec![TileType::Floor; 80 * 50];
|
||||
|
||||
(0..80).into_iter().for_each(|x| {
|
||||
map[Map::xy_idx(x, 0)] = TileType::Wall;
|
||||
map[Map::xy_idx(x, 49)] = TileType::Wall;
|
||||
});
|
||||
|
||||
(0..50).into_iter().for_each(|y| {
|
||||
map[Map::xy_idx(0, y)] = TileType::Wall;
|
||||
map[Map::xy_idx(79, y)] = TileType::Wall;
|
||||
});
|
||||
|
||||
//rando-splat of walls
|
||||
let mut rng = rltk::RandomNumberGenerator::new();
|
||||
|
||||
(0..400).into_iter().for_each(|_| {
|
||||
let x = rng.roll_dice(1, 79);
|
||||
let y = rng.roll_dice(1, 49);
|
||||
let idx = Map::xy_idx(x, y);
|
||||
if idx != Map::xy_idx(40, 25) {
|
||||
map[idx] = TileType::Wall;
|
||||
}
|
||||
});
|
||||
|
||||
map
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn draw(ecs: &World, ctx: &mut Rltk) {
|
||||
let map = ecs.fetch::<Map>();
|
||||
let mut y = 0;
|
||||
|
@ -171,26 +142,22 @@ pub fn draw(ecs: &World, ctx: &mut Rltk) {
|
|||
|
||||
map.tiles.iter().enumerate().for_each(|(idx, tile)| {
|
||||
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, 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);
|
||||
}
|
||||
|
||||
x += 1;
|
||||
|
|
|
@ -1,17 +1,20 @@
|
|||
use rltk::{Rltk, VirtualKeyCode};
|
||||
use specs::prelude::*;
|
||||
|
||||
use crate::entity::{Player, Position, TileType};
|
||||
use super::{entity::TileType, Player, Position, Viewshed};
|
||||
use crate::map::Map;
|
||||
use crate::state::State;
|
||||
|
||||
pub fn try_move(dx: i32, dy: i32, ecs: &mut World) {
|
||||
let mut posns = ecs.write_storage::<Position>();
|
||||
let mut players = ecs.write_storage::<Player>();
|
||||
let mut viewsheds = ecs.write_storage::<Viewshed>();
|
||||
let map = ecs.fetch::<Map>();
|
||||
|
||||
(&mut players, &mut posns).join().into_iter().for_each(
|
||||
|(_player, pos)| {
|
||||
(&mut players, &mut posns, &mut viewsheds)
|
||||
.join()
|
||||
.into_iter()
|
||||
.for_each(|(_player, pos, viewshed)| {
|
||||
let dest_idx = map.xy_idx(pos.x + dx, pos.y + dy);
|
||||
if map.tiles[dest_idx] != TileType::Wall {
|
||||
pos.x += dx;
|
||||
|
@ -29,9 +32,9 @@ pub fn try_move(dx: i32, dy: i32, ecs: &mut World) {
|
|||
if pos.y > 49 {
|
||||
pos.y = 49;
|
||||
}
|
||||
viewshed.dirty = true;
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn input(gs: &mut State, ctx: &mut Rltk) {
|
||||
|
|
|
@ -19,18 +19,25 @@ impl<'a> System<'a> for VisibilitySystem {
|
|||
.join()
|
||||
.into_iter()
|
||||
.for_each(|(ent, viewshed, pos)| {
|
||||
viewshed.visible_tiles.clear();
|
||||
viewshed.visible_tiles = field_of_view(
|
||||
Point::new(pos.x, pos.y),
|
||||
viewshed.range,
|
||||
&*map,
|
||||
);
|
||||
let p: Option<&Player> = player.get(ent);
|
||||
if let Some(p) = p {
|
||||
viewshed.visible_tiles.iter().for_each(|vis| {
|
||||
let idx = map.xy_idx(vis.x, vis.y);
|
||||
map.revealed_tiles[idx] = true;
|
||||
});
|
||||
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,
|
||||
);
|
||||
let _p: Option<&Player> = player.get(ent);
|
||||
if let Some(_p) = _p {
|
||||
map.visible_tiles
|
||||
.iter_mut()
|
||||
.for_each(|t| *t = false);
|
||||
viewshed.visible_tiles.iter().for_each(|vis| {
|
||||
let idx = map.xy_idx(vis.x, vis.y);
|
||||
map.revealed_tiles[idx] = true;
|
||||
map.visible_tiles[idx] = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue