Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't spawn invisible tiles on sublayers other than the first #231

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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