From 2727f417eaddaf24af955344b9b948acdc4183e2 Mon Sep 17 00:00:00 2001 From: Peter Hart Date: Sat, 28 Mar 2020 21:24:02 -0400 Subject: [PATCH] set up visibility system --- src/components.rs | 8 +++++++- src/main.rs | 13 ++++++++++--- src/visibility_system.rs | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 src/visibility_system.rs diff --git a/src/components.rs b/src/components.rs index 6887021..c4f9d34 100644 --- a/src/components.rs +++ b/src/components.rs @@ -1,4 +1,4 @@ -use rltk::RGB; +use rltk::{RGB,Point}; use specs::prelude::*; @@ -20,3 +20,9 @@ pub struct LeftMover {} #[derive(Component, Debug)] pub struct Player {} + +#[derive(Component)] +pub struct Viewshed { + pub visible_tiles : Vec, + pub range : i32 +} diff --git a/src/main.rs b/src/main.rs index 8c4c543..25e8644 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ mod map; pub use map::*; mod components; pub use components::*; +mod visibility_system; +pub use visibility_system::VisibilitySystem; #[macro_use] 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() { let x = min(79, max(0, pos.x + delta_x)); 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.y = y; } @@ -69,8 +71,8 @@ impl<'a> System<'a> for LeftWalker { impl State { fn run_systems(&mut self) { - let mut lw = LeftWalker {}; - lw.run_now(&self.ecs); + let mut vis = VisibilitySystem {}; + vis.run_now(&self.ecs); self.ecs.maintain(); } } @@ -104,6 +106,7 @@ fn main() { gs.ecs.register::(); gs.ecs.register::(); gs.ecs.register::(); + gs.ecs.register::(); gs.ecs .create_entity() @@ -117,6 +120,10 @@ fn main() { bg: RGB::named(rltk::BLACK), }) .with(Player {}) + .with(Viewshed { + visible_tiles: Vec::new(), + range: 8, + }) .build(); for i in 0..10 { diff --git a/src/visibility_system.rs b/src/visibility_system.rs new file mode 100644 index 0000000..c568930 --- /dev/null +++ b/src/visibility_system.rs @@ -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() { + } + } +} \ No newline at end of file