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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
use specs::{
ReadStorage,
WriteStorage,
Read,
Write,
System,
Join,
Entities
};
use crate::{
Pos,
Sprite,
components::{Visible, Player, Position, Inventory, New, Health, Ear},
resources::{Size, Output, Ground},
worldmessages::{WorldMessage, FieldMessage}
};
pub struct View;
impl <'a> System<'a> for View {
type SystemData = (
Entities<'a>,
ReadStorage<'a, Position>,
ReadStorage<'a, Inventory>,
ReadStorage<'a, Health>,
ReadStorage<'a, Visible>,
Read<'a, Size>,
ReadStorage<'a, Player>,
Write<'a, Output>,
ReadStorage<'a, New>,
Read<'a, Ground>,
WriteStorage<'a, Ear>
);
fn run(&mut self, (entities, positions, inventories, healths, visible, size, players, mut output, new, ground, mut ears): Self::SystemData) {
let changes: Vec<(Pos, Vec<Sprite>)> = ground.changes
.iter()
.map(|pos| (*pos, cell_sprites(ground.components_on(*pos, &visible))))
.collect();
let has_changed: bool = !changes.is_empty();
output.output.clear();
for (ent, player, pos) in (&entities, &players, &positions).join() {
let mut updates = WorldMessage::default();
if new.get(ent).is_some() {
let (values, mapping) = draw_room(&ground, (size.width, size.height), &visible);
let field = FieldMessage{
width: size.width,
height: size.height,
field: values,
mapping
};
updates.field = Some(field);
} else if has_changed {
updates.change = Some(changes.clone());
}
if let Some(inventory) = inventories.get(ent){
updates.inventory = Some(inventory.items.iter().map(|entry| (entry.item.name.clone(), entry.is_equipped)).collect());
}
if let Some(health) = healths.get(ent){
updates.health = Some((health.health, health.maxhealth));
}
if let Some(ear) = ears.get_mut(ent){
if !ear.sounds.is_empty(){
updates.sounds = Some(ear.sounds.drain(..).map(|s| s.as_message()).collect());
}
}
updates.ground = Some(
ground
.by_height(&pos.pos, &visible, &ent)
.into_iter()
.map(|ent| visible.get(ent).unwrap().name.clone())
.collect()
);
updates.pos = Some(pos.pos);
if !updates.is_empty() {
output.output.insert(player.id.clone(), updates);
}
}
}
}
fn cell_sprites(mut visibles: Vec<&Visible>) -> Vec<Sprite> {
visibles.sort_by(|a, b| b.height.partial_cmp(&a.height).unwrap());
visibles.iter().map(|vis| vis.sprite.clone()).collect()
}
fn draw_room(ground: &Read<Ground>, (width, height): (i64, i64), visible: &ReadStorage<Visible>) -> (Vec<usize>, Vec<Vec<Sprite>>){
let size = width * height;
let mut values :Vec<usize> = Vec::with_capacity(size as usize);
let mut mapping: Vec<Vec<Sprite>> = Vec::new();
for y in 0..height {
for x in 0..width {
let sprites: Vec<Sprite> = cell_sprites(ground.components_on(Pos{x, y}, visible));
values.push(
match mapping.iter().position(|x| x == &sprites) {
Some(index) => {
index
}
None => {
mapping.push(sprites);
mapping.len() - 1
}
}
)
}
}
(values, mapping)
}
|