Skip to content
Snippets Groups Projects
Verified Commit 0db0afa9 authored by Louis's avatar Louis :fire:
Browse files

Support Bevy 0.14

parent 3cf1e7d4
No related branches found
No related tags found
No related merge requests found
[package] [package]
name = "micro_games_macros" name = "micro_games_macros"
version = "0.4.0" version = "0.5.0"
edition = "2021" edition = "2021"
authors = ["Louis Capitanchik <contact@louiscap.co>"] authors = ["Louis Capitanchik <contact@louiscap.co>"]
description = "Utility macros to make it easier to build complex systems with Bevy" description = "Utility macros to make it easier to build complex systems with Bevy"
...@@ -25,13 +25,14 @@ test-case = "3.3.1" ...@@ -25,13 +25,14 @@ test-case = "3.3.1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
anyhow = "1.0" anyhow = "1.0"
micro_bevy_world_utils = "0.4.0" micro_bevy_world_utils = "0.5"
[dev-dependencies.bevy] [dev-dependencies.bevy]
version = "0.13" version = "0.14"
default-features = false default-features = false
features = [ features = [
"bevy_asset", "bevy_asset",
"bevy_sprite", "bevy_sprite",
"bevy_core_pipeline", "bevy_core_pipeline",
"serialize"
] ]
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
A collection of utility macros for building games A collection of utility macros for building games
**Current Version Support: 0.2.x -> Bevy 0.12** **Current Version Support: 0.5.x -> Bevy 0.14**
## Macros ## Macros
...@@ -13,8 +13,10 @@ For executable examples, visit the rustdoc and/or read the doctests in `src/lib. ...@@ -13,8 +13,10 @@ For executable examples, visit the rustdoc and/or read the doctests in `src/lib.
### JSON Loader ### JSON Loader
Generate a Bevy asset loader for a given asset, supporting JSON files that define a single instance of that asset type, or a Generate a Bevy asset loader for a given asset, supporting JSON files that define a single instance of that asset type,
list of that asset type. Instances of an asset need to be identifiable, though the property that is used to identify a particular or a
list of that asset type. Instances of an asset need to be identifiable, though the property that is used to identify a
particular
asset is customisable asset is customisable
```rust ```rust
...@@ -26,9 +28,9 @@ asset is customisable ...@@ -26,9 +28,9 @@ asset is customisable
)] )]
#[uuid = "00000000-0000-0000-0000-000000000001"] #[uuid = "00000000-0000-0000-0000-000000000001"]
pub struct MyAsset { pub struct MyAsset {
/// The asset identifier needs to implement [std::fmt::Display] /// The asset identifier needs to implement [std::fmt::Display]
#[asset_id] #[asset_id]
uniq_ident: usize, uniq_ident: usize,
} }
``` ```
...@@ -42,14 +44,14 @@ use micro_games_macros::asset_system; ...@@ -42,14 +44,14 @@ use micro_games_macros::asset_system;
#[asset_system] #[asset_system]
pub struct AssetHandles { pub struct AssetHandles {
my_asset: Image, my_asset: Image,
} }
pub fn loading_system(mut loader: AssetHandlesLoader) { pub fn loading_system(mut loader: AssetHandlesLoader) {
loader.load_my_asset("path/to/asset.png", "Asset"); loader.load_my_asset("path/to/asset.png", "Asset");
} }
pub fn use_asset_system(assets: Res<AssetHandles>) { pub fn use_asset_system(assets: Res<AssetHandles>) {
let handle = assets.my_asset("Asset"); let handle = assets.my_asset("Asset");
} }
``` ```
......
...@@ -56,7 +56,6 @@ fq!(BevyAsset => ::bevy::asset::Asset); ...@@ -56,7 +56,6 @@ fq!(BevyAsset => ::bevy::asset::Asset);
fq!(BevyAssets => ::bevy::asset::Assets); fq!(BevyAssets => ::bevy::asset::Assets);
fq!(BevyAssetEvent => ::bevy::asset::AssetEvent); fq!(BevyAssetEvent => ::bevy::asset::AssetEvent);
fq!(BevyAssetLoader => ::bevy::asset::AssetLoader); fq!(BevyAssetLoader => ::bevy::asset::AssetLoader);
fq!(BevyBoxedFuture => ::bevy::asset::BoxedFuture);
fq!(BevyLoadContext => ::bevy::asset::LoadContext); fq!(BevyLoadContext => ::bevy::asset::LoadContext);
fq!(BevyLoadedAsset => ::bevy::asset::LoadedAsset); fq!(BevyLoadedAsset => ::bevy::asset::LoadedAsset);
fq!(BevyAssetServer => ::bevy::asset::AssetServer); fq!(BevyAssetServer => ::bevy::asset::AssetServer);
......
...@@ -122,20 +122,28 @@ pub fn define_loader( ...@@ -122,20 +122,28 @@ pub fn define_loader(
type Settings = (); type Settings = ();
type Error = #error_name; type Error = #error_name;
fn load<'a>( async fn load<'a>(
&'a self, &'a self,
mut reader: &'a mut #BevyAssetReader, mut reader: &'a mut #BevyAssetReader<'_>,
_settings: &'a Self::Settings, _settings: &'a Self::Settings,
load_context: &'a mut #BevyLoadContext, load_context: &'a mut #BevyLoadContext<'_>,
) -> #BevyBoxedFuture<'a, #FQResult<Self::Asset, Self::Error>> { ) -> #FQResult<Self::Asset, Self::Error> {
#FQBox::pin(async move { let mut bytes = #FQVec::new();
let mut bytes = #FQVec::new(); #BevyAsyncRead::read_to_end(&mut reader, &mut bytes).await?;
#BevyAsyncRead::read_to_end(&mut reader, &mut bytes).await?; let data: #set_name = ::serde_json::from_slice(bytes.as_slice())?;
let data: #set_name = ::serde_json::from_slice(bytes.as_slice())?;
let mut asset_map = #FQHashMap::new();
let mut asset_map = #FQHashMap::new(); match data {
match data { #set_name::One(single_asset) => {
#set_name::One(single_asset) => { let asset_id = format!("{}", &single_asset.#id_field);
let handle = load_context.add_labeled_asset(
asset_id.clone(),
single_asset,
);
asset_map.insert(asset_id, handle);
}
#set_name::Many(asset_list) => {
for single_asset in asset_list.into_iter() {
let asset_id = format!("{}", &single_asset.#id_field); let asset_id = format!("{}", &single_asset.#id_field);
let handle = load_context.add_labeled_asset( let handle = load_context.add_labeled_asset(
asset_id.clone(), asset_id.clone(),
...@@ -143,19 +151,9 @@ pub fn define_loader( ...@@ -143,19 +151,9 @@ pub fn define_loader(
); );
asset_map.insert(asset_id, handle); asset_map.insert(asset_id, handle);
} }
#set_name::Many(asset_list) => {
for single_asset in asset_list.into_iter() {
let asset_id = format!("{}", &single_asset.#id_field);
let handle = load_context.add_labeled_asset(
asset_id.clone(),
single_asset,
);
asset_map.insert(asset_id, handle);
}
}
} }
Ok(#index_name(asset_map)) }
}) Ok(#index_name(asset_map))
} }
fn extensions(&self) -> &[&str] { fn extensions(&self) -> &[&str] {
...@@ -213,7 +211,7 @@ pub fn define_load_handler( ...@@ -213,7 +211,7 @@ pub fn define_load_handler(
match event { match event {
#BevyAssetEvent::LoadedWithDependencies { id } | #BevyAssetEvent::Added { id } | #BevyAssetEvent::Modified { id } => { #BevyAssetEvent::LoadedWithDependencies { id } | #BevyAssetEvent::Added { id } | #BevyAssetEvent::Modified { id } => {
let handle = #BevyHandle::Weak(*id); let handle = #BevyHandle::Weak(*id);
if let Some(asset_container) = asset_data.get(handle) { if let Some(asset_container) = asset_data.get(&handle) {
for (id, handle) in asset_container.iter() { for (id, handle) in asset_container.iter() {
asset_storage.#storage_asset_name.insert(id.clone(), handle.clone()); asset_storage.#storage_asset_name.insert(id.clone(), handle.clone());
} }
......
...@@ -249,7 +249,7 @@ pub fn json_loader(input: TokenStream) -> TokenStream { ...@@ -249,7 +249,7 @@ pub fn json_loader(input: TokenStream) -> TokenStream {
/// The included property must be a `SystemParam`, and has access to the `'w` lifetime /// The included property must be a `SystemParam`, and has access to the `'w` lifetime
/// ///
/// ```rust /// ```rust
/// use bevy::prelude::{Assets, Event, EventWriter, Handle, Image, ResMut, TextureAtlasLayout, Vec2}; /// use bevy::prelude::{Assets, Event, EventWriter, Handle, Image, ResMut, TextureAtlasLayout, UVec2};
/// use micro_games_macros::{asset_system, loader_property}; /// use micro_games_macros::{asset_system, loader_property};
/// ///
/// #[derive(Event)] /// #[derive(Event)]
...@@ -269,7 +269,7 @@ pub fn json_loader(input: TokenStream) -> TokenStream { ...@@ -269,7 +269,7 @@ pub fn json_loader(input: TokenStream) -> TokenStream {
/// impl<'w> AssetHandlesLoader<'w> { /// impl<'w> AssetHandlesLoader<'w> {
/// pub fn load_spritesheet(&mut self, path: String, name: String) -> Handle<TextureAtlasLayout> { /// pub fn load_spritesheet(&mut self, path: String, name: String) -> Handle<TextureAtlasLayout> {
/// let image_handle = self.load_image(path, name.clone()); /// let image_handle = self.load_image(path, name.clone());
/// let sheet = TextureAtlasLayout::new_empty(Vec2::ZERO); /// let sheet = TextureAtlasLayout::new_empty(UVec2::ZERO);
/// let sheet_handle = self.sheets.add(sheet); /// let sheet_handle = self.sheets.add(sheet);
/// ///
/// self.storage /// self.storage
......
...@@ -29,7 +29,7 @@ fn event_system_correctly_generates_and_dispatches_events() { ...@@ -29,7 +29,7 @@ fn event_system_correctly_generates_and_dispatches_events() {
); );
dispatch_my_events( dispatch_my_events(
&mut app.world, app.world_mut(),
MyEvents::Wait(WaitEvent { MyEvents::Wait(WaitEvent {
source: Entity::from_raw(0), source: Entity::from_raw(0),
}), }),
...@@ -37,5 +37,5 @@ fn event_system_correctly_generates_and_dispatches_events() { ...@@ -37,5 +37,5 @@ fn event_system_correctly_generates_and_dispatches_events() {
app.update(); app.update();
assert!(app.world.resource::<HasRun>().0); assert!(app.world().resource::<HasRun>().0);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment