From b4f29e8ef1354afe8d08af62c39363395f2e27d8 Mon Sep 17 00:00:00 2001 From: Scott McBee Date: Wed, 20 Sep 2023 12:37:16 -0400 Subject: [PATCH] count loaded external levels --- src/assets/ldtk_project.rs | 9 ++++ src/systems.rs | 104 ++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 52 deletions(-) diff --git a/src/assets/ldtk_project.rs b/src/assets/ldtk_project.rs index 4aa8bbab..2c60592e 100644 --- a/src/assets/ldtk_project.rs +++ b/src/assets/ldtk_project.rs @@ -29,6 +29,7 @@ pub struct LdtkProject { level_map: HashMap>, /// Image used for rendering int grid colors. int_grid_image_handle: Option>, + external_level_count: usize, } impl RawLevelAccessor for LdtkProject { @@ -52,6 +53,10 @@ impl LdtkProject { .find(|(i, l)| level_selection.is_match(i, l)) .map(|(_, l)| l) } + + pub fn get_level_count(&self) -> usize{ + self.external_level_count + } } #[derive(Default)] @@ -66,6 +71,7 @@ impl AssetLoader for LdtkProjectLoader { Box::pin(async move { let data: LdtkJson = serde_json::from_slice(bytes)?; + let mut external_level_count = 0; let mut external_level_paths = Vec::new(); let mut level_map = HashMap::new(); let mut background_images = Vec::new(); @@ -76,6 +82,7 @@ impl AssetLoader for LdtkProjectLoader { ldtk_path_to_asset_path(load_context.path(), external_rel_path); external_level_paths.push(asset_path.clone()); + external_level_count += 1; level_map.insert(level.iid.clone(), load_context.get_handle(asset_path)); } } @@ -123,6 +130,7 @@ impl AssetLoader for LdtkProjectLoader { tileset_map, level_map, int_grid_image_handle, + external_level_count, }; load_context.set_default_asset( LoadedAsset::new(ldtk_asset) @@ -170,6 +178,7 @@ mod tests { tileset_map: HashMap::default(), level_map: HashMap::default(), int_grid_image_handle: None, + external_level_count: 0, }; assert_eq!(project.root_levels(), data.root_levels()); diff --git a/src/systems.rs b/src/systems.rs index 1944b258..091132f2 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -230,16 +230,13 @@ pub fn process_ldtk_levels( ldtk_entity_map: NonSend, ldtk_int_cell_map: NonSend, ldtk_query: Query<&Handle>, - level_query: Query< - ( - Entity, - &Handle, - &Parent, - Option<&Respawn>, - Option<&Children>, - ), - Or<(Added>, With)>, - >, + level_query: Query<( + Entity, + &Handle, + &Parent, + Option<&Respawn>, + Option<&Children>, + )>, worldly_query: Query<&Worldly>, mut level_events: EventWriter, ldtk_settings: Res, @@ -259,49 +256,52 @@ pub fn process_ldtk_levels( if !already_processed { if let Ok(ldtk_handle) = ldtk_query.get(parent.get()) { if let Some(ldtk_asset) = ldtk_assets.get(ldtk_handle) { - // Commence the spawning - let tileset_definition_map: HashMap = ldtk_asset - .data() - .defs - .tilesets - .iter() - .map(|t| (t.uid, t)) - .collect(); - - let entity_definition_map = - create_entity_definition_map(&ldtk_asset.data().defs.entities); - - let layer_definition_map = - create_layer_definition_map(&ldtk_asset.data().defs.layers); - - let int_grid_image_handle = &ldtk_asset.int_grid_image_handle(); - - let worldly_set = worldly_query.iter().cloned().collect(); - - if let Some(level) = level_assets.get(level_handle) { - spawn_level( - level, - &mut commands, - &asset_server, - &images, - &mut texture_atlases, - &ldtk_entity_map, - &ldtk_int_cell_map, - &entity_definition_map, - &layer_definition_map, - ldtk_asset.tileset_map(), - &tileset_definition_map, - int_grid_image_handle, - worldly_set, - ldtk_entity, - &ldtk_settings, - ); - level_events - .send(LevelEvent::Spawned(LevelIid::new(level.data().iid.clone()))); - } + let fully_loaded = level_assets.ids().count() >= ldtk_asset.get_level_count(); + if fully_loaded { + // Commence the spawning + let tileset_definition_map: HashMap = ldtk_asset + .data() + .defs + .tilesets + .iter() + .map(|t| (t.uid, t)) + .collect(); + + let entity_definition_map = + create_entity_definition_map(&ldtk_asset.data().defs.entities); + + let layer_definition_map = + create_layer_definition_map(&ldtk_asset.data().defs.layers); + + let int_grid_image_handle = &ldtk_asset.int_grid_image_handle(); + + let worldly_set = worldly_query.iter().cloned().collect(); + + if let Some(level) = level_assets.get(level_handle) { + spawn_level( + level, + &mut commands, + &asset_server, + &images, + &mut texture_atlases, + &ldtk_entity_map, + &ldtk_int_cell_map, + &entity_definition_map, + &layer_definition_map, + ldtk_asset.tileset_map(), + &tileset_definition_map, + int_grid_image_handle, + worldly_set, + ldtk_entity, + &ldtk_settings, + ); + level_events + .send(LevelEvent::Spawned(LevelIid::new(level.data().iid.clone()))); + } - if respawn.is_some() { - commands.entity(ldtk_entity).remove::(); + if respawn.is_some() { + commands.entity(ldtk_entity).remove::(); + } } } }