First good iteration of field of view
This commit is contained in:
parent
8e23e09b3a
commit
3df0d8592b
@ -1,6 +1,6 @@
|
||||
use rltk::RGB;
|
||||
use specs::prelude::*;
|
||||
use specs_derive::*;
|
||||
use rltk::{RGB};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Position {
|
||||
@ -17,3 +17,9 @@ pub struct Renderable {
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct Player {}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Viewshed {
|
||||
pub visible_tiles: Vec<rltk::Point>,
|
||||
pub range: i32,
|
||||
}
|
||||
|
16
src/main.rs
16
src/main.rs
@ -8,6 +8,9 @@ pub use map::*;
|
||||
mod player;
|
||||
use player::*;
|
||||
mod rect;
|
||||
mod visibility_system;
|
||||
use visibility_system::VisibilitySystem;
|
||||
|
||||
pub use rect::Rect;
|
||||
|
||||
pub struct State {
|
||||
@ -16,6 +19,9 @@ pub struct State {
|
||||
|
||||
impl State {
|
||||
fn run_systems(&mut self) {
|
||||
let mut vis = VisibilitySystem {};
|
||||
vis.run_now(&self.ecs);
|
||||
|
||||
self.ecs.maintain();
|
||||
}
|
||||
}
|
||||
@ -50,6 +56,7 @@ fn main() -> rltk::BError {
|
||||
gs.ecs.register::<Position>();
|
||||
gs.ecs.register::<Renderable>();
|
||||
gs.ecs.register::<Player>();
|
||||
gs.ecs.register::<Viewshed>();
|
||||
|
||||
let map: Map = Map::new_map_rooms_and_corridors();
|
||||
let (player_x, player_y) = map.rooms[0].center();
|
||||
@ -58,13 +65,20 @@ fn main() -> rltk::BError {
|
||||
|
||||
gs.ecs
|
||||
.create_entity()
|
||||
.with(Position { x: player_x, y: player_y })
|
||||
.with(Position {
|
||||
x: player_x,
|
||||
y: player_y,
|
||||
})
|
||||
.with(Renderable {
|
||||
glyph: rltk::to_cp437('@'),
|
||||
fg: RGB::named(rltk::YELLOW),
|
||||
bg: RGB::named(rltk::BLACK),
|
||||
})
|
||||
.with(Player {})
|
||||
.with(Viewshed {
|
||||
visible_tiles: Vec::new(),
|
||||
range: 8,
|
||||
})
|
||||
.build();
|
||||
|
||||
rltk::main_loop(context, gs)
|
||||
|
27
src/map.rs
27
src/map.rs
@ -1,7 +1,7 @@
|
||||
use rltk::{ RGB, Rltk, RandomNumberGenerator };
|
||||
use super::{Rect};
|
||||
use std::cmp::{min, max};
|
||||
use super::Rect;
|
||||
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, RGB};
|
||||
use specs::prelude::*;
|
||||
use std::cmp::{max, min};
|
||||
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
pub enum TileType {
|
||||
@ -14,6 +14,7 @@ pub struct Map {
|
||||
pub rooms: Vec<Rect>,
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub revealed_tiles: Vec<bool>,
|
||||
}
|
||||
|
||||
impl Map {
|
||||
@ -57,7 +58,8 @@ impl Map {
|
||||
tiles: vec![TileType::Wall; 80 * 50],
|
||||
rooms: Vec::new(),
|
||||
width: 80,
|
||||
height: 50
|
||||
height: 50,
|
||||
revealed_tiles: vec![false; 80 * 50],
|
||||
};
|
||||
|
||||
const MAX_ROOMS: i32 = 30;
|
||||
@ -105,13 +107,27 @@ impl Map {
|
||||
}
|
||||
}
|
||||
|
||||
impl Algorithm2D for Map {
|
||||
fn dimensions(&self) -> Point {
|
||||
Point::new(self.width, self.height)
|
||||
}
|
||||
}
|
||||
|
||||
impl BaseMap for Map {
|
||||
fn is_opaque(&self, idx: usize) -> bool {
|
||||
self.tiles[idx as usize] == TileType::Wall
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
||||
let map = ecs.fetch::<Map>();
|
||||
|
||||
let mut y = 0;
|
||||
let mut x = 0;
|
||||
|
||||
for (_idx, tile) in map.tiles.iter().enumerate() {
|
||||
for (idx, tile) in map.tiles.iter().enumerate() {
|
||||
// Render a tile depending on the tile type
|
||||
if map.revealed_tiles[idx] {
|
||||
match tile {
|
||||
TileType::Floor => {
|
||||
ctx.set(
|
||||
@ -132,6 +148,7 @@ pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Move to the next set of coordinates
|
||||
x += 1;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use rltk::{VirtualKeyCode, Rltk};
|
||||
use super::{Map, Player, Position, State, TileType};
|
||||
use rltk::{Rltk, VirtualKeyCode};
|
||||
use specs::prelude::*;
|
||||
use super::{Position, Player, TileType, State, Map};
|
||||
use std::cmp::{min, max};
|
||||
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>();
|
||||
@ -22,21 +22,21 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
||||
match ctx.key {
|
||||
None => {} // Nothing happened
|
||||
Some(key) => match key {
|
||||
VirtualKeyCode::Left |
|
||||
VirtualKeyCode::Numpad4 |
|
||||
VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
|
||||
VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::H => {
|
||||
try_move_player(-1, 0, &mut gs.ecs)
|
||||
}
|
||||
|
||||
VirtualKeyCode::Right |
|
||||
VirtualKeyCode::Numpad6 |
|
||||
VirtualKeyCode::L=> try_move_player(1, 0, &mut gs.ecs),
|
||||
VirtualKeyCode::Right | VirtualKeyCode::Numpad6 | VirtualKeyCode::L => {
|
||||
try_move_player(1, 0, &mut gs.ecs)
|
||||
}
|
||||
|
||||
VirtualKeyCode::Up |
|
||||
VirtualKeyCode::Numpad8 |
|
||||
VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
|
||||
VirtualKeyCode::Up | VirtualKeyCode::Numpad8 | VirtualKeyCode::K => {
|
||||
try_move_player(0, -1, &mut gs.ecs)
|
||||
}
|
||||
|
||||
VirtualKeyCode::Down |
|
||||
VirtualKeyCode::Numpad2 |
|
||||
VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
|
||||
VirtualKeyCode::Down | VirtualKeyCode::Numpad2 | VirtualKeyCode::J => {
|
||||
try_move_player(0, 1, &mut gs.ecs)
|
||||
}
|
||||
|
||||
_ => {}
|
||||
},
|
||||
|
36
src/visibility_system.rs
Normal file
36
src/visibility_system.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use super::{Map, Player, Position, Viewshed};
|
||||
use rltk::{field_of_view, Point};
|
||||
use specs::prelude::*;
|
||||
|
||||
pub struct VisibilitySystem {}
|
||||
|
||||
impl<'a> System<'a> for VisibilitySystem {
|
||||
type SystemData = (
|
||||
WriteExpect<'a, Map>,
|
||||
Entities<'a>,
|
||||
WriteStorage<'a, Viewshed>,
|
||||
WriteStorage<'a, Position>,
|
||||
ReadStorage<'a, Player>,
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user