adrien-bon / bevy_ecs_tiled

Helpers for working with 2D tilemaps created with the Tiled map editor
MIT License
34 stars 8 forks source link

When using a map with multiple tilesets, objects are created once per tileset #28

Closed adrien-bon closed 3 months ago

adrien-bon commented 3 months ago

In the load_map function, we iterate over tilesets and layers to create both tiles and objects:

for (tileset_index, tileset) in tiled_map.map.tilesets().iter().enumerate() {
  for (layer_index, layer) in tiled_map.map.layers().enumerate() {
    match layer.layer_type() {
      LayerType::Tiles(tile_layer) => {
        // spawn tiles
      LayerType::Objects(object_layer) => {
        // spawn objects
  }
}

This logic is needed for tiles since a TileLayer can contains tiles from multiple tilesets but with the current implementation, it leads to spawn every object once per tileset which is not what we want.

Solution would be to only iterate over the tileset for LayerType::Tiles layers:

for (layer_index, layer) in tiled_map.map.layers().enumerate() {
  match layer.layer_type() {
    LayerType::Tiles(tile_layer) => {
      for (tileset_index, tileset) in tiled_map.map.tilesets().iter().enumerate() {
        // spawn tiles
      }
    LayerType::Objects(object_layer) => {
      // spawn objects
  }
}