diff --git a/src/level.rs b/src/level.rs index 79df7d6e..9355a7cd 100644 --- a/src/level.rs +++ b/src/level.rs @@ -481,6 +481,7 @@ pub fn spawn_level( layer_instance.c_wid, layer_instance.c_hei, layer_instance.grid_size, + i, ), layer_instance.opacity, ), @@ -549,24 +550,25 @@ pub fn spawn_level( layer_instance.c_hei as u32, ).expect("int_grid_csv indices should be within the bounds of 0..(layer_width * layer_height)"); - let tile_entity = storage.get(&grid_coords.into()).unwrap(); - - let mut entity_commands = commands.entity(tile_entity); - - let default_ldtk_int_cell: Box = - Box::new(PhantomLdtkIntCell::::new()); - - ldtk_map_get_or_default( - layer_instance.identifier.clone(), - *value, - &default_ldtk_int_cell, - ldtk_int_cell_map, - ) - .evaluate( - &mut entity_commands, - IntGridCell { value: *value }, - layer_instance, - ); + if let Some(tile_entity) = storage.get(&grid_coords.into()) { + let mut entity_commands = commands.entity(tile_entity); + + let default_ldtk_int_cell: Box< + dyn PhantomLdtkIntCellTrait, + > = Box::new(PhantomLdtkIntCell::::new()); + + ldtk_map_get_or_default( + layer_instance.identifier.clone(), + *value, + &default_ldtk_int_cell, + ldtk_int_cell_map, + ) + .evaluate( + &mut entity_commands, + IntGridCell { value: *value }, + layer_instance, + ); + } } } diff --git a/src/tile_makers.rs b/src/tile_makers.rs index 6f7ac5d4..65a64164 100644 --- a/src/tile_makers.rs +++ b/src/tile_makers.rs @@ -154,7 +154,7 @@ pub(crate) fn tile_pos_to_tile_if_int_grid_nonzero_maker( /// Creates a tile maker that returns one of the following: /// 1. Returns a tile that matches the tileset visual of the ldtk layer, if it exists -/// 2. Returns an invisible tile, if the corresponding intgrid position is nonzero, +/// 2. Returns an invisible tile, if the corresponding intgrid position is nonzero and the sublayer index is 0, /// 3. Returns none /// /// Used for spawning IntGrid layers with AutoTile functionality. @@ -164,12 +164,20 @@ pub(crate) fn tile_pos_to_int_grid_with_grid_tiles_tile_maker( layer_width_in_tiles: i32, layer_height_in_tiles: i32, layer_grid_size: i32, + sublayer_index: usize, ) -> impl FnMut(TilePos) -> Option { // Creating the tile makers outside of the returned tile maker so we only do it once. let mut auto_tile_maker = tile_pos_to_tile_maker(grid_tiles, layer_height_in_tiles, layer_grid_size); + + let invis_tile_type = if sublayer_index == 0 { + tile_pos_to_invisible_tile + } else { + |_| None + }; + let mut invisible_tile_maker = tile_pos_to_tile_if_int_grid_nonzero_maker( - tile_pos_to_invisible_tile, + invis_tile_type, int_grid_csv, layer_width_in_tiles, layer_height_in_tiles, @@ -372,8 +380,15 @@ mod tests { let int_grid_csv = vec![1, 0, 2, 0]; - let mut tile_maker = - tile_pos_to_int_grid_with_grid_tiles_tile_maker(&grid_tiles, &int_grid_csv, 2, 2, 32); + // Test when sublayer index is 0. Invisibile tiles should be created + let mut tile_maker = tile_pos_to_int_grid_with_grid_tiles_tile_maker( + &grid_tiles, + &int_grid_csv, + 2, + 2, + 32, + 0, + ); assert_eq!( tile_maker(TilePos { x: 0, y: 0 }).unwrap().texture_index.0, @@ -394,6 +409,32 @@ mod tests { 2 ); assert!(tile_maker(TilePos { x: 1, y: 1 }).unwrap().visible.0); + + // Test when sublayer index isn't 0. There should be no invisible tiles + let mut tile_maker = tile_pos_to_int_grid_with_grid_tiles_tile_maker( + &grid_tiles, + &int_grid_csv, + 2, + 2, + 32, + 1, + ); + + assert!(tile_maker(TilePos { x: 0, y: 0 }).is_none()); + + assert!(tile_maker(TilePos { x: 1, y: 0 }).is_none()); + + assert_eq!( + tile_maker(TilePos { x: 0, y: 1 }).unwrap().texture_index.0, + 1 + ); + assert!(tile_maker(TilePos { x: 0, y: 1 }).unwrap().visible.0); + + assert_eq!( + tile_maker(TilePos { x: 1, y: 1 }).unwrap().texture_index.0, + 2 + ); + assert!(tile_maker(TilePos { x: 1, y: 1 }).unwrap().visible.0); } #[test]