Skip to content

Commit

Permalink
feat: add LevelIid component and spawn it on every level (#215)
Browse files Browse the repository at this point in the history
Works towards #205.

`Handle<LdtkLevel>` is no longer going to be the main query-able
component on level entities, because "level assets" won't exist in the
internal level case. So instead, `LevelIid` will be the main component
on level entities. There will be a convenient API for retrieving
raw/loaded level data using this `LevelIid` as well. The only part of
this plan that this PR implements is adding the `LevelIid` component and
spawning it on every level.

This is based on the existing `EntityIid` component, but it simplifies
things a little bit. In particular, anything that cloned implicitly
before is gone, and the iid isn't stored as a `Cow` since it won't be
mutable anyway. I'm not against bringing the convenient copying APIs
back if users want them, just thought that that would be a good place to
trim it up a little bit. `EntityIid` might be updated to match in a
future PR.
  • Loading branch information
Trouv authored Aug 10, 2023
1 parent 3d40c15 commit ad83455
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
66 changes: 66 additions & 0 deletions src/components/level_iid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::fmt::Display;

use bevy::prelude::*;

/// `Component` that stores a level's instance identifier.
#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Component, Reflect)]
#[reflect(Component, Default, Debug)]
pub struct LevelIid(String);

impl LevelIid {
/// Creates a new [`LevelIid`] from any string-like type.
pub fn new(iid: impl Into<String>) -> Self {
let iid = iid.into();
LevelIid(iid)
}

/// Immutable access to the IID as a `&str`.
pub fn as_str(&self) -> &str {
&self.0
}
}

impl From<String> for LevelIid {
fn from(value: String) -> Self {
LevelIid::new(value)
}
}

impl From<LevelIid> for String {
fn from(value: LevelIid) -> String {
value.0
}
}

impl AsRef<str> for LevelIid {
fn as_ref(&self) -> &str {
&self.0
}
}

impl Display for LevelIid {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn string_converts_to_and_from_level_iid() {
let original_string = "level-iid".to_string();
let level_iid = LevelIid::new(original_string.clone());

assert_eq!(level_iid, LevelIid(original_string.clone()));
assert_eq!(level_iid.as_str(), original_string.as_str());
assert_eq!(LevelIid::from(original_string.clone()), level_iid);
assert_eq!(String::from(level_iid.clone()), original_string);
assert_eq!(level_iid.as_ref(), original_string.as_str());
assert_eq!(
format!("display: {level_iid}"),
format!("display: {original_string}")
);
}
}
3 changes: 3 additions & 0 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
mod entity_iid;
pub use entity_iid::EntityIid;

mod level_iid;
pub use level_iid::LevelIid;

pub use crate::ldtk::EntityInstance;
use crate::ldtk::{LayerInstance, Type};
use bevy::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub mod prelude {
assets::{LdtkLevel, LdtkProject},
components::{
EntityIid, EntityInstance, GridCoords, IntGridCell, LayerMetadata, LdtkWorldBundle,
LevelSet, Respawn, TileEnumTags, TileMetadata, Worldly,
LevelIid, LevelSet, Respawn, TileEnumTags, TileMetadata, Worldly,
},
ldtk::{self, ldtk_fields::LdtkFields, FieldValue, LayerInstance, TilesetDefinition},
plugin::{LdtkPlugin, ProcessLdtkApi},
Expand Down
1 change: 1 addition & 0 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl Plugin for LdtkPlugin {
PostUpdate,
systems::detect_level_spawned_events.pipe(systems::fire_level_transformed_events),
)
.register_type::<components::LevelIid>()
.register_type::<components::EntityIid>()
.register_type::<components::GridCoords>()
.register_type::<components::TileMetadata>()
Expand Down
1 change: 1 addition & 0 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ fn pre_spawn_level(

child_builder
.spawn_empty()
.insert(LevelIid::new(level_iid))
.insert(level_handle.clone())
.insert(SpatialBundle {
transform: Transform::from_translation(translation),
Expand Down

0 comments on commit ad83455

Please sign in to comment.