Skip to content

Commit

Permalink
Add exclusions to LdtkSettings and filters to level spawner.
Browse files Browse the repository at this point in the history
Fixes Trouv#272
  • Loading branch information
focustense committed Dec 3, 2023
1 parent 2d43efa commit aef8c41
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,16 @@ pub fn spawn_level(
}
}

for layer_instance in layer_instances.iter().rev() {
for layer_instance in layer_instances
.iter()
.filter(|layer| {
!ldtk_settings
.exclusions
.layer_identifiers
.contains(&layer.identifier)
})
.rev()
{
let layer_offset = Vec2::new(
layer_instance.px_total_offset_x as f32,
-layer_instance.px_total_offset_y as f32,
Expand Down Expand Up @@ -550,10 +559,10 @@ pub fn spawn_level(
.filter(|(_, v)| **v != 0)
{
let grid_coords = int_grid_index_to_grid_coords(
i,
layer_instance.c_wid as u32,
layer_instance.c_hei as u32,
).expect("int_grid_csv indices should be within the bounds of 0..(layer_width * layer_height)");
i,
layer_instance.c_wid as u32,
layer_instance.c_hei as u32,
).expect("int_grid_csv indices should be within the bounds of 0..(layer_width * layer_height)");

if let Some(tile_entity) = storage.get(&grid_coords.into()) {
let mut entity_commands = commands.entity(tile_entity);
Expand Down
13 changes: 12 additions & 1 deletion src/resources/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Resources and events used by the plugin.
use bevy::prelude::*;

#[allow(unused_imports)]
use crate::assets::LdtkProject;
#[allow(unused_imports)]
use crate::components::LdtkWorldBundle;

Expand Down Expand Up @@ -67,12 +69,21 @@ pub enum LevelBackground {
Nonexistent,
}

/// Specifies data that should be ignored completely when spawning levels. Excluded items will still
/// be present in the [`LdtkProject`] but will not cause any entities to be spawned in the world.
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct LdtkExclusions {
/// List of layer `Identifier` names (not UIDs) to skip spawning as tilemaps.
pub layer_identifiers: Vec<String>,
}

/// Settings resource for the plugin.
/// Check out the documentation for each field type to learn more.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Resource)]
#[derive(Clone, Eq, PartialEq, Debug, Default, Resource)]
pub struct LdtkSettings {
pub level_spawn_behavior: LevelSpawnBehavior,
pub set_clear_color: SetClearColor,
pub int_grid_rendering: IntGridRendering,
pub level_background: LevelBackground,
pub exclusions: LdtkExclusions,
}

0 comments on commit aef8c41

Please sign in to comment.