tmx/layer.rs
1use super::*;
2use glam::{IVec2, UVec2, Vec4};
3
4/// A layer
5pub enum Layer {
6 /// A layer densely populated with tiles.
7 TileLayer {
8 /// The amount of tiles in the x and y axis.
9 size: UVec2,
10 /// Position offset of the layer, measured in tiles.
11 position: IVec2,
12 /// Position offset of the layer, measured in pixels.
13 offset: IVec2,
14 /// Parallax factor for this layer.
15 parallax: Vec2,
16 /// Color to multiply the contents of this layer with.
17 color: Vec4,
18 /// Whether this layer is visible or not.
19 /// Contents of invisible layers will have their `Draw` component set to invisible.
20 visible: bool,
21 /// Tile data (global tile ids) for this layer, row by row.
22 data: Vec<u32>,
23 },
24 /// A layer populated with individual objects.
25 ObjectLayer {
26 /// Whether to draw objects ordered by index of appearance (true) or y coordinate (false).
27 draworder_index: bool,
28 /// The objects in the layer.
29 objects: Vec<Object>,
30 /// Position offset of the layer, measured in tiles.
31 offset: IVec2,
32 /// Parallax factor for this layer.
33 parallax: Vec2,
34 /// Color to multiply the contents of this layer with.
35 color: Vec4,
36 /// Whether this layer is visible or not.
37 /// Contents of invisible layers will have their `Draw` component set to invisible.
38 visible: bool,
39 },
40 /// A layer populated with a single big image, like a background.
41 ImageLayer {
42 /// The image contained in this layer.
43 image: Texture,
44 /// Position offset of the layer, measured in tiles.
45 offset: IVec2,
46 /// Parallax factor for this layer.
47 parallax: Vec2,
48 /// Color to multiply the contents of this layer with.
49 color: Vec4,
50 /// Whether this layer is visible or not.
51 /// Contents of invisible layers will have their `Draw` component set to invisible.
52 visible: bool,
53 },
54 /// A set of layers grouped together, mainly for convenience in the map editor.
55 Group {
56 /// The layers that were grouped together.
57 layers: Vec<Layer>,
58 },
59}
60
61impl Layer {
62 /*pub(crate) fn set_visible(&mut self, new_visible: bool) {
63 match self {
64 Layer::TileLayer { visible, .. }
65 | Layer::ObjectLayer { visible, .. }
66 | Layer::ImageLayer { visible, .. } => *visible = new_visible,
67 Layer::Group { layers } => {
68 for l in layers.iter_mut() {
69 l.set_visible(new_visible);
70 }
71 }
72 }
73 }*/
74
75 pub(crate) fn add_offset(&mut self, x: i32, y: i32) {
76 match self {
77 Layer::TileLayer { offset, .. } => {
78 offset.x += x;
79 offset.y += y;
80 }
81 Layer::ObjectLayer { offset, .. } => {
82 offset.x += x;
83 offset.y += y;
84 }
85 Layer::ImageLayer { offset, .. } => {
86 offset.x += x;
87 offset.y += y;
88 }
89 Layer::Group { layers } => {
90 for l in layers.iter_mut() {
91 l.add_offset(x, y);
92 }
93 }
94 }
95 }
96
97 pub(crate) fn mul_parallax(&mut self, x: f32, y: f32) {
98 match self {
99 Layer::TileLayer { parallax, .. } => {
100 parallax.x *= x;
101 parallax.y *= y;
102 }
103 Layer::ObjectLayer { parallax, .. } => {
104 parallax.x *= x;
105 parallax.y *= y;
106 }
107 Layer::ImageLayer { parallax, .. } => {
108 parallax.x *= x;
109 parallax.y *= y;
110 }
111 Layer::Group { layers } => {
112 for l in layers.iter_mut() {
113 l.mul_parallax(x, y);
114 }
115 }
116 }
117 }
118
119 pub(crate) fn mul_color(&mut self, o: Vec4) {
120 match self {
121 Layer::TileLayer { color, .. }
122 | Layer::ObjectLayer { color, .. }
123 | Layer::ImageLayer { color, .. } => {
124 *color *= o;
125 }
126 Layer::Group { layers } => {
127 for l in layers.iter_mut() {
128 l.mul_color(o);
129 }
130 }
131 }
132 }
133}