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
|
use ggez::filesystem::File;
use ggez::graphics::Rect;
use std::collections::HashMap;
use crate::constants;
use crate::xmlelements::{Property, XMLElements};
pub struct Tileset {
tiles: HashMap<usize, Rect>,
properties: HashMap<usize, Property>,
}
impl Tileset {
pub fn new(file: File) -> Tileset {
let elements = XMLElements::new(file);
let height = elements
.get_element_attribute("image", "height")
.unwrap()
.parse::<usize>()
.unwrap();
let columns = elements
.get_element_attribute("tileset", "columns")
.unwrap()
.parse::<usize>()
.unwrap();
let rows = height / (constants::TILE_HEIGHT as usize);
let mut tiles = HashMap::new();
tiles.insert(0, Rect::zero());
let w = 1.0 / columns as f32;
let h = 1.0 / rows as f32;
let mut key = 1;
for r in 0..rows {
for c in 0..columns {
let x = c as f32 / columns as f32;
let y = r as f32 / rows as f32;
tiles.insert(key, Rect::new(x, y, w, h));
key += 1;
}
}
let mut properties = HashMap::new();
for tile_element in elements.get_elements("tile") {
let tile_id = XMLElements::get_attribute(&tile_element, "id")
.unwrap()
.parse::<usize>()
.unwrap();
let property_elements = elements.get_children(&tile_element, "property");
properties.insert(tile_id + 1, Property::new(property_elements));
}
Tileset { tiles, properties }
}
pub fn get(&self, id: usize) -> Rect {
*self.tiles.get(&id).unwrap()
}
pub fn get_animation(&self, id: usize) -> Option<Vec<(usize, Rect)>> {
if let Some(property) = self.properties.get(&id) {
let entitys_properties: HashMap<usize, Property> = self
.properties
.clone()
.into_iter()
.filter(|(_, p)| p.entity == property.entity)
.collect();
Some(
entitys_properties
.iter()
.map(|(id, p)| (p.delay, self.get(*id)))
.collect(),
)
} else {
None
}
}
pub fn get_rect_by_entity(&self, entity: &str) -> Rect {
*self
.tiles
.get(
self.properties
.iter()
.find(|(_, p)| p.entity == entity && 0 == p.keyframe)
.unwrap()
.0,
)
.unwrap()
}
}
|