set up visibility system

This commit is contained in:
Peter Hart 2020-03-28 21:24:02 -04:00
parent 419ab1ba35
commit 2727f417ea
3 changed files with 32 additions and 4 deletions

View File

@ -1,4 +1,4 @@
use rltk::RGB; use rltk::{RGB,Point};
use specs::prelude::*; use specs::prelude::*;
@ -20,3 +20,9 @@ pub struct LeftMover {}
#[derive(Component, Debug)] #[derive(Component, Debug)]
pub struct Player {} pub struct Player {}
#[derive(Component)]
pub struct Viewshed {
pub visible_tiles : Vec<Point>,
pub range : i32
}

View File

@ -7,6 +7,8 @@ mod map;
pub use map::*; pub use map::*;
mod components; mod components;
pub use components::*; pub use components::*;
mod visibility_system;
pub use visibility_system::VisibilitySystem;
#[macro_use] #[macro_use]
extern crate specs_derive; extern crate specs_derive;
@ -23,7 +25,7 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
for (_player, pos) in (&mut players, &mut positions).join() { for (_player, pos) in (&mut players, &mut positions).join() {
let x = min(79, max(0, pos.x + delta_x)); let x = min(79, max(0, pos.x + delta_x));
let y = min(49, max(0, pos.y + delta_y)); let y = min(49, max(0, pos.y + delta_y));
if map.tile_at(x,y) != TileType::Wall { if map.tile_at(x, y) != TileType::Wall {
pos.x = x; pos.x = x;
pos.y = y; pos.y = y;
} }
@ -69,8 +71,8 @@ impl<'a> System<'a> for LeftWalker {
impl State { impl State {
fn run_systems(&mut self) { fn run_systems(&mut self) {
let mut lw = LeftWalker {}; let mut vis = VisibilitySystem {};
lw.run_now(&self.ecs); vis.run_now(&self.ecs);
self.ecs.maintain(); self.ecs.maintain();
} }
} }
@ -104,6 +106,7 @@ fn main() {
gs.ecs.register::<Renderable>(); gs.ecs.register::<Renderable>();
gs.ecs.register::<LeftMover>(); gs.ecs.register::<LeftMover>();
gs.ecs.register::<Player>(); gs.ecs.register::<Player>();
gs.ecs.register::<Viewshed>();
gs.ecs gs.ecs
.create_entity() .create_entity()
@ -117,6 +120,10 @@ fn main() {
bg: RGB::named(rltk::BLACK), bg: RGB::named(rltk::BLACK),
}) })
.with(Player {}) .with(Player {})
.with(Viewshed {
visible_tiles: Vec::new(),
range: 8,
})
.build(); .build();
for i in 0..10 { for i in 0..10 {

15
src/visibility_system.rs Normal file
View File

@ -0,0 +1,15 @@
extern crate specs;
use specs::prelude::*;
use super::{Viewshed, Position};
pub struct VisibilitySystem {}
impl<'a> System<'a> for VisibilitySystem {
type SystemData = ( WriteStorage<'a, Viewshed>,
WriteStorage<'a, Position>);
fn run(&mut self, (mut viewshed, pos) : Self::SystemData) {
for (viewshed,pos) in (&mut viewshed, &pos).join() {
}
}
}