place simple monsters, add dirty flag for viewshed
This commit is contained in:
35
src/main.rs
35
src/main.rs
@@ -17,17 +17,19 @@ pub struct State {
|
||||
ecs: World,
|
||||
}
|
||||
|
||||
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 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() {
|
||||
let x = min(79, max(0, pos.x + delta_x));
|
||||
let y = min(49, max(0, pos.y + delta_y));
|
||||
if map.tile_at(x, y) != TileType::Wall {
|
||||
pos.x = x;
|
||||
pos.y = y;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -141,9 +143,28 @@ fn main() {
|
||||
.with(Viewshed {
|
||||
visible_tiles: Vec::new(),
|
||||
range: 8,
|
||||
dirty: true,
|
||||
})
|
||||
.build();
|
||||
|
||||
for room in map.rooms.iter().skip(1) {
|
||||
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();
|
||||
}
|
||||
|
||||
gs.ecs.insert(map);
|
||||
|
||||
rltk::main_loop(context, gs);
|
||||
|
||||
Reference in New Issue
Block a user