handling visibility, also fixed corridor issue
This commit is contained in:
parent
6a5514d6bb
commit
bafe3fcbc9
42
src/main.rs
42
src/main.rs
@ -67,8 +67,7 @@ impl GameState for State {
|
|||||||
ctx.cls();
|
ctx.cls();
|
||||||
self.run_systems();
|
self.run_systems();
|
||||||
player_input(self, ctx);
|
player_input(self, ctx);
|
||||||
let map = self.ecs.fetch::<Map>();
|
draw_map(&self.ecs, ctx);
|
||||||
map.draw_map(ctx);
|
|
||||||
let positions = self.ecs.read_storage::<Position>();
|
let positions = self.ecs.read_storage::<Position>();
|
||||||
let renderables = self.ecs.read_storage::<Renderable>();
|
let renderables = self.ecs.read_storage::<Renderable>();
|
||||||
|
|
||||||
@ -78,6 +77,45 @@ impl GameState for State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
||||||
|
// Render a tile depending upon the tile type, if revealed
|
||||||
|
if map.revealed_tiles[idx] {
|
||||||
|
match tile {
|
||||||
|
TileType::Floor => {
|
||||||
|
ctx.set(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
RGB::from_f32(0.5, 0.5, 0.5),
|
||||||
|
RGB::from_f32(0., 0., 0.),
|
||||||
|
rltk::to_cp437('.'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
TileType::Wall => {
|
||||||
|
ctx.set(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
RGB::from_f32(0.0, 1.0, 0.0),
|
||||||
|
RGB::from_f32(0., 0., 0.),
|
||||||
|
rltk::to_cp437('#'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move the coordinates
|
||||||
|
x += 1;
|
||||||
|
if x >= map.width {
|
||||||
|
x = 0;
|
||||||
|
y += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
use rltk::RltkBuilder;
|
use rltk::RltkBuilder;
|
||||||
let context = RltkBuilder::simple80x50()
|
let context = RltkBuilder::simple80x50()
|
||||||
|
|||||||
61
src/map.rs
61
src/map.rs
@ -1,5 +1,5 @@
|
|||||||
use super::rect::*;
|
use super::rect::*;
|
||||||
use rltk::{Console, RandomNumberGenerator, Rltk, RGB};
|
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator};
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
@ -7,12 +7,13 @@ pub enum TileType {
|
|||||||
Wall,
|
Wall,
|
||||||
Floor,
|
Floor,
|
||||||
}
|
}
|
||||||
|
#[derive(Default)]
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
pub tiles: Vec<TileType>,
|
pub tiles: Vec<TileType>,
|
||||||
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 {
|
||||||
@ -20,14 +21,14 @@ impl Map {
|
|||||||
((y * self.width) + x) as usize
|
((y * self.width) + x) as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tile_at(&self, x: i32, y:i32) -> TileType {
|
pub fn tile_at(&self, x: i32, y: i32) -> TileType {
|
||||||
self.tiles[self.xy_idx(x, y)]
|
self.tiles[self.xy_idx(x, y)]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_room_to_map(&mut self, room: &Rect) {
|
fn apply_room_to_map(&mut self, room: &Rect) {
|
||||||
for x in room.x1..room.x2 {
|
for x in room.x1..room.x2 {
|
||||||
for y in room.y1..room.y2 {
|
for y in room.y1..room.y2 {
|
||||||
let idx = self.xy_idx(x,y);
|
let idx = self.xy_idx(x, y);
|
||||||
self.tiles[idx] = TileType::Floor
|
self.tiles[idx] = TileType::Floor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -35,14 +36,14 @@ 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);
|
||||||
self.tiles[idx] = TileType::Floor
|
self.tiles[idx] = TileType::Floor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_vertical_tunnel(&mut self, x: i32, y1: i32, y2: i32) {
|
fn apply_vertical_tunnel(&mut self, x: i32, y1: i32, y2: 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);
|
||||||
self.tiles[idx] = TileType::Floor
|
self.tiles[idx] = TileType::Floor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -53,6 +54,7 @@ impl Map {
|
|||||||
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;
|
||||||
@ -81,7 +83,7 @@ impl Map {
|
|||||||
if rng.range(0, 2) == 1 {
|
if rng.range(0, 2) == 1 {
|
||||||
map.apply_horizontal_tunnel(r1_center.0, r2_center.0, r1_center.1);
|
map.apply_horizontal_tunnel(r1_center.0, r2_center.0, r1_center.1);
|
||||||
map.apply_vertical_tunnel(
|
map.apply_vertical_tunnel(
|
||||||
max(r1_center.0, r2_center.0),
|
r2_center.0,
|
||||||
r1_center.1,
|
r1_center.1,
|
||||||
r2_center.1,
|
r2_center.1,
|
||||||
);
|
);
|
||||||
@ -90,7 +92,7 @@ impl Map {
|
|||||||
map.apply_horizontal_tunnel(
|
map.apply_horizontal_tunnel(
|
||||||
r1_center.0,
|
r1_center.0,
|
||||||
r2_center.0,
|
r2_center.0,
|
||||||
max(r1_center.1, r2_center.1),
|
r2_center.1,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,39 +102,16 @@ impl Map {
|
|||||||
|
|
||||||
map
|
map
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn draw_map(&self, ctx: &mut Rltk) {
|
impl BaseMap for Map {
|
||||||
let mut y = 0;
|
fn is_opaque(&self, idx: usize) -> bool {
|
||||||
let mut x = 0;
|
self.tiles[idx as usize] == TileType::Wall
|
||||||
for tile in self.tiles.iter() {
|
}
|
||||||
// Render a tile depending upon the tile type
|
}
|
||||||
match tile {
|
|
||||||
TileType::Floor => {
|
impl Algorithm2D for Map {
|
||||||
ctx.set(
|
fn dimensions(&self) -> Point {
|
||||||
x,
|
Point::new(self.width, self.height)
|
||||||
y,
|
|
||||||
RGB::from_f32(0.5, 0.5, 0.5),
|
|
||||||
RGB::from_f32(0., 0., 0.),
|
|
||||||
rltk::to_cp437('.'),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
TileType::Wall => {
|
|
||||||
ctx.set(
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
RGB::from_f32(0.0, 1.0, 0.0),
|
|
||||||
RGB::from_f32(0., 0., 0.),
|
|
||||||
rltk::to_cp437('#'),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move the coordinates
|
|
||||||
x += 1;
|
|
||||||
if x > 79 {
|
|
||||||
x = 0;
|
|
||||||
y += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,38 @@
|
|||||||
extern crate specs;
|
extern crate specs;
|
||||||
|
use super::{Map, Player, Position, Viewshed};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use super::{Viewshed, Position};
|
extern crate rltk;
|
||||||
|
use rltk::{field_of_view, Point};
|
||||||
|
|
||||||
pub struct VisibilitySystem {}
|
pub struct VisibilitySystem {}
|
||||||
|
|
||||||
impl<'a> System<'a> for VisibilitySystem {
|
impl<'a> System<'a> for VisibilitySystem {
|
||||||
type SystemData = ( WriteStorage<'a, Viewshed>,
|
type SystemData = (
|
||||||
WriteStorage<'a, Position>);
|
WriteExpect<'a, Map>,
|
||||||
|
Entities<'a>,
|
||||||
|
WriteStorage<'a, Viewshed>,
|
||||||
|
WriteStorage<'a, Position>,
|
||||||
|
ReadStorage<'a, Player>,
|
||||||
|
);
|
||||||
|
|
||||||
fn run(&mut self, (mut viewshed, pos) : Self::SystemData) {
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
for (viewshed,pos) in (&mut viewshed, &pos).join() {
|
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 - 1 && p.y > 0 && p.y < map.height - 1);
|
||||||
|
|
||||||
|
// 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…
x
Reference in New Issue
Block a user