bevy_ecs_tiled 0.11.2

A Bevy plugin for loading Tiled maps
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Asset loader for Tiled maps.
//!
//! This module defines the asset loader implementation for importing Tiled maps into Bevy's asset system.

#[cfg(feature = "user_properties")]
use std::ops::Deref;
use std::sync::Arc;

use crate::{
    prelude::*,
    tiled::{
        cache::TiledResourceCache, helpers::iso_projection, map::asset::TiledMapTileset,
        reader::BytesResourceReader,
    },
};
use bevy::{
    asset::{io::Reader, AssetLoader, AssetPath, LoadContext},
    platform::collections::HashMap,
    prelude::*,
};

#[derive(TypePath)]
struct TiledMapLoader {
    cache: TiledResourceCache,
    #[cfg(feature = "user_properties")]
    registry: bevy::reflect::TypeRegistryArc,
}

pub(crate) fn tileset_label(tileset: &tiled::Tileset) -> Option<String> {
    tileset
        .source
        .to_str()
        .map(|s| format!("{s}#{}", tileset.name))
}

impl FromWorld for TiledMapLoader {
    fn from_world(world: &mut World) -> Self {
        Self {
            cache: world.resource::<TiledResourceCache>().clone(),
            #[cfg(feature = "user_properties")]
            registry: world.resource::<AppTypeRegistry>().0.clone(),
        }
    }
}

/// [`TiledMapAsset`] loading error.
#[derive(Debug, thiserror::Error)]
pub enum TiledMapLoaderError {
    /// An [`IO`](std::io) Error
    #[error("Could not load Tiled file: {0}")]
    Io(#[from] std::io::Error),
}

impl AssetLoader for TiledMapLoader {
    type Asset = TiledMapAsset;
    type Settings = ();
    type Error = TiledMapLoaderError;

    async fn load(
        &self,
        reader: &mut dyn Reader,
        _settings: &Self::Settings,
        load_context: &mut LoadContext<'_>,
    ) -> Result<Self::Asset, Self::Error> {
        let mut bytes = Vec::new();
        reader.read_to_end(&mut bytes).await?;

        debug!("Start loading map '{}'", load_context.path());

        let map_path = load_context.path().path().to_path_buf();
        let map = {
            // Allow the loader to also load tileset images.
            let mut loader = tiled::Loader::with_cache_and_reader(
                self.cache.clone(),
                BytesResourceReader::new(&bytes, load_context),
            );
            // Load the map and all tiles.
            loader
                .load_tmx_map(map_path)
                .map_err(|e| std::io::Error::other(format!("Could not load TMX map: {e}")))?
        };

        let map_type = tilemap_type_from_map(&map);
        let grid_size = grid_size_from_map(&map);
        let mut tilesets = HashMap::default();
        let mut tilesets_label_by_index = HashMap::<u32, String>::default();

        for (tileset_index, tileset) in map.tilesets().iter().enumerate() {
            debug!(
                "Loading tileset (index={:?} name={:?}) from {:?}",
                tileset_index, tileset.name, tileset.source
            );

            let Some(label) = tileset_label(tileset) else {
                continue;
            };

            let Some(tiled_map_tileset) =
                tileset_to_tiled_map_tileset(tileset.clone(), load_context, &label)
            else {
                continue;
            };

            tilesets_label_by_index.insert(tileset_index as u32, label.to_owned());
            tilesets.insert(label.to_owned(), tiled_map_tileset);
        }

        let mut images = HashMap::default();
        let mut largest_tile_size = TilemapTileSize::new(grid_size.x, grid_size.y);
        for (layer_index, layer) in map.layers().enumerate() {
            match layer.layer_type() {
                tiled::LayerType::Tiles(tiles_layer) => {
                    // Iterate over tiles layers to find the largest tile_size of the map
                    match tiles_layer {
                        tiled::TileLayer::Finite(tiles_finite_layer) => {
                            for x in 0..tiles_finite_layer.width() as i32 {
                                for y in 0..tiles_finite_layer.height() as i32 {
                                    let Some(tile) = tiles_finite_layer
                                        .get_tile(x, y)
                                        .and_then(|t| t.get_tile())
                                    else {
                                        continue;
                                    };
                                    let tile_size = tile_size(&tile);
                                    if tile_size > largest_tile_size {
                                        debug!("Update tile_size: {tile_size:?}");
                                        largest_tile_size = tile_size;
                                    }
                                }
                            }
                        }
                        tiled::TileLayer::Infinite(tiles_infinite_layer) => {
                            for (_, chunk) in tiles_infinite_layer.chunks() {
                                for x in 0..tiled::ChunkData::WIDTH as i32 {
                                    for y in 0..tiled::ChunkData::HEIGHT as i32 {
                                        let Some(tile) =
                                            chunk.get_tile(x, y).and_then(|t| t.get_tile())
                                        else {
                                            continue;
                                        };
                                        let tile_size = tile_size(&tile);
                                        if tile_size > largest_tile_size {
                                            debug!("Update tile_size: {tile_size:?}");
                                            largest_tile_size = tile_size;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                tiled::LayerType::Objects(object_layer) => {
                    // Iterate over objects layers to load potential external tilesets (templates)
                    for object_data in object_layer.objects() {
                        let Some(tile) = object_data.get_tile() else {
                            continue;
                        };

                        let tiled::TilesetLocation::Template(tileset) = tile.tileset_location()
                        else {
                            continue;
                        };

                        let Some(label) = tileset_label(tileset) else {
                            continue;
                        };

                        if tilesets.contains_key(&label) {
                            continue;
                        }

                        let Some(tiled_map_tileset) =
                            tileset_to_tiled_map_tileset(tileset.clone(), load_context, &label)
                        else {
                            continue;
                        };

                        tilesets.insert(label.to_owned(), tiled_map_tileset);
                    }
                }
                tiled::LayerType::Image(image_layer) => {
                    // Load image assets used in image layers
                    let Some(image) = &image_layer.image else {
                        continue;
                    };

                    let asset_path = AssetPath::from(image.source.clone());
                    let handle: Handle<Image> = load_context.load(asset_path);

                    images.insert(layer_index as u32, handle);
                }
                _ => continue,
            }
        }

        let mut infinite = false;

        // Determine top left chunk index of all infinite layers for this map
        let mut topleft = (999999, 999999);
        for layer in map.layers() {
            if let tiled::LayerType::Tiles(tiled::TileLayer::Infinite(layer)) = layer.layer_type() {
                topleft = layer.chunks().fold(topleft, |acc, (pos, _)| {
                    (acc.0.min(pos.0), acc.1.min(pos.1))
                });
                infinite = true;
            }
        }
        // Determine bottom right chunk index of all infinite layers for this map
        let mut bottomright = (0, 0);
        for layer in map.layers() {
            if let tiled::LayerType::Tiles(tiled::TileLayer::Infinite(layer)) = layer.layer_type() {
                bottomright = layer.chunks().fold(bottomright, |acc, (pos, _)| {
                    (acc.0.max(pos.0), acc.1.max(pos.1))
                });
                infinite = true;
            }
        }

        let (tilemap_size, tiled_offset) = if infinite {
            debug!(
                "(infinite map) topleft = {:?}, bottomright = {:?}",
                topleft, bottomright
            );
            (
                TilemapSize {
                    x: (bottomright.0 - topleft.0 + 1) as u32 * tiled::ChunkData::WIDTH,
                    y: (bottomright.1 - topleft.1 + 1) as u32 * tiled::ChunkData::HEIGHT,
                },
                match map_type {
                    TilemapType::Square => Vec2 {
                        x: -topleft.0 as f32 * tiled::ChunkData::WIDTH as f32 * grid_size.x,
                        y: topleft.1 as f32 * tiled::ChunkData::HEIGHT as f32 * grid_size.y,
                    },
                    TilemapType::Hexagon(HexCoordSystem::ColumnOdd)
                    | TilemapType::Hexagon(HexCoordSystem::ColumnEven) => Vec2 {
                        x: -topleft.0 as f32 * tiled::ChunkData::WIDTH as f32 * grid_size.x * 0.75,
                        y: topleft.1 as f32 * tiled::ChunkData::HEIGHT as f32 * grid_size.y,
                    },
                    TilemapType::Hexagon(HexCoordSystem::RowOdd)
                    | TilemapType::Hexagon(HexCoordSystem::RowEven) => Vec2 {
                        x: -topleft.0 as f32 * tiled::ChunkData::WIDTH as f32 * grid_size.x,
                        y: topleft.1 as f32 * tiled::ChunkData::HEIGHT as f32 * grid_size.y * 0.75,
                    },
                    TilemapType::Isometric(IsoCoordSystem::Diamond) => Vec2 {
                        x: -topleft.0 as f32 * tiled::ChunkData::WIDTH as f32 * grid_size.y,
                        y: -topleft.1 as f32 * tiled::ChunkData::HEIGHT as f32 * grid_size.y,
                    },
                    TilemapType::Isometric(IsoCoordSystem::Staggered) => {
                        panic!("Isometric (Staggered) map is not supported");
                    }
                    _ => unreachable!(),
                },
            )
        } else {
            topleft = (0, 0);
            bottomright = (0, 0);
            (
                TilemapSize {
                    x: map.width,
                    y: map.height,
                },
                Vec2::ZERO,
            )
        };

        let rect = Rect {
            min: Vec2::ZERO,
            max: match map_type {
                TilemapType::Square => Vec2 {
                    x: tilemap_size.x as f32 * grid_size.x,
                    y: tilemap_size.y as f32 * grid_size.y,
                },
                TilemapType::Hexagon(HexCoordSystem::ColumnOdd)
                | TilemapType::Hexagon(HexCoordSystem::ColumnEven) => Vec2 {
                    x: tilemap_size.x as f32 * grid_size.x * 0.75,
                    y: tilemap_size.y as f32 * grid_size.y,
                },
                TilemapType::Hexagon(HexCoordSystem::RowOdd)
                | TilemapType::Hexagon(HexCoordSystem::RowEven) => Vec2 {
                    x: tilemap_size.x as f32 * grid_size.x,
                    y: tilemap_size.y as f32 * grid_size.y * 0.75,
                },
                TilemapType::Isometric(IsoCoordSystem::Diamond) => {
                    let topleft = iso_projection(Vec2::ZERO, &tilemap_size, &grid_size);
                    let topright = iso_projection(
                        Vec2 {
                            x: tilemap_size.x as f32 * grid_size.y,
                            y: 0.,
                        },
                        &tilemap_size,
                        &grid_size,
                    );

                    2. * (topright - topleft)
                }
                TilemapType::Isometric(IsoCoordSystem::Staggered) => {
                    panic!("Isometric (Staggered) map is not supported");
                }
                _ => unreachable!(),
            },
        };

        #[cfg(feature = "user_properties")]
        let properties = crate::tiled::properties::load::DeserializedMapProperties::load(
            &map,
            self.registry.read().deref(),
            load_context,
        );

        #[cfg(feature = "user_properties")]
        trace!(?properties, "user properties");
        trace!(?tilesets, "tilesets");

        let asset_map = TiledMapAsset {
            map,
            tilemap_size,
            largest_tile_size,
            tiled_offset,
            rect,
            topleft_chunk: topleft,
            bottomright_chunk: bottomright,
            tilesets,
            tilesets_label_by_index,
            images,
            #[cfg(feature = "user_properties")]
            properties,
        };
        debug!("Loaded map '{}': {:?}", load_context.path(), &asset_map,);
        Ok(asset_map)
    }

    fn extensions(&self) -> &[&str] {
        static EXTENSIONS: &[&str] = &["tmx"];
        EXTENSIONS
    }
}

fn tileset_to_tiled_map_tileset(
    tileset: Arc<tiled::Tileset>,
    load_context: &mut LoadContext<'_>,
    path: &str,
) -> Option<TiledMapTileset> {
    #[cfg(not(feature = "atlas"))]
    let tileset_path = tileset.source.to_str()?;

    let mut texture_atlas_layout_handle = None;
    #[cfg(not(feature = "atlas"))]
    let mut tile_image_offsets = HashMap::default();
    let (usable_for_tiles_layer, tilemap_texture) = match &tileset.image {
        None => {
            #[cfg(feature = "atlas")]
            {
                info!("Skipping image collection tileset '{}' which is incompatible with atlas feature", tileset.name);
                return None;
            }

            #[cfg(not(feature = "atlas"))]
            {
                let mut usable_for_tiles_layer = true;
                let mut image_size: Option<(i32, i32)> = None;
                let mut tile_images: Vec<Handle<Image>> = Vec::new();
                for (tile_id, tile) in tileset.tiles() {
                    if let Some(img) = &tile.image {
                        let asset_path = AssetPath::from(img.source.clone());
                        trace!("Loading tile image from {asset_path:?} as image ({tileset_path}, {tile_id})");
                        let texture: Handle<Image> = load_context.load(asset_path.clone());
                        tile_image_offsets.insert(tile_id, tile_images.len() as u32);
                        tile_images.push(texture.clone());
                        if usable_for_tiles_layer {
                            if let Some(image_size) = image_size {
                                if img.width != image_size.0 || img.height != image_size.1 {
                                    usable_for_tiles_layer = false;
                                }
                            } else {
                                image_size = Some((img.width, img.height));
                            }
                        }
                    }
                }
                if !usable_for_tiles_layer {
                    debug!(
                        "Tileset (path={:?}) have non constant image size and cannot be used for tiles layer",
                        tileset_path
                    );
                }
                (usable_for_tiles_layer, TilemapTexture::Vector(tile_images))
            }
        }
        Some(img) => {
            let asset_path = AssetPath::from(img.source.clone());
            let texture: Handle<Image> = load_context.load(asset_path);

            let columns = (img.width as u32 - tileset.margin + tileset.spacing)
                / (tileset.tile_width + tileset.spacing);
            if columns > 0 {
                texture_atlas_layout_handle = load_context
                    .labeled_asset_scope(path.to_owned(), |_| {
                        Ok::<_, ()>(TextureAtlasLayout::from_grid(
                            UVec2::new(tileset.tile_width, tileset.tile_height),
                            columns,
                            tileset.tilecount / columns,
                            Some(UVec2::splat(tileset.spacing)),
                            Some(UVec2::splat(tileset.margin)),
                        ))
                    })
                    .ok();
            }

            (true, TilemapTexture::Single(texture.clone()))
        }
    };

    Some(TiledMapTileset {
        usable_for_tiles_layer,
        tilemap_texture,
        texture_atlas_layout_handle,
        #[cfg(not(feature = "atlas"))]
        tile_image_offsets,
    })
}

pub(crate) fn plugin(app: &mut App) {
    app.init_asset_loader::<TiledMapLoader>();
}