Skip to content

Commit

Permalink
Add example for new dynamic asset array collections
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Apr 13, 2024
1 parent 2bdc536 commit b98e64c
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 18 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions bevy_asset_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 41 additions & 0 deletions bevy_asset_loader/assets/dynamic_asset_arrays.asset_arrays.ron
Original file line number Diff line number Diff line change
@@ -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,
),
],
})
31 changes: 16 additions & 15 deletions bevy_asset_loader/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>`.

| 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

Expand Down
93 changes: 93 additions & 0 deletions bevy_asset_loader/examples/dynamic_asset_arrays.rs
Original file line number Diff line number Diff line change
@@ -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::<MyStates>()
.add_plugins(DefaultPlugins)
.add_loading_state(
LoadingState::new(MyStates::AssetLoading)
.continue_to_state(MyStates::Next)
.with_dynamic_assets_file::<StandardDynamicAssetArrayCollection>(
"dynamic_asset_arrays.asset_arrays.ron",
)
.load_collection::<ImageAssets>(),
)
.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<Handle<TextureAtlasLayout>>,
#[asset(key = "mixed", collection)]
mixed_handlers: Vec<UntypedHandle>,
}

fn spawn_player_and_tree(mut commands: Commands, image_assets: Res<ImageAssets>) {
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<Time>,
mut query: Query<(&mut AnimationTimer, &mut TextureAtlas)>,
) {
for (mut timer, mut sprite) in &mut query {
timer.0.tick(time.delta());
if timer.0.finished() {
sprite.index = (sprite.index + 1) % 8;
}
}
}
10 changes: 10 additions & 0 deletions bevy_asset_loader/src/loading_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use iyes_progress::TrackedProgressSet;

use crate::dynamic_asset::{DynamicAsset, DynamicAssets};
use crate::loading_state::systems::{apply_internal_state_transition, run_loading_state};
use crate::prelude::StandardDynamicAssetArrayCollection;

/// A Bevy plugin to configure automatic asset loading
///
Expand Down Expand Up @@ -352,6 +353,12 @@ where
&self.standard_dynamic_asset_collection_file_endings,
));
}
#[cfg(feature = "standard_dynamic_assets")]
if !app.is_plugin_added::<RonAssetPlugin<StandardDynamicAssetArrayCollection>>() {
app.add_plugins(RonAssetPlugin::<StandardDynamicAssetArrayCollection>::new(
&[],
));
}

if !app.is_plugin_added::<InternalAssetLoaderPlugin<S>>() {
app.add_plugins(InternalAssetLoaderPlugin::<S>::new());
Expand Down Expand Up @@ -423,6 +430,9 @@ where
self.config = self
.config
.register_dynamic_asset_collection::<StandardDynamicAssetCollection>();
self.config = self
.config
.register_dynamic_asset_collection::<StandardDynamicAssetArrayCollection>();
}

#[cfg(feature = "progress_tracking")]
Expand Down

0 comments on commit b98e64c

Please sign in to comment.