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::prelude::*;
|
||||||
use specs_derive::*;
|
use specs_derive::*;
|
||||||
use rltk::{RGB};
|
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
@ -17,3 +17,9 @@ pub struct Renderable {
|
|||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct Player {}
|
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;
|
mod player;
|
||||||
use player::*;
|
use player::*;
|
||||||
mod rect;
|
mod rect;
|
||||||
|
mod visibility_system;
|
||||||
|
use visibility_system::VisibilitySystem;
|
||||||
|
|
||||||
pub use rect::Rect;
|
pub use rect::Rect;
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
@ -16,6 +19,9 @@ pub struct State {
|
|||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
fn run_systems(&mut self) {
|
fn run_systems(&mut self) {
|
||||||
|
let mut vis = VisibilitySystem {};
|
||||||
|
vis.run_now(&self.ecs);
|
||||||
|
|
||||||
self.ecs.maintain();
|
self.ecs.maintain();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -50,6 +56,7 @@ fn main() -> rltk::BError {
|
|||||||
gs.ecs.register::<Position>();
|
gs.ecs.register::<Position>();
|
||||||
gs.ecs.register::<Renderable>();
|
gs.ecs.register::<Renderable>();
|
||||||
gs.ecs.register::<Player>();
|
gs.ecs.register::<Player>();
|
||||||
|
gs.ecs.register::<Viewshed>();
|
||||||
|
|
||||||
let map: Map = Map::new_map_rooms_and_corridors();
|
let map: Map = Map::new_map_rooms_and_corridors();
|
||||||
let (player_x, player_y) = map.rooms[0].center();
|
let (player_x, player_y) = map.rooms[0].center();
|
||||||
@ -58,13 +65,20 @@ fn main() -> rltk::BError {
|
|||||||
|
|
||||||
gs.ecs
|
gs.ecs
|
||||||
.create_entity()
|
.create_entity()
|
||||||
.with(Position { x: player_x, y: player_y })
|
.with(Position {
|
||||||
|
x: player_x,
|
||||||
|
y: player_y,
|
||||||
|
})
|
||||||
.with(Renderable {
|
.with(Renderable {
|
||||||
glyph: rltk::to_cp437('@'),
|
glyph: rltk::to_cp437('@'),
|
||||||
fg: RGB::named(rltk::YELLOW),
|
fg: RGB::named(rltk::YELLOW),
|
||||||
bg: RGB::named(rltk::BLACK),
|
bg: RGB::named(rltk::BLACK),
|
||||||
})
|
})
|
||||||
.with(Player {})
|
.with(Player {})
|
||||||
|
.with(Viewshed {
|
||||||
|
visible_tiles: Vec::new(),
|
||||||
|
range: 8,
|
||||||
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
rltk::main_loop(context, gs)
|
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 super::{Rect};
|
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, RGB};
|
||||||
use std::cmp::{min, max};
|
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
pub enum TileType {
|
pub enum TileType {
|
||||||
@ -14,6 +14,7 @@ pub struct Map {
|
|||||||
pub rooms: Vec<Rect>,
|
pub rooms: Vec<Rect>,
|
||||||
pub width: i32,
|
pub width: i32,
|
||||||
pub height: i32,
|
pub height: i32,
|
||||||
|
pub revealed_tiles: Vec<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map {
|
impl Map {
|
||||||
@ -57,7 +58,8 @@ impl Map {
|
|||||||
tiles: vec![TileType::Wall; 80 * 50],
|
tiles: vec![TileType::Wall; 80 * 50],
|
||||||
rooms: Vec::new(),
|
rooms: Vec::new(),
|
||||||
width: 80,
|
width: 80,
|
||||||
height: 50
|
height: 50,
|
||||||
|
revealed_tiles: vec![false; 80 * 50],
|
||||||
};
|
};
|
||||||
|
|
||||||
const MAX_ROOMS: i32 = 30;
|
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) {
|
pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
||||||
let map = ecs.fetch::<Map>();
|
let map = ecs.fetch::<Map>();
|
||||||
|
|
||||||
let mut y = 0;
|
let mut y = 0;
|
||||||
let mut x = 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 {
|
match tile {
|
||||||
TileType::Floor => {
|
TileType::Floor => {
|
||||||
ctx.set(
|
ctx.set(
|
||||||
@ -132,6 +148,7 @@ pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Move to the next set of coordinates
|
// Move to the next set of coordinates
|
||||||
x += 1;
|
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 specs::prelude::*;
|
||||||
use super::{Position, Player, TileType, State, Map};
|
use std::cmp::{max, min};
|
||||||
use std::cmp::{min, max};
|
|
||||||
|
|
||||||
pub 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 positions = ecs.write_storage::<Position>();
|
||||||
@ -22,21 +22,21 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
|||||||
match ctx.key {
|
match ctx.key {
|
||||||
None => {} // Nothing happened
|
None => {} // Nothing happened
|
||||||
Some(key) => match key {
|
Some(key) => match key {
|
||||||
VirtualKeyCode::Left |
|
VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::H => {
|
||||||
VirtualKeyCode::Numpad4 |
|
try_move_player(-1, 0, &mut gs.ecs)
|
||||||
VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
|
}
|
||||||
|
|
||||||
VirtualKeyCode::Right |
|
VirtualKeyCode::Right | VirtualKeyCode::Numpad6 | VirtualKeyCode::L => {
|
||||||
VirtualKeyCode::Numpad6 |
|
try_move_player(1, 0, &mut gs.ecs)
|
||||||
VirtualKeyCode::L=> try_move_player(1, 0, &mut gs.ecs),
|
}
|
||||||
|
|
||||||
VirtualKeyCode::Up |
|
VirtualKeyCode::Up | VirtualKeyCode::Numpad8 | VirtualKeyCode::K => {
|
||||||
VirtualKeyCode::Numpad8 |
|
try_move_player(0, -1, &mut gs.ecs)
|
||||||
VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
|
}
|
||||||
|
|
||||||
VirtualKeyCode::Down |
|
VirtualKeyCode::Down | VirtualKeyCode::Numpad2 | VirtualKeyCode::J => {
|
||||||
VirtualKeyCode::Numpad2 |
|
try_move_player(0, 1, &mut gs.ecs)
|
||||||
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