Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Bevy 0.15 #52

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "leafwing_manifest"
version = "0.2.0"
version = "0.3.0"
authors = ["Leafwing Studios"]
homepage = "https://leafwing-studios.com/"
repository = "https://github.com/leafwing-studios/leafwing_manifest"
Expand All @@ -15,11 +15,11 @@ exclude = ["assets/**/*", "tools/**/*", ".github/**/*"]
members = ["./", "tools/ci"]

[dependencies]
bevy = { version = "0.14", default-features = false, features = [
bevy = { version = "0.15", default-features = false, features = [
"bevy_asset",
"bevy_state",
] }
bevy_common_assets = { version = "0.11.0", default-features = false }
bevy_common_assets = { version = "0.12.0", default-features = false }
serde = "1.0.195"
thiserror = "1.0.58"

Expand Down Expand Up @@ -62,7 +62,7 @@ ron = "0.8"
# Enables non-default features for examples and tests.
leafwing_manifest = { path = ".", features = ["ron"] }
# Give us access to the full Bevy default features for examples.
bevy = { version = "0.14" }
bevy = { version = "0.15" }

[workspace.lints.rust]
unsafe_code = "forbid"
Expand Down
4 changes: 4 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 0.3

- Now support Bevy 0.15

## 0.2

- Now support Bevy 0.14
Expand Down
22 changes: 10 additions & 12 deletions examples/custom_asset_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
//! As this example demonstrates, you can bypass the [`ManifestPlugin`](leafwing_manifest::plugin::ManifestPlugin) entirely, and load your assets however you like,
//! calling the publicly exposed methods yourself to replicate the work it does.
use std::future::Future;

use bevy::{
asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext, LoadState},
asset::{io::Reader, AssetLoader, LoadContext, LoadState},
prelude::*,
utils::ConditionalSendFuture,
};
Expand Down Expand Up @@ -126,14 +124,14 @@ impl AssetLoader for ItemAssetLoader {
type Settings = ();
type Error = RonLoaderError;

fn load<'a>(
&'a self,
reader: &'a mut Reader,
_settings: &'a Self::Settings,
_load_context: &'a mut LoadContext,
) -> impl ConditionalSendFuture
+ Future<Output = Result<<Self as AssetLoader>::Asset, <Self as AssetLoader>::Error>>
{
fn load(
&self,
reader: &mut dyn Reader,
_settings: &Self::Settings,
_load_context: &mut LoadContext,
) -> impl ConditionalSendFuture<
Output = Result<<Self as AssetLoader>::Asset, <Self as AssetLoader>::Error>,
> {
Box::pin(async move {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Expand Down Expand Up @@ -212,7 +210,7 @@ fn manage_manifests(

// We're deferring the actual work with commands to avoid blocking the whole world
// every time this system runs.
commands.add(|world: &mut World| {
commands.queue(|world: &mut World| {
let item_manifest = ItemManifest::from_raw_manifest(raw_manifest, world).unwrap();

world.insert_resource(item_manifest);
Expand Down
23 changes: 12 additions & 11 deletions examples/entities_from_manifests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! If you need to spawn a scene hierarchy (such as for levels or 3D models), storing a handle to that scene can work well,
//! or a scene bundle can be added to your custom bundle type.
use bevy::{prelude::*, sprite::Mesh2dHandle, utils::HashMap};
use bevy::{prelude::*, utils::HashMap};
use leafwing_manifest::{
asset_state::SimpleAssetState,
identifier::Id,
Expand All @@ -37,7 +37,7 @@ pub struct Tile {
color_material: Handle<ColorMaterial>,
// The same square mesh is used for all tiles,
// and can be procedurally generated during .
mesh: Mesh2dHandle,
mesh: Mesh2d,
tile_type: TileType,
}

Expand All @@ -56,10 +56,10 @@ pub struct TileBundle {
// It also serves as a nice way to filter for tiles in queries.
id: Id<Tile>,
tile_type: TileType,
material: Handle<ColorMaterial>,
mesh: Mesh2dHandle,
// Add all of the components needed to render the tile.
spatial_bundle: SpatialBundle,
material: MeshMaterial2d<ColorMaterial>,
mesh: Mesh2d,
transform: Transform,
visibility: Visibility,
}

impl TileBundle {
Expand All @@ -74,12 +74,13 @@ impl TileBundle {
tile_type: tile.tile_type,
// We can use weak clones here and save a tiny bit of work,
// since the manifest will always store a canonical strong handle to the assets.
material: tile.color_material.clone_weak(),
material: MeshMaterial2d(tile.color_material.clone_weak()),
// While the value of the mesh is the same for all tiles, passing around `&Assets<Mesh>` everywhere
// is miserable. Instead, we sacrifice a little bit of memory to redundantly store the mesh handle in the manifest:
// like always, the mesh itself is only stored once in the asset storage.
mesh: tile.mesh.clone(),
spatial_bundle: SpatialBundle::from_transform(transform),
transform,
visibility: Visibility::default(),
}
}
}
Expand Down Expand Up @@ -113,7 +114,7 @@ impl Manifest for TileManifest {
let mut meshes = world.resource_mut::<Assets<Mesh>>();
let mesh = meshes.add(Mesh::from(Rectangle::new(1.0, 1.0)));
// This is a thin wrapper around a `Handle<Mesh>`, used in 2D rendering.
let mesh_2d = Mesh2dHandle::from(mesh.clone());
let mesh_2d = Mesh2d(mesh.clone());

let mut color_materials = world.resource_mut::<Assets<ColorMaterial>>();

Expand Down Expand Up @@ -151,8 +152,8 @@ pub fn spawn_tiles(mut commands: Commands, tile_manifest: Res<TileManifest>) {

info!("Spawning tiles...");

// Remember to add the camera bundle to the world, or you won't see anything!
commands.spawn(Camera2dBundle::default());
// Remember to add the camera to the world, or you won't see anything!
commands.spawn(Camera2d);

for (i, tile) in tile_manifest.tiles.values().enumerate() {
info!("Spawning tile: {:?}", tile);
Expand Down
9 changes: 7 additions & 2 deletions examples/items_by_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//!
//! This code is largely copied from the `simple.rs` example: we're just adding constants and a new system to demonstrate the name-based lookups.

use bevy::{log::LogPlugin, prelude::*, utils::HashMap};
use bevy::{log::LogPlugin, prelude::*, state::app::StatesPlugin, utils::HashMap};
use leafwing_manifest::{
asset_state::SimpleAssetState,
identifier::Id,
Expand Down Expand Up @@ -67,7 +67,12 @@ impl Manifest for ItemManifest {

fn main() {
App::new()
.add_plugins((MinimalPlugins, AssetPlugin::default(), LogPlugin::default()))
.add_plugins((
MinimalPlugins,
AssetPlugin::default(),
LogPlugin::default(),
StatesPlugin,
))
.init_state::<SimpleAssetState>()
.add_plugins(ManifestPlugin::<SimpleAssetState>::default())
.register_manifest::<ItemManifest>("items.ron")
Expand Down
11 changes: 8 additions & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! See the other examples for more advanced use cases!
//! The `raw_manifest.rs` example is a good next step that builds upon this example.

use bevy::{app::AppExit, log::LogPlugin, prelude::*, utils::HashMap};
use bevy::{app::AppExit, log::LogPlugin, prelude::*, state::app::StatesPlugin, utils::HashMap};
use leafwing_manifest::{
asset_state::SimpleAssetState,
identifier::Id,
Expand Down Expand Up @@ -69,9 +69,14 @@ impl Manifest for ItemManifest {

fn main() {
App::new()
// leafwing_manifest requires `AssetPlugin` to function
// leafwing_manifest requires `AssetPlugin`, and `StatesPlugin` to function
// This is included in `DefaultPlugins`, but this example is very small, so it only uses the `MinimalPlugins`
.add_plugins((MinimalPlugins, AssetPlugin::default(), LogPlugin::default()))
.add_plugins((
MinimalPlugins,
AssetPlugin::default(),
LogPlugin::default(),
StatesPlugin,
))
// This is our simple state, used to navigate the asset loading process.
.init_state::<SimpleAssetState>()
// Coordinates asset loading and state transitions.
Expand Down
8 changes: 4 additions & 4 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl RegisterManifest for App {
.add_systems(
Update,
report_failed_raw_manifest_loading::<M>
.run_if(on_event::<AssetLoadFailedEvent<M::RawManifest>>()),
.run_if(on_event::<AssetLoadFailedEvent<M::RawManifest>>),
)
.add_systems(
PreUpdate,
Expand Down Expand Up @@ -188,7 +188,7 @@ pub enum ProcessingStatus {
}

/// Information about the loading status of a raw manifest.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone)]
pub struct RawManifestStatus {
/// The path to the manifest file.
pub path: PathBuf,
Expand Down Expand Up @@ -247,7 +247,7 @@ impl RawManifestTracker {

self.raw_manifests
.values()
.all(|status| status.load_state == LoadState::Loaded)
.all(|status| status.load_state.is_loaded())
}

/// Returns true if any registered raw manifests have failed to load.
Expand All @@ -256,7 +256,7 @@ impl RawManifestTracker {

self.raw_manifests
.values()
.any(|status| matches!(status.load_state, LoadState::Failed(..)))
.any(|status| status.load_state.is_failed())
}

/// Returns the [`ProcessingStatus`] of the raw manifests.
Expand Down