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)
|
||||||
|
83
src/map.rs
83
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 {
|
||||||
@ -22,8 +23,8 @@ impl Map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn apply_room_to_map(&mut self, room: &Rect) {
|
fn apply_room_to_map(&mut self, room: &Rect) {
|
||||||
for y in room.y1+1 ..= room.y2 {
|
for y in room.y1 + 1..=room.y2 {
|
||||||
for x in room.x1+1 ..= room.x2 {
|
for x in room.x1 + 1..=room.x2 {
|
||||||
let idx = self.xy_idx(x, y);
|
let idx = self.xy_idx(x, y);
|
||||||
self.tiles[idx] = TileType::Floor;
|
self.tiles[idx] = TileType::Floor;
|
||||||
}
|
}
|
||||||
@ -31,7 +32,7 @@ impl Map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn apply_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
|
fn apply_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
|
||||||
for x in min(x1, x2) ..= max(x1, x2) {
|
for x in min(x1, x2)..=max(x1, x2) {
|
||||||
let idx = self.xy_idx(x, y);
|
let idx = self.xy_idx(x, y);
|
||||||
|
|
||||||
if idx > 0 && idx < self.width as usize * self.height as usize {
|
if idx > 0 && idx < self.width as usize * self.height as usize {
|
||||||
@ -41,7 +42,7 @@ impl Map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn apply_vertical_tunnel(&mut self, y1: i32, y2: i32, x: i32) {
|
fn apply_vertical_tunnel(&mut self, y1: i32, y2: i32, x: i32) {
|
||||||
for y in min(y1, y2) ..= max(y1, y2) {
|
for y in min(y1, y2)..=max(y1, y2) {
|
||||||
let idx = self.xy_idx(x, y);
|
let idx = self.xy_idx(x, y);
|
||||||
|
|
||||||
if idx > 0 && idx < self.width as usize * self.height as usize {
|
if idx > 0 && idx < self.width as usize * self.height as usize {
|
||||||
@ -57,12 +58,13 @@ 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;
|
||||||
const MIN_SIZE :i32 = 6;
|
const MIN_SIZE: i32 = 6;
|
||||||
const MAX_SIZE :i32 = 10;
|
const MAX_SIZE: i32 = 10;
|
||||||
|
|
||||||
let mut rng = RandomNumberGenerator::new();
|
let mut rng = RandomNumberGenerator::new();
|
||||||
|
|
||||||
@ -84,9 +86,9 @@ impl Map {
|
|||||||
if ok {
|
if ok {
|
||||||
map.apply_room_to_map(&new_room);
|
map.apply_room_to_map(&new_room);
|
||||||
|
|
||||||
if ! map.rooms.is_empty() {
|
if !map.rooms.is_empty() {
|
||||||
let (new_x, new_y) = new_room.center();
|
let (new_x, new_y) = new_room.center();
|
||||||
let (prev_x, prev_y) = map.rooms[map.rooms.len()-1].center();
|
let (prev_x, prev_y) = map.rooms[map.rooms.len() - 1].center();
|
||||||
|
|
||||||
if rng.range(0, 2) == 1 {
|
if rng.range(0, 2) == 1 {
|
||||||
map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
|
map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
|
||||||
@ -105,31 +107,46 @@ 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() {
|
||||||
match tile {
|
// Render a tile depending on the tile type
|
||||||
TileType::Floor => {
|
if map.revealed_tiles[idx] {
|
||||||
ctx.set(
|
match tile {
|
||||||
x,
|
TileType::Floor => {
|
||||||
y,
|
ctx.set(
|
||||||
RGB::from_f32(0.5, 0.5, 0.5),
|
x,
|
||||||
RGB::from_f32(0., 0., 0.),
|
y,
|
||||||
rltk::to_cp437('.'),
|
RGB::from_f32(0.5, 0.5, 0.5),
|
||||||
);
|
RGB::from_f32(0., 0., 0.),
|
||||||
}
|
rltk::to_cp437('.'),
|
||||||
TileType::Wall => {
|
);
|
||||||
ctx.set(
|
}
|
||||||
x,
|
TileType::Wall => {
|
||||||
y,
|
ctx.set(
|
||||||
RGB::from_f32(0.0, 1.0, 0.0),
|
x,
|
||||||
RGB::from_f32(0., 0., 0.),
|
y,
|
||||||
rltk::to_cp437('#'),
|
RGB::from_f32(0.0, 1.0, 0.0),
|
||||||
);
|
RGB::from_f32(0., 0., 0.),
|
||||||
|
rltk::to_cp437('#'),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,4 +157,4 @@ pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
|||||||
y += 1;
|
y += 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,23 +22,23 @@ 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),
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,11 +7,11 @@ pub struct Rect {
|
|||||||
|
|
||||||
impl Rect {
|
impl Rect {
|
||||||
pub fn new(x: i32, y: i32, w: i32, h: i32) -> Rect {
|
pub fn new(x: i32, y: i32, w: i32, h: i32) -> Rect {
|
||||||
Rect{
|
Rect {
|
||||||
x1: x,
|
x1: x,
|
||||||
y1: y,
|
y1: y,
|
||||||
x2: x+w,
|
x2: x + w,
|
||||||
y2: y+h,
|
y2: y + h,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,4 +23,4 @@ impl Rect {
|
|||||||
pub fn center(&self) -> (i32, i32) {
|
pub fn center(&self) -> (i32, i32) {
|
||||||
((self.x1 + self.x2) / 2, (self.y1 + self.y2) / 2)
|
((self.x1 + self.x2) / 2, (self.y1 + self.y2) / 2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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