Skip to content

Commit

Permalink
fix: only spawn invisible tiles on first sub-layer of AutoTile+IntGri…
Browse files Browse the repository at this point in the history
…d layers (#231)

Another possible solution to #227. Based off of the previous settings
PR, this changes it so that instead of passing a setting to spawn
invisible tiles or not, it passes the sublayer index.
  • Loading branch information
MScottMcBee authored Sep 19, 2023
1 parent f003127 commit d2873e3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
38 changes: 20 additions & 18 deletions src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ pub fn spawn_level(
layer_instance.c_wid,
layer_instance.c_hei,
layer_instance.grid_size,
i,
),
layer_instance.opacity,
),
Expand Down Expand Up @@ -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<dyn PhantomLdtkIntCellTrait> =
Box::new(PhantomLdtkIntCell::<IntGridCellBundle>::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::<IntGridCellBundle>::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,
);
}
}
}

Expand Down
49 changes: 45 additions & 4 deletions src/tile_makers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<TileBundle> {
// 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,
Expand Down Expand Up @@ -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,
Expand All @@ -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]
Expand Down

0 comments on commit d2873e3

Please sign in to comment.