From 4bb710c6c6df8a24a2efa8033ad3c17663345dbd Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 3 Feb 2020 15:50:36 +0100 Subject: output is a system now too --- src/systems.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/systems.rs') 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>, (width, height): (i32, i32)) -> (Vec, Vec>){ + let size = width * height; + let mut values :Vec = Vec::with_capacity(size as usize); + let mut mapping: Vec> = Vec::new(); + for y in 0..height { + for x in 0..width { + let sprites: Vec = 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) +} + -- cgit