diff options
| author | troido <troido@protonmail.com> | 2020-02-03 15:50:36 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-03 15:50:36 +0100 |
| commit | 4bb710c6c6df8a24a2efa8033ad3c17663345dbd (patch) | |
| tree | 0803959069b625882223fdc765d353adc2b18f32 /src/systems.rs | |
| parent | 4cf72119121f1b6d3f85a5f4279b6fffaf8138e9 (diff) | |
output is a system now too
Diffstat (limited to 'src/systems.rs')
| -rw-r--r-- | src/systems.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/systems.rs b/src/systems.rs index ac19b45..41f7d18 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -1,6 +1,7 @@ use std::collections::{HashMap, HashSet}; + use specs::{ ReadStorage, WriteStorage, @@ -29,9 +30,16 @@ use super::resources::{ Size, Floor, Input, + Output, NewEntities }; +use super::worldmessages::{ + WorldMessage, + WorldUpdate, + FieldMessage +}; + use super::assemblages::Player; @@ -125,3 +133,52 @@ impl <'a> System<'a> for ClearControllers { } } +pub struct View; +impl <'a> System<'a> for View { + type SystemData = (Read<'a, TopView>, Read<'a, Size>, ReadStorage<'a, Played>, Write<'a, Output>); + fn run(&mut self, (topview, size, players, mut output): Self::SystemData) { + + + let width = size.width; + let height = size.height; + let (values, mapping) = draw_room(&topview.cells, (width, height)); + + let message = WorldMessage{updates: vec![WorldUpdate::Field(FieldMessage{ + width, + height, + field: values, + mapping + })]}; + output.output.clear(); + for player in (&players).join() { + output.output.insert(player.name.clone(), message.clone()); + } + } +} + +fn draw_room(cells: &HashMap<Position, Vec<Visible>>, (width, height): (i32, i32)) -> (Vec<usize>, Vec<Vec<String>>){ + let size = width * height; + let mut values :Vec<usize> = Vec::with_capacity(size as usize); + let mut mapping: Vec<Vec<String>> = Vec::new(); + for y in 0..height { + for x in 0..width { + let sprites: Vec<String> = match cells.get(&Position{x: x, y: y}) { + Some(sprites) => {sprites.iter().map(|v| v.sprite.clone()).collect()} + None => {vec![]} + }; + values.push( + match mapping.iter().position(|x| x == &sprites) { + Some(index) => { + index + } + None => { + mapping.push(sprites); + mapping.len() - 1 + } + } + ) + } + } + (values, mapping) +} + |
