summaryrefslogtreecommitdiff
path: root/src/systems/view.rs
blob: da6d6c4d078c35a221f1119597939fb94445dacb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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)
}