line-of-sight

This commit is contained in:
Ben Morrison 2019-09-11 12:29:28 -04:00
parent 95a2ffc13e
commit 20dac27bff
Signed by: gbmor
GPG Key ID: 8F192E4720BB0DAC
2 changed files with 52 additions and 16 deletions

Binary file not shown.

View File

@ -1,5 +1,8 @@
use rand::Rng; use rand::Rng;
use rltk::{Console, GameState, Rltk, VirtualKeyCode, RGB}; use rltk::{
Algorithm2D, BaseMap, Console, GameState, Point, Rltk,
VirtualKeyCode, RGB,
};
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
enum TileType { enum TileType {
@ -10,6 +13,7 @@ enum TileType {
struct State { struct State {
map: Vec<TileType>, map: Vec<TileType>,
player_position: usize, player_position: usize,
visible: Vec<bool>,
} }
fn xy_idx(x: i32, y: i32) -> usize { fn xy_idx(x: i32, y: i32) -> usize {
@ -25,6 +29,7 @@ impl State {
let mut state = State { let mut state = State {
map: vec![TileType::Floor; 80 * 50], map: vec![TileType::Floor; 80 * 50],
player_position: xy_idx(40, 25), player_position: xy_idx(40, 25),
visible: vec![false; 80 * 50],
}; };
(0..80).into_iter().for_each(|x| { (0..80).into_iter().for_each(|x| {
@ -63,6 +68,7 @@ impl State {
} }
impl GameState for State { impl GameState for State {
#[allow(non_snake_case)]
fn tick(&mut self, ctx: &mut Rltk) { fn tick(&mut self, ctx: &mut Rltk) {
match ctx.key { match ctx.key {
None => {} None => {}
@ -90,31 +96,40 @@ impl GameState for State {
} }
} }
self.visible.iter_mut().for_each(|v| {
*v = false;
});
let player_position =
self.index_to_point2d(self.player_position as i32);
let fov = rltk::field_of_view(player_position, 8, self);
fov.iter().for_each(|idx| {
self.visible[xy_idx(idx.x, idx.y)] = true;
});
ctx.cls(); ctx.cls();
let mut y = 0; let mut y = 0;
let mut x = 0; let mut x = 0;
&self.map.iter().for_each(|tile| { &self.map.iter().enumerate().for_each(|(i, tile)| {
let mut fg;
let mut glyph = ".";
match tile { match tile {
TileType::Floor => { TileType::Floor => {
ctx.print_color( fg = RGB::from_f32(0.5, 0.5, 0.0);
x,
y,
RGB::from_f32(0.5, 0.5, 0.5),
RGB::from_f32(0., 0., 0.),
".",
);
} }
TileType::Wall => { TileType::Wall => {
ctx.print_color( fg = RGB::from_f32(0.0, 1.0, 0.0);
x, glyph = "#";
y,
RGB::from_f32(0.0, 1.0, 0.0),
RGB::from_f32(0., 0., 0.),
"#",
);
} }
} }
if !self.visible[i] {
fg = fg.to_greyscale();
}
ctx.print_color(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
x += 1; x += 1;
if x > 79 { if x > 79 {
x = 0; x = 0;
@ -133,6 +148,27 @@ impl GameState for State {
} }
} }
impl BaseMap for State {
fn is_opaque(&self, idx: i32) -> bool {
self.map[idx as usize] == TileType::Wall
}
fn get_available_exits(&self, _idx: i32) -> Vec<(i32, f32)> {
Vec::new() // no exits? it's a trap!!!
}
fn get_pathing_distance(&self, _idx1: i32, _idx2: i32) -> f32 {
0.0
}
}
impl Algorithm2D for State {
fn point2d_to_index(&self, pt: Point) -> i32 {
xy_idx(pt.x, pt.y) as i32
}
fn index_to_point2d(&self, idx: i32) -> Point {
Point::new(idx % 80, idx / 80)
}
}
fn main() { fn main() {
let context = let context =
Rltk::init_simple8x8(80, 50, "Mortal Wombat", "resources"); Rltk::init_simple8x8(80, 50, "Mortal Wombat", "resources");