rendering first mob in visible areas

This commit is contained in:
Ben Morrison 2020-01-12 02:45:02 -05:00
parent e295b02704
commit c108e2d882
Signed by: gbmor
GPG Key ID: 8F192E4720BB0DAC
5 changed files with 38 additions and 18 deletions

View File

@ -53,5 +53,23 @@ fn main() {
})
.build();
map.rooms.iter().skip(1).for_each(|room| {
let (x, y) = room.center();
gs.ecs
.create_entity()
.with(Position { x, y })
.with(Renderable {
glyph: rltk::to_cp437('g'),
fg: RGB::named(rltk::RED),
bg: RGB::named(rltk::BLACK),
})
.with(Viewshed {
visible_tiles: Vec::new(),
range: 8,
dirty: true,
})
.build();
});
rltk::main_loop(context, gs);
}

View File

@ -51,8 +51,8 @@ impl Map {
}
pub fn apply_room(&mut self, room: &Rect) {
(room.y1 + 1..=room.y2).into_iter().for_each(|y| {
(room.x1 + 1..=room.x2).into_iter().for_each(|x| {
(room.y1 + 1..=room.y2).for_each(|y| {
(room.x1 + 1..=room.x2).for_each(|x| {
let idx = self.xy_idx(x, y);
self.tiles[idx] = TileType::Floor;
});
@ -60,7 +60,7 @@ impl Map {
}
pub fn apply_horiz_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
(min(x1, x2)..=max(x1, x2)).into_iter().for_each(|x| {
(min(x1, x2)..=max(x1, x2)).for_each(|x| {
let idx = self.xy_idx(x, y);
if idx > 0
&& idx < self.width as usize * self.height as usize
@ -71,7 +71,7 @@ impl Map {
}
pub fn apply_vert_tunnel(&mut self, y1: i32, y2: i32, x: i32) {
(min(y1, y2)..=max(y1, y2)).into_iter().for_each(|y| {
(min(y1, y2)..=max(y1, y2)).for_each(|y| {
let idx = self.xy_idx(x, y);
if idx > 0
&& idx < self.width as usize * self.height as usize
@ -97,7 +97,7 @@ impl Map {
let mut rng = RandomNumberGenerator::new();
(0..MAX_ROOMS).into_iter().for_each(|_| {
(0..MAX_ROOMS).for_each(|_| {
let w = rng.range(MIN_SIZE, MAX_SIZE);
let h = rng.range(MIN_SIZE, MAX_SIZE);
let x = rng.roll_dice(1, 80 - w - 1) - 1;

View File

@ -11,10 +11,8 @@ pub fn try_move(dx: i32, dy: i32, ecs: &mut World) {
let mut viewsheds = ecs.write_storage::<Viewshed>();
let map = ecs.fetch::<Map>();
(&mut players, &mut posns, &mut viewsheds)
.join()
.into_iter()
.for_each(|(_player, pos, viewshed)| {
(&mut players, &mut posns, &mut viewsheds).join().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;
@ -34,7 +32,8 @@ pub fn try_move(dx: i32, dy: i32, ecs: &mut World) {
}
viewshed.dirty = true;
}
});
},
);
}
pub fn input(gs: &mut State, ctx: &mut Rltk) {

View File

@ -21,9 +21,13 @@ impl GameState for State {
let positions = self.ecs.read_storage::<Position>();
let renderables = self.ecs.read_storage::<Renderable>();
let map = self.ecs.fetch::<map::Map>();
for (pos, rend) in (&positions, &renderables).join() {
ctx.set(pos.x, pos.y, rend.fg, rend.bg, rend.glyph);
}
(&positions, &renderables).join().for_each(|(pos, rend)| {
let idx = map.xy_idx(pos.x, pos.y);
if map.visible_tiles[idx] {
ctx.set(pos.x, pos.y, rend.fg, rend.bg, rend.glyph);
}
});
}
}

View File

@ -15,10 +15,8 @@ impl<'a> System<'a> for VisibilitySystem {
fn run(&mut self, data: Self::SystemData) {
let (mut map, entities, mut viewshed, pos, player) = data;
(&entities, &mut viewshed, &pos)
.join()
.into_iter()
.for_each(|(ent, viewshed, pos)| {
(&entities, &mut viewshed, &pos).join().for_each(
|(ent, viewshed, pos)| {
if viewshed.dirty {
viewshed.dirty = false;
viewshed.visible_tiles.clear();
@ -39,6 +37,7 @@ impl<'a> System<'a> for VisibilitySystem {
});
}
}
});
},
);
}
}