-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add
LevelIndices
type defining a level's location in a proje…
…ct and use it in `LevelSelection::Indices` (#221) To provide constant lookup of raw/loaded levels in an asset, we need to use its world index/level index separately. Simply iterating through all the levels with the existing `LdtkProject::iter_levels` and getting the `.nth()` element isn't constant, even though it does chain together root levels and world levels. This PR provides a simple type `LevelIndices` that stores a world index and a level index, which can be used for this kind of lookup. Methods for performing this lookup and more have been added to `LdtkJson`/`LdtkProject`. It is also further integrated with the API by being used in a new `LevelSelection::Indices` variant, which replaces `LevelSelection::Index`. Getting a level by `LevelSelection` can now be made more performant if users are using the `Indices` variant, but this optimization will be implemented in a future PR. Some new convenience constructor methods have been added to `LevelSelection` to ease the migration. # Changes - add `LevelIndices` type - add `iter_levels_with_indices` methods to `LdtkProject` and `LdtkJson` that iterate through levels while enumerating them using the new type - add `get_level_at_indices` methods to `LdtkProject` and `LdtkJson` for constant lookup of levels with `LevelIndices` - add `LevelSelection::index` constructor method for easing migration to new variant for most users - add `LevelSelection::indices` constructor method for more ergonomically using the new variant in multi-world projects ## Breaking feat!: replace `LevelSelection::Index` with `LevelSelection::Indices` variant that uses `LevelIndices` internally BREAKING-CHANGE: Since it has been replaced by `LevelSelection::Indices`, any usage of `LevelSelection::Index` will break. Construction of the new variant can easily be migrated for non-multi-world projects with the `LevelSelection::index` constructor.
- Loading branch information
Showing
10 changed files
with
405 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/// Indices pointing to the location of a level in an [`LdtkProject`] or [`LdtkJson`]. | ||
/// | ||
/// This type supports multi-world projects by storing an optional `world` index. | ||
/// If this is present, the level index is used within that world. | ||
/// If not, the level index is used in the project root's level collection. | ||
/// | ||
/// [`LdtkProject`]: crate::assets::LdtkProject | ||
/// [`LdtkJson`]: crate::ldtk::LdtkJson | ||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] | ||
pub struct LevelIndices { | ||
/// The index of the world the level belongs to, if the project is multi-world. | ||
pub world: Option<usize>, | ||
/// The index of the level, either within a world or in the root of the project. | ||
pub level: usize, | ||
} | ||
|
||
impl LevelIndices { | ||
/// Construct a new [`LevelIndices`] pointing to a level in a world. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// use bevy_ecs_ldtk::prelude::*; | ||
/// | ||
/// let level_indices = LevelIndices::in_world(1, 2); | ||
/// | ||
/// assert_eq!(level_indices, LevelIndices { world: Some(1), level: 2 }); | ||
/// ``` | ||
pub fn in_world(world_index: usize, level_index: usize) -> LevelIndices { | ||
LevelIndices { | ||
world: Some(world_index), | ||
level: level_index, | ||
} | ||
} | ||
|
||
/// Construct a new [`LevelIndices`] pointing to a level in the project root. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// use bevy_ecs_ldtk::prelude::*; | ||
/// | ||
/// let level_indices = LevelIndices::in_root(3); | ||
/// | ||
/// assert_eq!(level_indices, LevelIndices { world: None, level: 3 }); | ||
/// ``` | ||
pub fn in_root(index: usize) -> LevelIndices { | ||
LevelIndices { | ||
world: None, | ||
level: index, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.