RpackAtlases system param

This commit is contained in:
Piotr Siuszko 2025-01-27 17:30:52 +01:00
parent 71908c659b
commit d2dfeea6c2
6 changed files with 30 additions and 8 deletions

View File

@ -0,0 +1,17 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.1.1] - 2025-01-27
### Added
- `RpackAtlases` system param.
## [0.1.0] - 2025-01-14
- initial version

View File

@ -1,7 +1,7 @@
[package] [package]
name = "bevy_rpack" name = "bevy_rpack"
description = "Bevy plugin with rpack atlas support" description = "Bevy plugin with rpack atlas support"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
repository = "https://github.com/Leinnan/rpack.git" repository = "https://github.com/Leinnan/rpack.git"
homepage = "https://github.com/Leinnan/rpack" homepage = "https://github.com/Leinnan/rpack"

View File

@ -22,21 +22,21 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn on_loaded( fn on_loaded(
mut ev_asset: EventReader<AssetEvent<RpackAtlasAsset>>, mut ev_asset: EventReader<AssetEvent<RpackAtlasAsset>>,
assets: Res<Assets<RpackAtlasAsset>>, atlases: RpackAtlases,
mut commands: Commands, mut commands: Commands,
) { ) {
for ev in ev_asset.read() { for ev in ev_asset.read() {
let AssetEvent::LoadedWithDependencies { id: _ } = ev else { if !matches!(ev, AssetEvent::LoadedWithDependencies { id: _ }) {
continue; continue;
}; }
if let Ok(sprite) = assets.try_make_sprite("agents/spaceAstronauts_005") { if let Ok(sprite) = atlases.try_make_sprite("agents/spaceAstronauts_005") {
commands.spawn(Sprite { commands.spawn(Sprite {
color: Color::linear_rgb(1.0, 0.0, 0.0), color: Color::linear_rgb(1.0, 0.0, 0.0),
..sprite ..sprite
}); });
}; };
if let Ok(image_node) = assets.try_make_image_node("agents/spaceShips_006") { if let Ok(image_node) = atlases.try_make_image_node("agents/spaceShips_006") {
commands.spawn(image_node); commands.spawn(image_node);
} }
} }

View File

@ -10,7 +10,7 @@ pub mod prelude {
/// Provides easy access to `Rpack` asset-related functionality in a Bevy application. /// Provides easy access to `Rpack` asset-related functionality in a Bevy application.
pub use super::plugin::{ pub use super::plugin::{
RpackAssetHelper, RpackAssetPlugin, RpackAtlasAsset, RpackAtlasAssetError, RpackAssetHelper, RpackAssetPlugin, RpackAtlasAsset, RpackAtlasAssetError,
RpackAtlasAssetLoader, RpackAtlasError, RpackAtlasAssetLoader, RpackAtlasError, RpackAtlases,
}; };
/// Re-exports core types for working with texture atlases. /// Re-exports core types for working with texture atlases.
pub use super::{AtlasAsset, AtlasFrame, SerializableRect}; pub use super::{AtlasAsset, AtlasFrame, SerializableRect};

View File

@ -1,5 +1,6 @@
use crate::{AtlasAsset, SerializableRect}; use crate::{AtlasAsset, SerializableRect};
use bevy::asset::{AssetLoader, AsyncReadExt}; use bevy::asset::{AssetLoader, AsyncReadExt};
use bevy::ecs::system::SystemParam;
use bevy::image::ImageSampler; use bevy::image::ImageSampler;
use bevy::{prelude::*, utils::HashMap}; use bevy::{prelude::*, utils::HashMap};
use thiserror::Error; use thiserror::Error;
@ -38,6 +39,10 @@ impl From<SerializableRect> for URect {
} }
} }
/// SystemParam helper for accessing and creating components from `Rpack` atlas data.
#[derive(SystemParam, DerefMut, Deref)]
pub struct RpackAtlases<'w>(pub Res<'w, Assets<RpackAtlasAsset>>);
/// A helper trait for accessing and creating components from `Rpack` atlas data. /// A helper trait for accessing and creating components from `Rpack` atlas data.
#[allow(dead_code)] #[allow(dead_code)]
pub trait RpackAssetHelper { pub trait RpackAssetHelper {