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

feat!: use new LevelIid type in LevelEvent, LevelSet, and LevelSelection, plus other improvements #219

Merged
merged 23 commits into from
Aug 15, 2023
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b3e231d
refactor: make resources a directory module for now
Trouv Aug 12, 2023
1ef7523
feat: move LevelSelection to its own file
Trouv Aug 12, 2023
de66555
feat: add LevelIid::get for getting the level Iid as a String
Trouv Aug 12, 2023
c964406
feat: use LevelIid as inner type for LevelSelection::Iid, and provide…
Trouv Aug 12, 2023
5fc6b5d
refactor: move LevelSet to its own module
Trouv Aug 12, 2023
ab5cd4b
feat: use LevelIid in LevelSet
Trouv Aug 13, 2023
0487e7a
feat: implement iterator conversion for LevelSet
Trouv Aug 13, 2023
5d0a77c
feat: improve memory management of pre-spawning levels
Trouv Aug 13, 2023
eb1e5e4
feat: do a little more borrowing in apply_level_set
Trouv Aug 13, 2023
c3e7d19
refactor: move LevelEvent to its own module
Trouv Aug 13, 2023
422af79
feat: update LevelEvent to use LevelIids internally
Trouv Aug 13, 2023
138be29
docs(example): update level set example to use LevelIids
Trouv Aug 13, 2023
5775e48
test: add one more test case to string_converts_to_and_from_level_iid
Trouv Aug 13, 2023
f870ab2
docs: fix doc links for LevelSet
Trouv Aug 13, 2023
d6b258c
feat: change from_iid to from_iids which accepts a generic iterator
Trouv Aug 13, 2023
cd010b5
feat: update level_set example to use LevelSet::from_iids
Trouv Aug 13, 2023
ebd4e6c
test: add another trivial test for LevelSet
Trouv Aug 13, 2023
07aebe0
docs: properly link to Component in LevelIid docs
Trouv Aug 13, 2023
660cccb
docs: update imports in components module to adjust for moved/unused …
Trouv Aug 13, 2023
db7696b
docs: update LevelSelection docs with proper links and a doctest
Trouv Aug 13, 2023
e8b55fb
docs: fix typo in LevelSelection::is_match docs
Trouv Aug 13, 2023
8e7d2e0
docs: no longer use unused unused import in resources module
Trouv Aug 13, 2023
3d6d6a3
docs: fix code links in LevelEvent docs
Trouv Aug 13, 2023
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
Prev Previous commit
Next Next commit
feat: improve memory management of pre-spawning levels
  • Loading branch information
Trouv committed Aug 13, 2023
commit 5d0a77c8966588958413991b08e8ab17a337f9b6
88 changes: 47 additions & 41 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,16 @@ pub fn apply_level_set(
previous_level_maps.keys().cloned().cloned().collect();

// Spawn levels that should be spawned but aren't
let iids_to_spawn = level_set.iids.difference(&previous_iids);
if iids_to_spawn.clone().count() > 0 {
commands.entity(world_entity).with_children(|c| {
for iid in iids_to_spawn.clone() {
level_events.send(LevelEvent::SpawnTriggered(iid.get().clone()));
pre_spawn_level(c, ldtk_asset, iid.as_str(), &ldtk_settings);
}
});
}
let spawned_levels = level_set
.iids
.difference(&previous_iids)
.filter_map(|iid| {
level_events.send(LevelEvent::SpawnTriggered(iid.get().clone()));
pre_spawn_level(&mut commands, ldtk_asset, iid.clone(), &ldtk_settings)
})
.collect::<Vec<_>>();

commands.entity(world_entity).push_children(&spawned_levels);

// Despawn levels that shouldn't be spawned but are
for iid in previous_iids.difference(&level_set.iids) {
Expand All @@ -165,49 +166,54 @@ pub fn apply_level_set(
// portion of said respawn.
// In that case, the respawn component needs to be removed so that the cleanup system
// doesn't start the process over again.
if previous_iids.is_empty() && iids_to_spawn.count() > 0 && respawn.is_some() {
if previous_iids.is_empty() && !spawned_levels.is_empty() && respawn.is_some() {
commands.entity(world_entity).remove::<Respawn>();
}
}
}
}

fn pre_spawn_level(
child_builder: &mut ChildBuilder,
commands: &mut Commands,
ldtk_asset: &LdtkProject,
level_iid: &str,
level_iid: LevelIid,
ldtk_settings: &LdtkSettings,
) {
if let Some(level_handle) = ldtk_asset.level_map().get(level_iid) {
let mut translation = Vec3::ZERO;

if let LevelSpawnBehavior::UseWorldTranslation { .. } = ldtk_settings.level_spawn_behavior {
if let Some(level) = ldtk_asset.get_level(&LevelSelection::iid(level_iid)) {
let level_coords = ldtk_pixel_coords_to_translation(
IVec2::new(level.world_x, level.world_y + level.px_hei),
0,
);
translation.x = level_coords.x;
translation.y = level_coords.y;
) -> Option<Entity> {
ldtk_asset
.level_map()
.get(level_iid.get())
.map(|level_handle| {
let mut translation = Vec3::ZERO;

if let LevelSpawnBehavior::UseWorldTranslation { .. } =
ldtk_settings.level_spawn_behavior
{
if let Some(level) = ldtk_asset.get_level(&LevelSelection::iid(level_iid.get())) {
let level_coords = ldtk_pixel_coords_to_translation(
IVec2::new(level.world_x, level.world_y + level.px_hei),
0,
);
translation.x = level_coords.x;
translation.y = level_coords.y;
}
}
}

child_builder
.spawn_empty()
.insert(LevelIid::new(level_iid))
.insert(level_handle.clone())
.insert(SpatialBundle {
transform: Transform::from_translation(translation),
..default()
})
.insert(Name::new(
ldtk_asset
.get_level(&LevelSelection::iid(level_iid))
.unwrap()
.identifier
.to_owned(),
));
}
commands
.spawn(level_iid.clone())
.insert(level_handle.clone())
.insert(SpatialBundle {
transform: Transform::from_translation(translation),
..default()
})
.insert(Name::new(
ldtk_asset
.get_level(&LevelSelection::Iid(level_iid))
.unwrap()
.identifier
.to_owned(),
))
.id()
})
}

/// Performs all the spawning of levels, layers, chunks, bundles, entities, tiles, etc. when an
Expand Down