-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add LevelIid component and spawn it on every level (#215)
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
Showing
5 changed files
with
72 additions
and
1 deletion.
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
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}") | ||
); | ||
} | ||
} |
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