Skip to content
Snippets Groups Projects
Verified Commit ac83ea28 authored by Louis's avatar Louis :fire:
Browse files

Update to Bevy 0.12; Include ldtk 1.4.0

parent df3dfe92
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
...@@ -28,7 +28,7 @@ autotile = ["micro_autotile"] ...@@ -28,7 +28,7 @@ autotile = ["micro_autotile"]
no_panic = [] no_panic = []
[dependencies] [dependencies]
bevy = { version = "0.11", default-features = false, features = ["bevy_render", "bevy_sprite", "bevy_asset"] } bevy = { version = "0.12", default-features = false, features = ["bevy_render", "bevy_sprite", "bevy_asset"] }
anyhow = "1.0" anyhow = "1.0"
thiserror = "1.0" thiserror = "1.0"
......
...@@ -17,7 +17,7 @@ select the schema version you need: ...@@ -17,7 +17,7 @@ select the schema version you need:
```toml ```toml
[dependencies] [dependencies]
micro_ldtk = { version = "0.7.0", default-features = false, features = ["ldtk_1_3_0", "autotile"] } micro_ldtk = { version = "0.8.0", default-features = false, features = ["ldtk_1_3_0", "autotile"] }
``` ```
### Features ### Features
......
...@@ -13,9 +13,10 @@ pub fn handle_ldtk_project_events( ...@@ -13,9 +13,10 @@ pub fn handle_ldtk_project_events(
mut tilset_index: ResMut<TilesetIndex>, mut tilset_index: ResMut<TilesetIndex>,
mut update_events: EventWriter<LevelDataUpdated>, mut update_events: EventWriter<LevelDataUpdated>,
) { ) {
for event in events.iter() { for event in events.read() {
match event { match event {
AssetEvent::Created { handle } | AssetEvent::Modified { handle } => { AssetEvent::Added { id } | AssetEvent::Modified { id } => {
let handle = Handle::Weak(*id);
if let Some(project) = assets.get(handle) { if let Some(project) = assets.get(handle) {
for level in project.get_all_levels() { for level in project.get_all_levels() {
if level.external_rel_path.is_none() { if level.external_rel_path.is_none() {
...@@ -50,7 +51,8 @@ pub fn handle_ldtk_level_events( ...@@ -50,7 +51,8 @@ pub fn handle_ldtk_level_events(
) { ) {
for event in events.iter() { for event in events.iter() {
match event { match event {
AssetEvent::Created { handle } | AssetEvent::Modified { handle } => { AssetEvent::Added { id } | AssetEvent::Modified { id } => {
let handle = Handle::Weak(*id);
if let Some(level) = assets.get(handle) { if let Some(level) = assets.get(handle) {
level_index.insert(level.identifier.clone(), LdtkLevel::from(level.clone())); level_index.insert(level.identifier.clone(), LdtkLevel::from(level.clone()));
update_events.send(LevelDataUpdated(level.identifier.clone())); update_events.send(LevelDataUpdated(level.identifier.clone()));
......
This diff is collapsed.
...@@ -14,10 +14,18 @@ mod data_1_2_4; ...@@ -14,10 +14,18 @@ mod data_1_2_4;
mod data_1_2_5; mod data_1_2_5;
#[cfg(feature = "ldtk_1_3_0")] #[cfg(feature = "ldtk_1_3_0")]
mod data_1_3_0; mod data_1_3_0;
#[cfg(feature = "ldtk_1_4_0")]
use bevy::asset::{AssetLoader, AssetPath, BoxedFuture, LoadContext, LoadedAsset}; mod data_1_4_0;
use bevy::asset::io::Reader;
use bevy::asset::{
AssetLoader, AssetPath, AsyncReadExt, BoxedFuture, LoadContext, LoadedAsset, UntypedAssetId,
VisitAssetDependencies,
};
use bevy::prelude::Asset;
use bevy::reflect::{TypePath, TypeUuid, Uuid}; use bevy::reflect::{TypePath, TypeUuid, Uuid};
use crate::{ldtk, LdtkLevel};
#[cfg(feature = "ldtk_1_0_0")] #[cfg(feature = "ldtk_1_0_0")]
pub use data_1_0_0::*; pub use data_1_0_0::*;
#[cfg(any(feature = "ldtk_1_1_1", feature = "ldtk_1_1_0"))] #[cfg(any(feature = "ldtk_1_1_1", feature = "ldtk_1_1_0"))]
...@@ -34,6 +42,8 @@ pub use data_1_2_4::*; ...@@ -34,6 +42,8 @@ pub use data_1_2_4::*;
pub use data_1_2_5::*; pub use data_1_2_5::*;
#[cfg(feature = "ldtk_1_3_0")] #[cfg(feature = "ldtk_1_3_0")]
pub use data_1_3_0::*; pub use data_1_3_0::*;
#[cfg(feature = "ldtk_1_4_0")]
pub use data_1_4_0::*;
use serde::Deserialize; use serde::Deserialize;
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
...@@ -91,6 +101,12 @@ impl TypePath for Project { ...@@ -91,6 +101,12 @@ impl TypePath for Project {
} }
} }
impl VisitAssetDependencies for Project {
fn visit_dependencies(&self, _visit: &mut impl FnMut(UntypedAssetId)) {}
}
impl Asset for Project {}
impl TypeUuid for Level { impl TypeUuid for Level {
const TYPE_UUID: Uuid = Uuid::from_u128(18486863672600588966868281477384349187); const TYPE_UUID: Uuid = Uuid::from_u128(18486863672600588966868281477384349187);
} }
...@@ -105,6 +121,12 @@ impl TypePath for Level { ...@@ -105,6 +121,12 @@ impl TypePath for Level {
} }
} }
impl VisitAssetDependencies for Level {
fn visit_dependencies(&self, _visit: &mut impl FnMut(UntypedAssetId)) {}
}
impl Asset for Level {}
impl Project { impl Project {
pub fn get_all_levels(&self) -> Vec<&Level> { pub fn get_all_levels(&self) -> Vec<&Level> {
if !self.worlds.is_empty() { if !self.worlds.is_empty() {
...@@ -134,7 +156,7 @@ impl Project { ...@@ -134,7 +156,7 @@ impl Project {
vec![] vec![]
} }
#[cfg(any(feature = "ldtk_1_3_0",))] #[cfg(any(feature = "ldtk_1_3_0", feature = "ldtk_1_4_0"))]
pub fn get_world_levels(&self, identifier: impl ToString) -> Vec<&Level> { pub fn get_world_levels(&self, identifier: impl ToString) -> Vec<&Level> {
let id = identifier.to_string(); let id = identifier.to_string();
self.worlds self.worlds
...@@ -145,58 +167,56 @@ impl Project { ...@@ -145,58 +167,56 @@ impl Project {
} }
} }
#[derive(Debug, thiserror::Error)]
pub enum LdtkLoadError {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Serde(#[from] serde_json::Error),
#[error(transparent)]
Ldtk(#[from] ldtk::ParseError),
}
pub type LdtkProject = Project; pub type LdtkProject = Project;
#[derive(Default)] #[derive(Default)]
pub struct LdtkLoader; pub struct LdtkLoader;
impl AssetLoader for LdtkLoader { impl AssetLoader for LdtkLoader {
type Asset = Project;
type Settings = ();
type Error = LdtkLoadError;
fn load<'a>( fn load<'a>(
&'a self, &'a self,
bytes: &'a [u8], reader: &'a mut Reader,
_settings: &'a Self::Settings,
load_context: &'a mut LoadContext, load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, anyhow::Result<(), anyhow::Error>> { ) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move { Box::pin(async move {
log::debug!( let mut bytes = Vec::new();
"Loading ldtk project file {}", reader.read_to_end(&mut bytes).await?;
load_context.path().display() let project = Project::from_bytes(bytes.as_slice())?;
);
let mut levels = project.levels.iter().flat_map(|level| {
let project = Project::from_bytes(bytes)?; log::debug!(
"Checking if level is external: {} [{}]",
level.identifier,
level.external_rel_path.is_some()
);
level
.external_rel_path
.as_ref()
.map(|path| (level.identifier.clone(), path))
});
for (id, path) in levels {
load_context.labeled_asset_scope(id, |lc| {
lc.load::<Level>(path);
});
}
let sub_levels = project Ok(project)
.levels
.iter()
.flat_map(|level| {
log::debug!(
"Checking if level is external: {} [{}]",
level.identifier,
level.external_rel_path.is_some()
);
level
.external_rel_path
.as_ref()
.map(|rel_path| (level.identifier.clone(), rel_path.clone()))
})
.collect::<Vec<(String, String)>>();
let asset = LoadedAsset::new(project).with_dependencies(
sub_levels
.into_iter()
.flat_map(|(id, path)| {
log::debug!(
"Checking for file {}",
load_context.path().parent()?.join(&path).display()
);
let path = load_context.path().parent()?.join(path);
Some(AssetPath::new(path, Some(id)))
})
.collect(),
);
load_context.set_default_asset(asset);
Ok(())
}) })
} }
...@@ -204,21 +224,27 @@ impl AssetLoader for LdtkLoader { ...@@ -204,21 +224,27 @@ impl AssetLoader for LdtkLoader {
&["ldtk"] &["ldtk"]
} }
} }
#[derive(Default)] #[derive(Default)]
pub struct LdtkLevelLoader; pub struct LdtkLevelLoader;
impl AssetLoader for LdtkLevelLoader { impl AssetLoader for LdtkLevelLoader {
type Asset = Level;
type Settings = ();
type Error = LdtkLoadError;
fn load<'a>( fn load<'a>(
&'a self, &'a self,
bytes: &'a [u8], reader: &'a mut Reader,
load_context: &'a mut LoadContext, _settings: &'a Self::Settings,
) -> BoxedFuture<'a, anyhow::Result<(), anyhow::Error>> { _load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move { Box::pin(async move {
log::debug!("Loading ldtkl level file {}", load_context.path().display()); let mut bytes = Vec::new();
load_context.set_default_asset(LoadedAsset::new(Level::from_bytes(bytes)?)); reader.read_to_end(&mut bytes).await?;
Ok(()) let level = Level::from_bytes(bytes.as_slice())?;
Ok(level)
}) })
} }
fn extensions(&self) -> &[&str] { fn extensions(&self) -> &[&str] {
&["ldtkl"] &["ldtkl"]
} }
......
...@@ -74,10 +74,10 @@ mod __plugin { ...@@ -74,10 +74,10 @@ mod __plugin {
))] ))]
{ {
app.add_event::<super::system::LevelDataUpdated>() app.add_event::<super::system::LevelDataUpdated>()
.add_asset::<super::ldtk::Project>() .init_asset::<super::ldtk::Project>()
.add_asset::<super::ldtk::Level>() .init_asset::<super::ldtk::Level>()
.add_asset_loader(super::ldtk::LdtkLoader) .init_asset_loader::<super::ldtk::LdtkLoader>()
.add_asset_loader(super::ldtk::LdtkLevelLoader) .init_asset_loader::<super::ldtk::LdtkLevelLoader>()
.init_resource::<super::assets::TilesetIndex>() .init_resource::<super::assets::TilesetIndex>()
.init_resource::<super::assets::LevelIndex>() .init_resource::<super::assets::LevelIndex>()
.add_systems(Update, super::assets::handle_ldtk_project_events) .add_systems(Update, super::assets::handle_ldtk_project_events)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment