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
use xml::attribute::OwnedAttribute;
use crate::{
util::{get_attrs, map_wrapper, XmlEventResult},
LayerTile, LayerTileData, MapTilesetGid, Result,
};
use super::util::parse_data_line;
#[derive(PartialEq, Clone, Default)]
pub struct FiniteTileLayerData {
width: u32,
height: u32,
tiles: Vec<Option<LayerTileData>>,
}
impl std::fmt::Debug for FiniteTileLayerData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FiniteTileLayerData")
.field("width", &self.width)
.field("height", &self.height)
.finish()
}
}
impl FiniteTileLayerData {
#[inline]
pub fn width(&self) -> u32 {
self.width
}
#[inline]
pub fn height(&self) -> u32 {
self.height
}
pub(crate) fn new(
parser: &mut impl Iterator<Item = XmlEventResult>,
attrs: Vec<OwnedAttribute>,
width: u32,
height: u32,
tilesets: &[MapTilesetGid],
) -> Result<Self> {
let (e, c) = get_attrs!(
attrs,
optionals: [
("encoding", encoding, |v| Some(v)),
("compression", compression, |v| Some(v)),
]
);
let tiles = parse_data_line(e, c, parser, tilesets)?;
Ok(Self {
width,
height,
tiles,
})
}
pub(crate) fn get_tile(&self, x: i32, y: i32) -> Option<&LayerTileData> {
if x < self.width as i32 && y < self.height as i32 && x >= 0 && y >= 0 {
self.tiles[x as usize + y as usize * self.width as usize].as_ref()
} else {
None
}
}
}
map_wrapper!(
#[doc = "A [`TileLayer`](super::TileLayer) with a defined bound (width and height)."]
FiniteTileLayer => FiniteTileLayerData
);
impl<'map> FiniteTileLayer<'map> {
pub fn get_tile(&self, x: i32, y: i32) -> Option<LayerTile> {
self.data
.get_tile(x, y)
.and_then(|data| Some(LayerTile::new(self.map(), data)))
}
}