From 82a7ccbcab8c9b1c81fba64d63eaf42ac91aad4c Mon Sep 17 00:00:00 2001 From: Trevor Lovell Date: Wed, 4 Oct 2023 20:49:34 -0600 Subject: [PATCH 1/8] feat: add LevelLocale trait for making LdtkJsonWithMetadata parameters slightly more readable and private and enumerated --- src/assets/level_locale.rs | 29 +++++++++++++++++++++++++++++ src/assets/mod.rs | 14 +++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/assets/level_locale.rs diff --git a/src/assets/level_locale.rs b/src/assets/level_locale.rs new file mode 100644 index 00000000..2b3bb96d --- /dev/null +++ b/src/assets/level_locale.rs @@ -0,0 +1,29 @@ +use bevy::reflect::Reflect; + +#[cfg(feature = "internal_levels")] +use crate::assets::LevelMetadata; + +#[cfg(feature = "external_levels")] +use crate::assets::ExternalLevelMetadata; + +pub trait LevelLocale { + type Metadata; +} + +#[cfg(feature = "internal_levels")] +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Reflect)] +pub struct InternalLevels; + +#[cfg(feature = "internal_levels")] +impl LevelLocale for InternalLevels { + type Metadata = LevelMetadata; +} + +#[cfg(feature = "external_levels")] +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Reflect)] +pub struct ExternalLevels; + +#[cfg(feature = "external_levels")] +impl LevelLocale for ExternalLevels { + type Metadata = ExternalLevelMetadata; +} diff --git a/src/assets/mod.rs b/src/assets/mod.rs index a416be3c..7b7dfa5d 100644 --- a/src/assets/mod.rs +++ b/src/assets/mod.rs @@ -7,7 +7,19 @@ mod ldtk_asset_plugin; pub use ldtk_asset_plugin::LdtkAssetPlugin; mod level_metadata; -pub use level_metadata::{ExternalLevelMetadata, LevelMetadata}; + +pub use level_metadata::LevelMetadata; + +#[cfg(feature = "external_levels")] +pub use level_metadata::ExternalLevelMetadata; + +mod level_locale; + +#[cfg(feature = "internal_levels")] +pub use level_locale::InternalLevels; + +#[cfg(feature = "external_levels")] +pub use level_locale::ExternalLevels; mod level_metadata_accessor; pub use level_metadata_accessor::LevelMetadataAccessor; From 4803525ffcd3a119a5c5ca8abc234da226cf1cae Mon Sep 17 00:00:00 2001 From: Trevor Lovell Date: Wed, 4 Oct 2023 21:12:57 -0600 Subject: [PATCH 2/8] docs: add basic docs for LevelLocale module --- src/assets/level_locale.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/assets/level_locale.rs b/src/assets/level_locale.rs index 2b3bb96d..9b7ae5bd 100644 --- a/src/assets/level_locale.rs +++ b/src/assets/level_locale.rs @@ -6,15 +6,30 @@ use crate::assets::LevelMetadata; #[cfg(feature = "external_levels")] use crate::assets::ExternalLevelMetadata; +/// Trait for marker types describing the location of levels. +/// +/// Used as a trait bound to parameterize [`LdtkJsonWithMetadata`]. +/// Also provides an associated type defining the level metadata type for the locale. +/// +/// Only implemented by [`InternalLevels`] and [`ExternalLevels`]. +/// +/// [`LdtkJsonWithMetadata`]: crate::assets::LdtkJsonWithMetadata pub trait LevelLocale { + /// Level metadata type used for this locale. type Metadata; } #[cfg(feature = "internal_levels")] +/// Marker type for indicating an internal-levels LDtk project. +/// +/// Used to parameterize [`LdtkJsonWithMetadata`]. #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Reflect)] pub struct InternalLevels; #[cfg(feature = "internal_levels")] +/// Marker type for indicating an external-levels LDtk projects. +/// +/// Used to parameterize [`LdtkJsonWithMetadata`]. impl LevelLocale for InternalLevels { type Metadata = LevelMetadata; } From 49687962aa4b6c024e2cf32e73bea49b80a97648 Mon Sep 17 00:00:00 2001 From: Trevor Lovell Date: Mon, 9 Oct 2023 18:04:28 -0600 Subject: [PATCH 3/8] docs: fix level-locale docs --- src/assets/level_locale.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/assets/level_locale.rs b/src/assets/level_locale.rs index 9b7ae5bd..a5416341 100644 --- a/src/assets/level_locale.rs +++ b/src/assets/level_locale.rs @@ -23,18 +23,22 @@ pub trait LevelLocale { /// Marker type for indicating an internal-levels LDtk project. /// /// Used to parameterize [`LdtkJsonWithMetadata`]. +/// +/// [`LdtkJsonWithMetadata`]: crate::assets::LdtkJsonWithMetadata #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Reflect)] pub struct InternalLevels; #[cfg(feature = "internal_levels")] -/// Marker type for indicating an external-levels LDtk projects. -/// -/// Used to parameterize [`LdtkJsonWithMetadata`]. impl LevelLocale for InternalLevels { type Metadata = LevelMetadata; } #[cfg(feature = "external_levels")] +/// Marker type for indicating an external-levels LDtk projects. +/// +/// Used to parameterize [`LdtkJsonWithMetadata`]. +/// +/// [`LdtkJsonWithMetadata`]: crate::assets::LdtkJsonWithMetadata #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Reflect)] pub struct ExternalLevels; From d7a195032969199bd4f706eb5dad0010bc7bfb2f Mon Sep 17 00:00:00 2001 From: Trevor Lovell Date: Fri, 13 Oct 2023 23:42:13 -0600 Subject: [PATCH 4/8] feat: add default internal_levels feature and non-default external_levels feature --- Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3fd02e84..253befdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,10 +33,12 @@ rand = "0.8" bevy-inspector-egui = "0.19.0" [features] -default = ["derive", "render"] +default = ["derive", "render", "internal_levels"] derive = ["bevy_ecs_ldtk_macros"] atlas = ["bevy_ecs_tilemap/atlas"] render = ["bevy_ecs_tilemap/render"] +internal_levels = [] +external_levels = [] [[example]] name = "platformer" From 8ef07216418353eb6462a3342c264c38ddc4cf17 Mon Sep 17 00:00:00 2001 From: Trevor Lovell Date: Fri, 13 Oct 2023 23:42:30 -0600 Subject: [PATCH 5/8] feat: implement Reflect for LevelIndices --- src/assets/level_indices.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/assets/level_indices.rs b/src/assets/level_indices.rs index 08240977..af894c8b 100644 --- a/src/assets/level_indices.rs +++ b/src/assets/level_indices.rs @@ -1,3 +1,5 @@ +use bevy::reflect::Reflect; + /// 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. @@ -6,7 +8,7 @@ /// /// [`LdtkProject`]: crate::assets::LdtkProject /// [`LdtkJson`]: crate::ldtk::LdtkJson -#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Reflect)] pub struct LevelIndices { /// The index of the world the level belongs to, if the project is multi-world. pub world: Option, From f262af3843b998a566b5d1bba37c95acff3725cb Mon Sep 17 00:00:00 2001 From: Trevor Lovell Date: Fri, 13 Oct 2023 23:43:21 -0600 Subject: [PATCH 6/8] feat: put level metadata types behind features and implement reflect for them --- src/assets/level_metadata.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/assets/level_metadata.rs b/src/assets/level_metadata.rs index c7cf9345..ae454e82 100644 --- a/src/assets/level_metadata.rs +++ b/src/assets/level_metadata.rs @@ -1,17 +1,14 @@ use crate::assets::LevelIndices; -use bevy::{ - prelude::*, - reflect::{TypePath, TypeUuid}, -}; +use bevy::{prelude::*, reflect::Reflect}; use derive_getters::Getters; +#[cfg(feature = "external_levels")] use crate::assets::LdtkLevel; /// Metadata produced for every level during [`LdtkProject`] loading. /// /// [`LdtkProject`]: crate::assets::LdtkProject -#[derive(Clone, Debug, Default, Eq, PartialEq, TypeUuid, TypePath, Getters)] -#[uuid = "bba47e30-5036-4994-acde-d62a440b16b8"] +#[derive(Clone, Debug, Default, Eq, PartialEq, Getters, Reflect)] pub struct LevelMetadata { /// Image handle for the background image of this level, if it has one. bg_image: Option>, @@ -26,11 +23,11 @@ impl LevelMetadata { } } +#[cfg(feature = "external_levels")] /// Metadata produced for every level during [`LdtkProject`] loading for external-levels projects. /// /// [`LdtkProject`]: crate::assets::LdtkProject -#[derive(Clone, Debug, Default, Eq, PartialEq, TypeUuid, TypePath, Getters)] -#[uuid = "d3190ad4-6fa4-4f47-b15b-87f92f191738"] +#[derive(Clone, Debug, Default, Eq, PartialEq, Getters, Reflect)] pub struct ExternalLevelMetadata { /// Common metadata for this level. metadata: LevelMetadata, @@ -38,6 +35,7 @@ pub struct ExternalLevelMetadata { external_handle: Handle, } +#[cfg(feature = "external_levels")] impl ExternalLevelMetadata { /// Construct a new [`ExternalLevelMetadata`]. pub fn new(metadata: LevelMetadata, external_handle: Handle) -> Self { @@ -73,6 +71,7 @@ mod tests { assert_eq!(*level_metadata.indices(), LevelIndices::in_world(2, 3)); } + #[cfg(feature = "external_levels")] #[test] fn external_level_metadata_construction() { let level_metadata = LevelMetadata::new(None, LevelIndices::in_root(1)); From e87b7962fe03a9e9d3fc83287c761166e27b7c2f Mon Sep 17 00:00:00 2001 From: Trevor Lovell Date: Fri, 13 Oct 2023 23:46:05 -0600 Subject: [PATCH 7/8] ci: don't check no-default-features, enable at least internal_levels or external_levels --- .github/workflows/lint.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 8cc9abda..df9d37ab 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -31,8 +31,10 @@ jobs: uses: dtolnay/rust-toolchain@stable - name: Install Dependencies run: sudo apt-get update; sudo apt-get install pkg-config libx11-dev libasound2-dev libudev-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev - - name: Run cargo check (no features, exclude examples) - run: cargo check --no-default-features + - name: Run cargo check (minimal features, internal levels, exclude examples) + run: cargo check --no-default-features --features internal_levels + - name: Run cargo check (minimal features, external levels, exclude examples) + run: cargo check --no-default-features --features external_levels - name: Run cargo check (default features) run: cargo check --all-targets - name: Run cargo check (all features) From 6aced9c32fa6810dfb7576ee34329d906677d57f Mon Sep 17 00:00:00 2001 From: Trevor Lovell Date: Wed, 27 Sep 2023 22:14:09 -0600 Subject: [PATCH 8/8] docs: add explanation of new cargo features --- src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b6cd5d51..7296d801 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,13 +99,18 @@ //! ### Feature flags //! //! This crate uses the following set of [feature flags]: +//! - `internal_levels`: Enable support for projects that store levels internally. +//! I.e., projects that store level data within the main project file. +//! - `external_levels`: Enable support for projects that store levels externally. +//! I.e., projects that store data for each level in files separate from the main project file. //! - `derive`: Enables the derive macros for [LdtkEntity] and [LdtkIntCell]. //! - `render`: Enables rendering via [bevy_ecs_tilemap]'s `render` feature. Disable it if you want //! to run in headless mode. //! - `atlas`: Enables the `atlas` feature of [bevy_ecs_tilemap]. This is required for WASM support //! and also for tile spacing to work on Tile and AutoTile layers. //! -//! The `derive` and `render` features are enabled by default. +//! The `derive`, `render`, and `internal_levels` features are enabled by default. +//! Furthermore, one or both of `internal_levels` and `external_levels` must be enabled. //! //! [App]: bevy::prelude::App //! [Commands]: bevy::prelude::Commands