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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! ECS components for Tiled objects.
//!
//! This module defines Bevy components used to represent Tiled objects within the ECS world.
use crate::prelude::{geo::Centroid, *};
use crate::tiled::helpers::iso_projection;
use bevy::prelude::*;
/// Relationship and Marker [`Component`] for the visual representation of a [`TiledObject`].
///
/// Added on the child [`Entity`] of a [`TiledObject::Tile`].
/// These entity have an associated [`Sprite`] and eventually a [`TiledAnimation`] component.
#[derive(Component, Reflect, Copy, Clone, Debug, Deref)]
#[reflect(Component, Debug)]
#[require(Visibility, Transform, Sprite)]
#[relationship(relationship_target = TiledObjectVisuals)]
pub struct TiledObjectVisualOf(pub Entity);
/// Relationship target [`Component`] pointing to a single child [`TiledObjectVisualOf`]s.
#[derive(Component, Reflect, Debug, Deref)]
#[reflect(Component, Debug)]
#[relationship_target(relationship = TiledObjectVisualOf)]
pub struct TiledObjectVisuals(Vec<Entity>);
/// Tiled object ID associated with this [`TiledObject`].
///
/// You can retrieve the corresponding [`tiled::Object`] using the [`get_object_from_map`] helper function.
#[derive(Component, Default, Reflect, Copy, Clone, Debug, Deref)]
#[reflect(Component, Default, Debug)]
pub struct TiledObjectId(pub u32);
/// Marker [`Component`] for a Tiled map object.
#[derive(Component, Default, Reflect, Clone, Debug)]
#[reflect(Component, Default, Debug)]
#[require(Visibility, Transform, TiledName, TiledObjectId)]
pub enum TiledObject {
/// A point shape.
#[default]
Point,
/// A rectangle shape.
///
/// Anchor is at the top-left corner of the rect.
Rectangle {
/// The width of the rectangle.
width: f32,
/// The height of the rectangle.
height: f32,
},
/// An ellipse shape.
///
/// Anchor is at the top-left corner of the ellipse.
Ellipse {
/// The width of the ellipse.
width: f32,
/// The height of the ellipse.
height: f32,
},
/// A polygon shape.
Polygon {
/// The vertices of the polygon, relative to object center.
vertices: Vec<Vec2>,
},
/// A polyline shape.
Polyline {
/// The vertices of the polyline, relative to object center.
vertices: Vec<Vec2>,
},
/// A tile object, which is a reference to a tile in a tilemap.
///
/// Anchor is at the bottom-left corner of the tile.
/// These objects have a child [`TiledObjectVisualOf`] entity holding
/// their visual representation, which is usually a [`Sprite`].
Tile {
/// The width of the tile.
width: f32,
/// The height of the tile.
height: f32,
},
/// A text object, which contains text data.
///
/// Not supported yet.
Text,
}
impl TiledObject {
const ELLIPSE_NUM_POINTS: u32 = 20;
/// Creates a new [`TiledObject`] from the provided [`tiled::ObjectData`].
pub fn from_object_data(object_data: &tiled::ObjectData) -> Self {
if object_data.tile_data().is_some() {
if let tiled::ObjectShape::Rect { width, height } = object_data.shape {
TiledObject::Tile { width, height }
} else {
warn!(
"Object with tile data should have a rectangle shape, but found {:?}",
object_data.shape
);
TiledObject::default()
}
} else {
match object_data.shape.clone() {
tiled::ObjectShape::Point { .. } => TiledObject::Point,
tiled::ObjectShape::Rect { width, height } => {
TiledObject::Rectangle { width, height }
}
tiled::ObjectShape::Ellipse { width, height } => {
TiledObject::Ellipse { width, height }
}
tiled::ObjectShape::Polygon { points } => TiledObject::Polygon {
vertices: points.into_iter().map(|(x, y)| Vec2::new(x, -y)).collect(),
},
tiled::ObjectShape::Polyline { points } => TiledObject::Polyline {
vertices: points.into_iter().map(|(x, y)| Vec2::new(x, -y)).collect(),
},
tiled::ObjectShape::Text { .. } => {
log::warn!("Text objects are not supported yet");
TiledObject::Text
}
}
}
}
/// Apply rotation and scaling to given coordinate.
fn apply_rotation_and_scaling(
inverse_rotation: bool,
vertex: Vec2,
transform: &GlobalTransform,
) -> Vec2 {
let mut rotation = transform.rotation().to_euler(EulerRot::ZYX).0;
if inverse_rotation {
rotation *= -1.;
}
let (cos, sin) = (rotation.cos(), rotation.sin());
Vec2 {
x: (vertex.x * cos - vertex.y * sin) * transform.scale().x,
y: (vertex.x * sin + vertex.y * cos) * transform.scale().y,
}
}
/// Returns the center position of the object in world space.
///
/// The center is computed from the object's vertices, taking into account its shape and transformation.
///
/// # Arguments
/// * `transform` - The global transform to apply to the object.
/// * `isometric_projection` - Wheter or not to perform an isometric projection.
/// * `tilemap_size` - Size of the tilemap in tiles.
/// * `grid_size` - Size of each tile on the grid in pixels.
/// * `offset` - Global map offset to apply.
///
/// # Returns
/// * `Option<geo::Coord<f32>>` - The computed center, or `None` if not applicable.
pub fn center(
&self,
transform: &GlobalTransform,
isometric_projection: bool,
tilemap_size: &TilemapSize,
grid_size: &TilemapGridSize,
offset: Vec2,
) -> Option<geo::Coord<f32>> {
geo::MultiPoint::from(self.vertices(
transform,
isometric_projection,
tilemap_size,
grid_size,
offset,
))
.centroid()
.map(|p| geo::Coord { x: p.x(), y: p.y() })
}
/// Returns the vertices of the object in world space.
///
/// Vertices are calculated based on the object's shape and its transformation (translation, rotation, scale).
/// For isometric maps, the vertices are projected through the isometric transformation.
///
/// # Arguments
/// * `transform` - The global transform to apply to the object.
/// * `isometric_projection` - Wheter or not to perform an isometric projection.
/// * `tilemap_size` - Size of the tilemap in tiles.
/// * `grid_size` - Size of each tile on the grid in pixels.
/// * `offset` - Global map offset to apply.
///
/// # Returns
/// * `Vec<geo::Coord<f32>>` - The transformed vertices.
pub fn vertices(
&self,
transform: &GlobalTransform,
isometric_projection: bool,
tilemap_size: &TilemapSize,
grid_size: &TilemapGridSize,
offset: Vec2,
) -> Vec<geo::Coord<f32>> {
// Get object world position
let object_world_pos = geo::Coord {
x: transform.translation().x,
y: transform.translation().y,
};
// Generate shape vertices relative to origin
match self {
TiledObject::Point | TiledObject::Text => vec![Vec2::ZERO],
TiledObject::Tile { width, height } => {
vec![
Vec2::new(0., 0.), // Bottom-left relative to object
Vec2::new(0., *height), // Top-left
Vec2::new(*width, *height), // Top-right
Vec2::new(*width, 0.), // Bottom-right
]
}
TiledObject::Rectangle { width, height } => {
vec![
Vec2::new(0., 0.), // Top-left relative to object
Vec2::new(*width, 0.), // Top-right
Vec2::new(*width, -*height), // Bottom-right
Vec2::new(0., -*height), // Bottom-left
]
}
TiledObject::Ellipse { width, height } => (0..Self::ELLIPSE_NUM_POINTS)
.map(|i| {
let theta =
2.0 * std::f32::consts::PI * (i as f32) / (Self::ELLIPSE_NUM_POINTS as f32);
let local_x = width / 2.0 * theta.cos() + width / 2.0;
let local_y = height / 2.0 * theta.sin() - height / 2.0;
Vec2::new(local_x, local_y)
})
.collect(),
TiledObject::Polyline { vertices } | TiledObject::Polygon { vertices } => {
vertices.clone()
}
}
.into_iter()
.map(|v| {
// Only perform isometric projection if requested by caller and if we do not handle a Tile
if isometric_projection && !matches!(self, TiledObject::Tile { .. }) {
let offset_projected = iso_projection(
Vec2::new(offset.x + v.x, offset.y - v.y),
tilemap_size,
grid_size,
);
let origin_projected = iso_projection(offset, tilemap_size, grid_size);
let relative_projected = offset_projected - origin_projected;
let v = Self::apply_rotation_and_scaling(true, relative_projected, transform);
geo::Coord {
x: object_world_pos.x + v.x,
y: object_world_pos.y - v.y,
}
} else {
let v = Self::apply_rotation_and_scaling(false, v, transform);
geo::Coord {
x: v.x + object_world_pos.x,
y: v.y + object_world_pos.y,
}
}
})
.collect()
}
/// Creates a [`geo::LineString`] from the object's vertices.
///
/// Returns `None` for point and text objects.
/// For ellipses, rectangles, tiles, and polygons, returns a closed line string.
/// For polylines, returns an open line string.
///
/// # Arguments
/// * `transform` - The global transform to apply to the object.
/// * `isometric_projection` - Wheter or not to perform an isometric projection.
/// * `tilemap_size` - Size of the tilemap in tiles.
/// * `grid_size` - Size of each tile on the grid in pixels.
/// * `offset` - Global map offset to apply.
///
/// # Returns
/// * `Option<geo::LineString<f32>>` - The resulting line string, or `None` if not applicable.
pub fn line_string(
&self,
transform: &GlobalTransform,
isometric_projection: bool,
tilemap_size: &TilemapSize,
grid_size: &TilemapGridSize,
offset: Vec2,
) -> Option<geo::LineString<f32>> {
let coords = self.vertices(
transform,
isometric_projection,
tilemap_size,
grid_size,
offset,
);
match self {
TiledObject::Point | TiledObject::Text => None,
TiledObject::Ellipse { .. }
| TiledObject::Rectangle { .. }
| TiledObject::Tile { .. }
| TiledObject::Polygon { .. } => {
let mut line_string = geo::LineString::from(coords);
line_string.close();
Some(line_string)
}
TiledObject::Polyline { .. } => Some(geo::LineString::new(coords)),
}
}
/// Creates a [`geo::Polygon`] from the object's vertices.
///
/// Returns `None` for polyline, point, and text objects.
/// For closed shapes, returns the corresponding polygon.
///
/// # Arguments
/// * `transform` - The global transform to apply to the object.
/// * `isometric_projection` - Wheter or not to perform an isometric projection.
/// * `tilemap_size` - Size of the tilemap in tiles.
/// * `grid_size` - Size of each tile on the grid in pixels.
/// * `offset` - Global map offset to apply.
///
/// # Returns
/// * `Option<geo::Polygon<f32>>` - The resulting polygon, or `None` if not applicable.
pub fn polygon(
&self,
transform: &GlobalTransform,
isometric_projection: bool,
tilemap_size: &TilemapSize,
grid_size: &TilemapGridSize,
offset: Vec2,
) -> Option<geo::Polygon<f32>> {
self.line_string(
transform,
isometric_projection,
tilemap_size,
grid_size,
offset,
)
.and_then(|ls| match ls.is_closed() {
true => Some(geo::Polygon::new(ls, vec![])),
false => None,
})
}
}
pub(crate) fn plugin(app: &mut App) {
app.register_type::<TiledObject>();
app.register_type::<TiledObjectId>();
app.register_type::<TiledObjectVisualOf>();
app.register_type::<TiledObjectVisuals>();
}