diff options
author | Griffin Smith <root@gws.fyi> | 2019-07-28T21·45-0400 |
---|---|---|
committer | Griffin Smith <root@gws.fyi> | 2019-07-28T21·45-0400 |
commit | 6c1eba67629504f10fa08ee68fb31f507c99b0d1 (patch) | |
tree | d5af8f3eb6dc32a1308a20863e5c8814a4634098 /src/entities/environment.rs | |
parent | f22bcad817ee354b355d29b6b289894e2d15cfaa (diff) |
Allow converting generated levels to entities
Add a new Wall entity, and allow converting generated levels to entity maps containing them, then finally displaying them using some of the (now expanded) box drawing machinery.
Diffstat (limited to 'src/entities/environment.rs')
-rw-r--r-- | src/entities/environment.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/entities/environment.rs b/src/entities/environment.rs new file mode 100644 index 000000000000..64366a505496 --- /dev/null +++ b/src/entities/environment.rs @@ -0,0 +1,34 @@ +use crate::display; +use crate::display::draw_box::{BoxStyle, Stylable}; +use crate::entities::Entity; +use crate::types::{Neighbors, Position}; +use std::io::{self, Write}; + +entity! { + pub struct Wall { + pub style: BoxStyle + } +} + +impl Wall { + pub fn new(position: Position, style: BoxStyle) -> Self { + new_entity!(Wall { position, style }) + } +} + +impl display::DrawWithNeighbors for Wall { + fn do_draw_with_neighbors<'a, 'b>( + &'a self, + out: &'b mut Write, + neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, + ) -> io::Result<()> { + let neighbor_styles: Neighbors<Option<BoxStyle>> = + neighbors.map(|es| { + es.iter() + .filter_map(|e| e.downcast_ref::<Wall>()) + .map(|wall| wall.style) + .next() + }); + write!(out, "{}", neighbor_styles.style(self.style)) + } +} |