-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for new dynamic asset array collections
- Loading branch information
Showing
6 changed files
with
170 additions
and
18 deletions.
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
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
41 changes: 41 additions & 0 deletions
41
bevy_asset_loader/assets/dynamic_asset_arrays.asset_arrays.ron
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,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, | ||
), | ||
], | ||
}) |
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
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; | ||
} | ||
} | ||
} |
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