diff --git a/CHANGELOG.md b/CHANGELOG.md index a02ca9c..054af15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ # Changelog +## v0.20.1 - 13.04.2024 - new map key type `AssetLabel` that creates an asset map using the label as the key - -## v0.20.1 - 08.04.2024 -- introduces the ability to load and build collections of dynamic assets (resolves [#186](https://github.com/NiklasEi/bevy_asset_loader/issues/186)) +- New Dynamic asset collection `StandardDynamicAssetArrayCollection` + - Define dynamic assets as arrays (resolves [#186](https://github.com/NiklasEi/bevy_asset_loader/issues/186)) + - A final solution extending the existing `StandardDynamicAssetCollection` is blocked on the next `ron` version (see [#151](https://github.com/NiklasEi/bevy_asset_loader/pull/151)) + - New example: [`dynamic_asset_arrays.rs`](bevy_asset_loader/examples/dynamic_asset_arrays.rs) ## v0.20.0 - 18.02.2024 - update to Bevy 0.13 diff --git a/bevy_asset_loader/Cargo.toml b/bevy_asset_loader/Cargo.toml index 2d724ce..db49390 100644 --- a/bevy_asset_loader/Cargo.toml +++ b/bevy_asset_loader/Cargo.toml @@ -99,6 +99,11 @@ name = "full_dynamic_collection" path = "examples/full_dynamic_collection.rs" required-features = ["2d", "3d", "standard_dynamic_assets"] +[[example]] +name = "dynamic_asset_arrays" +path = "examples/dynamic_asset_arrays.rs" +required-features = ["2d", "3d", "standard_dynamic_assets"] + [[example]] name = "custom_dynamic_assets" path = "examples/custom_dynamic_assets.rs" diff --git a/bevy_asset_loader/assets/dynamic_asset_arrays.asset_arrays.ron b/bevy_asset_loader/assets/dynamic_asset_arrays.asset_arrays.ron new file mode 100644 index 0000000..1656784 --- /dev/null +++ b/bevy_asset_loader/assets/dynamic_asset_arrays.asset_arrays.ron @@ -0,0 +1,41 @@ +({ + "layouts": [ + TextureAtlasLayout( + tile_size_x: 96., + tile_size_y: 99., + columns: 8, + rows: 1, + ), + TextureAtlasLayout( + tile_size_x: 32., + tile_size_y: 64., + columns: 12, + rows: 6, + ), + TextureAtlasLayout( + tile_size_x: 64., + tile_size_y: 32., + columns: 6, + rows: 12, + ), + TextureAtlasLayout( + tile_size_x: 64., + tile_size_y: 64., + columns: 6, + rows: 6, + ), + ], + "mixed": [ + StandardMaterial( + path: "images/tree.png", + ), + Image( + path: "images/female_adventurer_sheet.png", + sampler: Nearest, + ), + Image( + path: "images/female_adventurer_sheet.png", + sampler: Linear, + ), + ], +}) \ No newline at end of file diff --git a/bevy_asset_loader/examples/README.md b/bevy_asset_loader/examples/README.md index 7d7b69a..1439f28 100644 --- a/bevy_asset_loader/examples/README.md +++ b/bevy_asset_loader/examples/README.md @@ -3,22 +3,23 @@ These examples are simple Bevy Apps illustrating the capabilities of `bevy_asset_loader`. Run the examples with `cargo run --example `. -| Example | Description | -|------------------------------------------------------------|--------------------------------------------------------------------------| -| [`atlas_from_grid.rs`](atlas_from_grid.rs) | Loading a texture atlas from a sprite sheet | -| [`custom_dynamic_assets.rs`](custom_dynamic_assets.rs) | Define and use your own dynamic assets | -| [`dynamic_asset.rs`](dynamic_asset.rs) | Load dynamic assets from a `.ron` file | -| [`failure_state.rs`](failure_state.rs) | Sets up a failure state | -| [`full_collection.rs`](full_collection.rs) | A complete asset collection with all supported non-dynamic field types | +| Example | Description | +|----------------------------------------------------------|--------------------------------------------------------------------------| +| [`atlas_from_grid.rs`](atlas_from_grid.rs) | Loading a texture atlas from a sprite sheet | +| [`custom_dynamic_assets.rs`](custom_dynamic_assets.rs) | Define and use your own dynamic assets | +| [`dynamic_asset.rs`](dynamic_asset.rs) | Load dynamic assets from a `.ron` file | +| [`failure_state.rs`](failure_state.rs) | Sets up a failure state | +| [`full_collection.rs`](full_collection.rs) | A complete asset collection with all supported non-dynamic field types | | [`full_dynamic_collection.rs`](full_dynamic_collection.rs) | A complete asset collection with all supported dynamic asset field types | -| [`image_asset.rs`](image_asset.rs) | How to set different samplers for image assets | -| [`init_resource.rs`](init_resource.rs) | Inserting a `FromWorld` resource when all asset collections are loaded | -| [`manual_dynamic_asset.rs`](manual_dynamic_asset.rs) | Load an image asset from a path resolved at run time | -| [`no_loading_state.rs`](no_loading_state.rs) | How to use asset collections without a loading state | -| [`progress_tracking.rs`](progress_tracking.rs) | How to set up progress tracking using `iyes_progress` | -| [`standard_material.rs`](standard_material.rs) | Loading a standard material from a png file | -| [`two_collections.rs`](two_collections.rs) | Load multiple asset collections | -| [`asset_maps.rs`](asset_maps.rs) | Shows how to use different types as keys in asset maps | +| [`image_asset.rs`](image_asset.rs) | How to set different samplers for image assets | +| [`init_resource.rs`](init_resource.rs) | Inserting a `FromWorld` resource when all asset collections are loaded | +| [`manual_dynamic_asset.rs`](manual_dynamic_asset.rs) | Load an image asset from a path resolved at run time | +| [`no_loading_state.rs`](no_loading_state.rs) | How to use asset collections without a loading state | +| [`progress_tracking.rs`](progress_tracking.rs) | How to set up progress tracking using `iyes_progress` | +| [`standard_material.rs`](standard_material.rs) | Loading a standard material from a png file | +| [`two_collections.rs`](two_collections.rs) | Load multiple asset collections | +| [`asset_maps.rs`](asset_maps.rs) | Shows how to use different types as keys in asset maps | +| [`dynamic_asset_arrays.rs`](dynamic_asset_arrays.rs) | Defines dynamic assets in arrays | ## Credits diff --git a/bevy_asset_loader/examples/dynamic_asset_arrays.rs b/bevy_asset_loader/examples/dynamic_asset_arrays.rs new file mode 100644 index 0000000..d47472b --- /dev/null +++ b/bevy_asset_loader/examples/dynamic_asset_arrays.rs @@ -0,0 +1,93 @@ +use bevy::prelude::*; +use bevy_asset_loader::prelude::*; + +/// This example shows how to load an asset collection with dynamic assets defined in a `.ron` file. +/// +/// The assets loaded in this example are defined in `assets/dynamic_asset_arrays.asset_arrays.ron` +fn main() { + App::new() + .init_state::() + .add_plugins(DefaultPlugins) + .add_loading_state( + LoadingState::new(MyStates::AssetLoading) + .continue_to_state(MyStates::Next) + .with_dynamic_assets_file::( + "dynamic_asset_arrays.asset_arrays.ron", + ) + .load_collection::(), + ) + .add_systems(OnEnter(MyStates::Next), spawn_player_and_tree) + .add_systems( + Update, + animate_sprite_system.run_if(in_state(MyStates::Next)), + ) + .run(); +} + +// The keys used here are defined in `assets/dynamic_asset_arrays.assets` +#[derive(AssetCollection, Resource)] +struct ImageAssets { + #[asset(key = "layouts", collection(typed))] + atlas_layout: Vec>, + #[asset(key = "mixed", collection)] + mixed_handlers: Vec, +} + +fn spawn_player_and_tree(mut commands: Commands, image_assets: Res) { + commands.spawn(Camera2dBundle::default()); + let mut transform = Transform::from_translation(Vec3::new(0., 0., 1.)); + transform.scale = Vec3::splat(0.5); + commands + .spawn(SpriteSheetBundle { + transform: Transform { + translation: Vec3::new(0., 150., 0.), + ..Default::default() + }, + texture: image_assets.mixed_handlers[1].clone().typed(), + atlas: TextureAtlas { + layout: image_assets.atlas_layout[0].clone(), + index: 0, + }, + ..Default::default() + }) + .insert(AnimationTimer(Timer::from_seconds( + 0.1, + TimerMode::Repeating, + ))) + .insert(Player); + commands.spawn(SpriteBundle { + texture: image_assets.mixed_handlers[1].clone().typed(), + transform: Transform::from_translation(Vec3::new(50., 30., 1.)), + ..Default::default() + }); + commands.spawn(SpriteBundle { + texture: image_assets.mixed_handlers[2].clone().typed(), + transform: Transform::from_translation(Vec3::new(50., -90., 1.)), + ..Default::default() + }); +} + +#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)] +enum MyStates { + #[default] + AssetLoading, + Next, +} + +#[derive(Component)] +struct Player; + +#[derive(Component)] +struct AnimationTimer(Timer); + +fn animate_sprite_system( + time: Res