summaryrefslogtreecommitdiff
path: root/src/systems/view.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-03 16:13:58 +0100
committertroido <troido@protonmail.com>2020-02-03 16:13:58 +0100
commitf0153eefd580ec443b380504303620a61f24630b (patch)
tree98fe9cb6a9039d50b180ea774396e2d162a41656 /src/systems/view.rs
parent4bb710c6c6df8a24a2efa8033ad3c17663345dbd (diff)
refactored systems in their own file each
Diffstat (limited to 'src/systems/view.rs')
-rw-r--r--src/systems/view.rs82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/systems/view.rs b/src/systems/view.rs
new file mode 100644
index 0000000..da6d6c4
--- /dev/null
+++ b/src/systems/view.rs
@@ -0,0 +1,82 @@
+
+use std::collections::HashMap;
+
+use specs::{
+ ReadStorage,
+ Read,
+ Write,
+ System,
+ Join
+};
+
+use super::super::components::{
+ Position,
+ Visible,
+ Played
+};
+
+
+use super::super::resources::{
+ TopView,
+ Size,
+ Output,
+};
+
+use super::super::worldmessages::{
+ WorldMessage,
+ WorldUpdate,
+ FieldMessage
+};
+
+
+
+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)
+}