Skip to content

Commit

Permalink
Merge pull request #189 from NiklasEi/bevy_main
Browse files Browse the repository at this point in the history
Update to Bevy 0.13
  • Loading branch information
NiklasEi committed Feb 18, 2024
2 parents c6b06f8 + ef1b591 commit b3691eb
Show file tree
Hide file tree
Showing 50 changed files with 322 additions and 408 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Install alsa and udev
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev
if: runner.os == 'linux'
- name: Build & run tests for 2d, 3d, standard
- name: Build & run tests
run: cargo test -p bevy_asset_loader
- name: Build & run tests for derive package
run: cargo test -p bevy_asset_loader_derive
Expand All @@ -54,9 +54,9 @@ jobs:
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev
if: runner.os == 'linux'
- name: Build & run tests for 2d, 3d, standard
run: cargo test --features "2d","3d","standard_dynamic_assets" -p bevy_asset_loader
run: cargo test --features "2d, 3d, standard_dynamic_assets" -p bevy_asset_loader
- name: Build & run tests for derive package
run: cargo test --features "2d","3d" -p bevy_asset_loader_derive
run: cargo test --features "2d, 3d" -p bevy_asset_loader_derive
progress-tracking-test:
strategy:
matrix:
Expand All @@ -82,7 +82,7 @@ jobs:
- name: Build & run tests progress tracking
run: cargo test --features "progress_tracking" -p bevy_asset_loader
- name: Build & run tests progress tracking and 2d,3d,dynamic
run: cargo test --features "2d","3d","standard_dynamic_assets","progress_tracking" -p bevy_asset_loader
run: cargo test --features "2d, 3d, standard_dynamic_assets, progress_tracking" -p bevy_asset_loader
lint:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

## v0.20.0 - 18.02.2024
- update to Bevy 0.13
- support any type implementing the new trait `MapKey` as keys for mapped assets (resolves [#153](https://github.com/NiklasEi/bevy_asset_loader/issues/153))
- derive Serialize on StandardDynamicAssets (resolves [#177](https://github.com/NiklasEi/bevy_asset_loader/issues/177))

Expand Down
52 changes: 30 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This [Bevy][bevy] plugin reduces boilerplate for handling game assets. The crate

In most cases you will want to load your asset collections during loading states (think loading screens). During such a state, all assets are loaded and their loading progress is observed. Only when asset collections can be built with fully loaded asset handles, the collections are inserted to Bevy's ECS as resources. If you do not want to use a loading state, asset collections can still result in cleaner code and improved maintainability (see the ["usage without a loading state"](#usage-without-a-loading-state) section).

_The `main` branch and the latest release support Bevy version `0.12` (see [version table](#compatible-bevy-versions))_
_The `main` branch and the latest release support Bevy version `0.13` (see [version table](#compatible-bevy-versions))_

## Loading states

Expand All @@ -26,7 +26,7 @@ use bevy_asset_loader::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_state::<MyStates>()
.init_state::<MyStates>()
.add_loading_state(
LoadingState::new(MyStates::AssetLoading)
.continue_to_state(MyStates::Next)
Expand Down Expand Up @@ -187,17 +187,18 @@ The following sections describe more types of asset fields that you can load thr

### Texture atlases

You can directly load texture atlases from sprite sheets if you enable the feature `2d`. For a complete example please take a look at [atlas_from_grid.rs](/bevy_asset_loader/examples/atlas_from_grid.rs).
You can create texture atlas layouts as part of an `AssetCollection` if you enable the feature `2d`. For a complete example please take a look at [atlas_from_grid.rs](/bevy_asset_loader/examples/atlas_from_grid.rs).

```rust
use bevy::prelude::*;
use bevy_asset_loader::asset_collection::AssetCollection;

#[derive(AssetCollection, Resource)]
struct MyAssets {
#[asset(texture_atlas(tile_size_x = 64., tile_size_y = 64., columns = 8, rows = 1, padding_x = 12., padding_y = 12., offset_x = 6., offset_y = 6.))]
#[asset(texture_atlas_layout(tile_size_x = 64., tile_size_y = 64., columns = 8, rows = 1, padding_x = 12., padding_y = 12., offset_x = 6., offset_y = 6.))]
layout: Handle<TextureAtlasLayout>,
#[asset(path = "images/sprite_sheet.png")]
sprite: Handle<TextureAtlas>,
sprite: Handle<Image>,
}
```

Expand All @@ -206,15 +207,19 @@ As a dynamic asset this example becomes:
```rust ignore
#[derive(AssetCollection, Resource)]
struct MyAssets {
#[asset(key = "image.player")]
sprite: Handle<TextureAtlas>,
#[asset(key = "player.layout")]
layout: Handle<TextureAtlasLayout>,
#[asset(key = "player.image")]
sprite: Handle<Image>,
}
```

```ron
({
"image.player": TextureAtlas (
"player.image": File (
path: "images/sprite_sheet.png",
),
"player.layout": TextureAtlasLayout (
tile_size_x: 100.,
tile_size_y: 64.,
columns: 8,
Expand Down Expand Up @@ -478,9 +483,10 @@ fn main() {

#[derive(AssetCollection, Resource)]
struct MyAssets {
#[asset(texture_atlas(tile_size_x = 100., tile_size_y = 96., columns = 8, rows = 1, padding_x = 12., padding_y = 12.))]
#[asset(texture_atlas_layout(tile_size_x = 64., tile_size_y = 64., columns = 8, rows = 1, padding_x = 12., padding_y = 12., offset_x = 6., offset_y = 6.))]
layout: Handle<TextureAtlasLayout>,
#[asset(path = "images/sprite_sheet.png")]
sprite: Handle<TextureAtlas>,
sprite: Handle<Image>,
}
```

Expand All @@ -493,18 +499,20 @@ Bevy unloads an asset when there are no strong asset handles left pointing to th
The main branch is compatible with the latest Bevy release, while the branch `bevy_main` tries to track the `main` branch of Bevy (PRs updating the tracked commit are welcome).

Compatibility of `bevy_asset_loader` versions:
| `bevy_asset_loader` | `bevy` |
| :-- | :-- |
| `0.18` - `0.19` | `0.12` |
| `0.17` | `0.11` |
| `0.15` - `0.16` | `0.10` |
| `0.14` | `0.9` |
| `0.12` - `0.13` | `0.8` |
| `0.10` - `0.11` | `0.7` |
| `0.8` - `0.9` | `0.6` |
| `0.1` - `0.7` | `0.5` |
| branch `main` | `0.12` |
| branch `bevy_main` | `main` |

| Bevy version | `bevy_asset_loader` version |
|:-------------|:----------------------------|
| `0.13` | `0.20` |
| `0.12` | `0.18` - `0.19` |
| `0.11` | `0.17` |
| `0.10` | `0.15` - `0.16` |
| `0.9` | `0.14` |
| `0.8` | `0.12` - `0.13` |
| `0.7` | `0.10` - `0.11` |
| `0.6` | `0.8` - `0.9` |
| `0.5` | `0.1` - `0.7` |
| `0.13` | branch `main` |
| `main` | branch `bevy_main` |

## License

Expand Down
16 changes: 8 additions & 8 deletions bevy_asset_loader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_asset_loader"
version = "0.19.1"
version = "0.20.0"
authors = ["Niklas Eicker <git@nikl.me>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -22,20 +22,20 @@ standard_dynamic_assets = ["dep:bevy_common_assets", "dep:serde"]
progress_tracking = ["dep:iyes_progress"]

[dependencies]
bevy = { version = "0.12", default-features = false, features = ["bevy_asset"] }
bevy_asset_loader_derive = { version = "=0.19.0", path = "../bevy_asset_loader_derive" }
bevy = { version = "0.13", default-features = false, features = ["bevy_asset"] }
bevy_asset_loader_derive = { version = "=0.20.0", path = "../bevy_asset_loader_derive" }
anyhow = "1"
path-slash = "0.2"

bevy_common_assets = { version = "0.9.0", features = ["ron"], optional = true }
bevy_common_assets = { version = "0.10", features = ["ron"], optional = true }
serde = { version = "1", optional = true }
iyes_progress = { version = "0.10", optional = true }
iyes_progress = { version = "0.11", optional = true }

[dev-dependencies]
bevy = { version = "0.12", features = ["vorbis"] }
bevy = { version = "0.13", features = ["vorbis"] }
anyhow = "1"
iyes_progress = { version = "0.10" }
bevy_common_assets = { version = "0.9.0", features = ["ron"] }
iyes_progress = { version = "0.11" }
bevy_common_assets = { version = "0.10", features = ["ron"] }
serde = { version = "1" }
ron = "0.8.1"
trybuild = { version = "1.0" }
Expand Down
4 changes: 3 additions & 1 deletion bevy_asset_loader/assets/dynamic_asset.assets.ron
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
({
"image.player": TextureAtlas (
"image.player_sheet": File (
path: "images/female_adventurer_sheet.png",
),
"layout.player_sheet": TextureAtlasLayout (
tile_size_x: 96.,
tile_size_y: 99.,
columns: 8,
Expand Down
4 changes: 1 addition & 3 deletions bevy_asset_loader/assets/full_dynamic_collection.assets.ron
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"standard_material": StandardMaterial (
path: "images/tree.png",
),
"texture_atlas": TextureAtlas (
path: "images/female_adventurer_sheet.png",
sampler: Nearest,
"texture_atlas_layout": TextureAtlasLayout (
tile_size_x: 96.,
tile_size_y: 99.,
columns: 8,
Expand Down
46 changes: 18 additions & 28 deletions bevy_asset_loader/examples/atlas_from_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_asset_loader::prelude::*;
/// Requires the feature '2d'
fn main() {
App::new()
.add_state::<MyStates>()
.init_state::<MyStates>()
.add_plugins(DefaultPlugins)
.add_loading_state(
LoadingState::new(MyStates::AssetLoading)
Expand All @@ -25,51 +25,41 @@ fn main() {
struct MyAssets {
// if the sheet would have padding, you could set that with `padding_x` and `padding_y`.
// if there would be space between the top left corner of the sheet and the first sprite, you could configure that with `offset_x` and `offset_y`
#[asset(texture_atlas(tile_size_x = 96., tile_size_y = 99., columns = 8, rows = 1))]
// A texture atlas layout does not have a path as no asset file will be loaded for the layout
#[asset(texture_atlas_layout(tile_size_x = 96., tile_size_y = 99., columns = 8, rows = 1))]
female_adventurer_layout: Handle<TextureAtlasLayout>,
// you can configure the sampler for the sprite sheet image
#[asset(image(sampler = nearest))]
#[asset(path = "images/female_adventurer_sheet.png")]
female_adventurer: Handle<TextureAtlas>,
female_adventurer: Handle<Image>,
}

fn draw_atlas(
mut commands: Commands,
my_assets: Res<MyAssets>,
texture_atlases: Res<Assets<TextureAtlas>>,
) {
fn draw_atlas(mut commands: Commands, my_assets: Res<MyAssets>) {
commands.spawn(Camera2dBundle::default());
// draw the original image (whole atlas)
let atlas = texture_atlases
.get(&my_assets.female_adventurer)
.expect("Failed to find our atlas");
// draw the original image (whole sprite sheet)
commands.spawn(SpriteBundle {
texture: atlas.texture.clone(),
texture: my_assets.female_adventurer.clone(),
transform: Transform::from_xyz(0., -150., 0.),
..Default::default()
});
// draw single texture from sprite sheet starting at index 0
commands
.spawn(SpriteSheetBundle {
transform: Transform {
translation: Vec3::new(0., 150., 0.),
..Default::default()
},
sprite: TextureAtlasSprite::new(0),
texture_atlas: my_assets.female_adventurer.clone(),
// draw animated sprite using the texture atlas layout
commands.spawn((
SpriteBundle {
texture: my_assets.female_adventurer.clone(),
transform: Transform::from_xyz(0., 150., 0.),
..Default::default()
})
.insert(AnimationTimer(Timer::from_seconds(
0.1,
TimerMode::Repeating,
)));
},
TextureAtlas::from(my_assets.female_adventurer_layout.clone()),
AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
));
}

#[derive(Component)]
struct AnimationTimer(Timer);

fn animate_sprite_system(
time: Res<Time>,
mut sprites_to_animate: Query<(&mut AnimationTimer, &mut TextureAtlasSprite)>,
mut sprites_to_animate: Query<(&mut AnimationTimer, &mut TextureAtlas)>,
) {
for (mut timer, mut sprite) in &mut sprites_to_animate {
timer.0.tick(time.delta());
Expand Down
2 changes: 1 addition & 1 deletion bevy_asset_loader/examples/configure_loading_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct MainPlugin;

impl Plugin for MainPlugin {
fn build(&self, app: &mut App) {
app.add_state::<MyStates>()
app.init_state::<MyStates>()
// General loading state setup goes here, but if you like, you can already add all
// the configuration at this point, too. In this example we will configure the loading state
// later in PlayerAndMusicPlugin.
Expand Down
13 changes: 7 additions & 6 deletions bevy_asset_loader/examples/custom_dynamic_assets.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::core_pipeline::clear_color::ClearColorConfig;
use bevy::prelude::*;
use bevy::reflect::TypePath;
use bevy::render::render_asset::RenderAssetUsages;
use bevy::utils::HashMap;
use bevy_asset_loader::prelude::*;
use bevy_common_assets::ron::RonAssetPlugin;
Expand All @@ -12,7 +12,7 @@ fn main() {
RonAssetPlugin::<CustomDynamicAssetCollection>::new(&["my-assets.ron"]),
))
// We need to make sure that our dynamic asset collections can be loaded from the asset file
.add_state::<MyStates>()
.init_state::<MyStates>()
.add_loading_state(
LoadingState::new(MyStates::AssetLoading)
.continue_to_state(MyStates::Next)
Expand Down Expand Up @@ -54,10 +54,8 @@ fn render_stuff(mut commands: Commands, assets: Res<MyAssets>) {
commands.spawn(Camera2dBundle {
camera: Camera {
order: 1,
..default()
},
camera_2d: Camera2d {
clear_color: ClearColorConfig::None,
..default()
},
..default()
});
Expand Down Expand Up @@ -154,6 +152,7 @@ impl DynamicAsset for CustomDynamicAsset {
})
.collect(),
second.texture_descriptor.format,
RenderAssetUsages::all(),
);

Ok(DynamicAssetType::Single(images.add(combined).untyped()))
Expand All @@ -178,7 +177,9 @@ impl DynamicAsset for CustomDynamicAsset {
.get_resource_mut::<Assets<Mesh>>()
.expect("Failed to get mesh assets");
let handle = meshes
.add(Mesh::from(shape::Cube { size: *size }))
.add(Mesh::from(Cuboid {
half_size: Vec3::splat(size / 2.),
}))
.untyped();

Ok(DynamicAssetType::Single(handle))
Expand Down
17 changes: 11 additions & 6 deletions bevy_asset_loader/examples/dynamic_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_asset_loader::prelude::*;
/// The assets loaded in this example are defined in `assets/dynamic_asset.assets.ron`
fn main() {
App::new()
.add_state::<MyStates>()
.init_state::<MyStates>()
.add_plugins(DefaultPlugins)
.add_loading_state(
LoadingState::new(MyStates::AssetLoading)
Expand All @@ -31,8 +31,10 @@ fn main() {
// The keys used here are defined in `assets/dynamic_asset_ron.assets`
#[derive(AssetCollection, Resource)]
struct ImageAssets {
#[asset(key = "image.player")]
player: Handle<TextureAtlas>,
#[asset(key = "layout.player_sheet")]
player_layout: Handle<TextureAtlasLayout>,
#[asset(key = "image.player_sheet")]
player: Handle<Image>,
#[asset(key = "image.tree")]
tree: Handle<Image>,
}
Expand All @@ -53,8 +55,11 @@ fn spawn_player_and_tree(mut commands: Commands, image_assets: Res<ImageAssets>)
translation: Vec3::new(0., 150., 0.),
..Default::default()
},
sprite: TextureAtlasSprite::new(0),
texture_atlas: image_assets.player.clone(),
texture: image_assets.player.clone(),
atlas: TextureAtlas {
layout: image_assets.player_layout.clone(),
index: 0,
},
..Default::default()
})
.insert(AnimationTimer(Timer::from_seconds(
Expand Down Expand Up @@ -91,7 +96,7 @@ struct AnimationTimer(Timer);

fn animate_sprite_system(
time: Res<Time>,
mut query: Query<(&mut AnimationTimer, &mut TextureAtlasSprite)>,
mut query: Query<(&mut AnimationTimer, &mut TextureAtlas)>,
) {
for (mut timer, mut sprite) in &mut query {
timer.0.tick(time.delta());
Expand Down
2 changes: 1 addition & 1 deletion bevy_asset_loader/examples/failure_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_asset_loader::prelude::*;

fn main() {
App::new()
.add_state::<MyStates>()
.init_state::<MyStates>()
.add_plugins(DefaultPlugins)
.add_loading_state(
LoadingState::new(MyStates::AssetLoading)
Expand Down
Loading

0 comments on commit b3691eb

Please sign in to comment.