Format some code, update wgsl-preprocessor crate

This commit is contained in:
SeanOMik 2024-11-24 16:32:52 -05:00
parent cb4cf9a48f
commit b9d0398157
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
2 changed files with 131 additions and 71 deletions

View File

@ -1,7 +1,9 @@
use std::cell::RefMut;
use std::collections::HashMap; use std::collections::HashMap;
use lyra_ecs::query::filter::Or;
use lyra_ecs::query::{Entities, Res, View}; use lyra_ecs::query::{Entities, Res, View};
use lyra_ecs::{Commands, Component}; use lyra_ecs::{Commands, Component, Entity};
use lyra_math::URect; use lyra_math::URect;
use lyra_reflect::Reflect; use lyra_reflect::Reflect;
use lyra_resource::{ResHandle, ResourceStorage}; use lyra_resource::{ResHandle, ResourceStorage};
@ -38,7 +40,7 @@ impl SpriteAnimation {
where where
I: Iterator<Item = u32>, I: Iterator<Item = u32>,
{ {
let mut frames = vec![];//Vec::with_capacity(sprites.len()); let mut frames = vec![]; //Vec::with_capacity(sprites.len());
for i in sprites { for i in sprites {
let r = atlas.index_rect(i); let r = atlas.index_rect(i);
@ -81,15 +83,16 @@ pub struct AtlasAnimations {
} }
impl AtlasAnimations { impl AtlasAnimations {
pub fn from_animations(atlas: ResHandle<TextureAtlas>, animations: Vec<SpriteAnimation>) -> Self { pub fn from_animations(
let animations = animations.into_iter() atlas: ResHandle<TextureAtlas>,
animations: Vec<SpriteAnimation>,
) -> Self {
let animations = animations
.into_iter()
.map(|a| (a.name.clone(), a)) .map(|a| (a.name.clone(), a))
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
Self { Self { atlas, animations }
atlas,
animations,
}
} }
/// Helper for creating [`AtlasAnimations`]. /// Helper for creating [`AtlasAnimations`].
@ -111,12 +114,13 @@ impl AtlasAnimations {
/// ``` /// ```
pub fn new<A>(atlas: ResHandle<TextureAtlas>, animations: &[A]) -> Self pub fn new<A>(atlas: ResHandle<TextureAtlas>, animations: &[A]) -> Self
where where
A: IntoSpriteAnimation A: IntoSpriteAnimation,
{ {
let animations = { let animations = {
let atlas = atlas.data_ref().unwrap(); let atlas = atlas.data_ref().unwrap();
animations.into_iter() animations
.into_iter()
.map(|a| { .map(|a| {
let a = a.into_animation(&atlas); let a = a.into_animation(&atlas);
(a.name.clone(), a) (a.name.clone(), a)
@ -125,22 +129,36 @@ impl AtlasAnimations {
.collect::<HashMap<_, _>>() .collect::<HashMap<_, _>>()
}; };
Self { Self { atlas, animations }
atlas,
animations,
}
} }
/// Get the [`ActiveAtlasAnimation`] for an animation with `name`. /// Get the [`ActiveAtlasAnimation`] for an animation with `name`.
/// ///
/// > NOTE: this asserts that the animation exists in self in debug builds (uses `debug_assert`). /// > NOTE: this asserts that the animation exists in self in debug builds (uses `debug_assert`).
pub fn get_active(&self, name: &str) -> ActiveAtlasAnimation { pub fn get_active(&self, name: &str) -> ActiveAtlasAnimation {
debug_assert!(self.animations.contains_key(name), "The animation with name '{name}' does not exist!"); debug_assert!(
self.animations.contains_key(name),
"The animation with name '{name}' does not exist!"
);
ActiveAtlasAnimation::new(name) ActiveAtlasAnimation::new(name)
} }
} }
impl lyra_resource::ResourceData for AtlasAnimations {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn dependencies(&self) -> Vec<lyra_resource::UntypedResHandle> {
vec![self.atlas.untyped_clone()]
}
}
/// The active sprite animation from an [`AtlasAnimations`]. /// The active sprite animation from an [`AtlasAnimations`].
#[derive(Clone, Component, Reflect)] #[derive(Clone, Component, Reflect)]
pub struct ActiveAtlasAnimation { pub struct ActiveAtlasAnimation {
@ -183,7 +201,17 @@ impl ActiveAtlasAnimation {
} }
} }
pub fn system_sprite_atlas_animation(mut commands: Commands, dt: Res<DeltaTime>, view: View<(Entities, Option<&mut AtlasSprite>, &AtlasAnimations, &mut ActiveAtlasAnimation)>) -> anyhow::Result<()> { pub fn system_sprite_atlas_animation(
mut commands: Commands,
dt: Res<DeltaTime>,
view: View<(
Entities,
Option<&mut AtlasSprite>,
// support animations from assets or non-asset handles.
Or<&AtlasAnimations, &ResHandle<AtlasAnimations>>,
&mut ActiveAtlasAnimation,
)>,
) -> anyhow::Result<()> {
let dt = **dt; let dt = **dt;
for (en, mut sprite, animations, mut active) in view.iter() { for (en, mut sprite, animations, mut active) in view.iter() {
@ -192,6 +220,41 @@ pub fn system_sprite_atlas_animation(mut commands: Commands, dt: Res<DeltaTime>,
continue; continue;
} }
if let Some(animations) = animations.0 {
system_animation_entity_impl(
&mut commands,
dt,
en,
&mut sprite,
&animations,
&mut active,
);
} else {
let animations = animations.1.unwrap();
if let Some(animations) = animations.data_ref() {
system_animation_entity_impl(
&mut commands,
dt,
en,
&mut sprite,
&animations,
&mut active,
);
};
}
}
Ok(())
}
fn system_animation_entity_impl(
commands: &mut Commands,
dt: f32,
en: Entity,
sprite: &mut Option<RefMut<AtlasSprite>>,
animations: &AtlasAnimations,
active: &mut ActiveAtlasAnimation,
) {
if let Some(anim) = animations.animations.get(&active.name) { if let Some(anim) = animations.animations.get(&active.name) {
if animations.atlas.is_loaded() { if animations.atlas.is_loaded() {
active.timer += dt; active.timer += dt;
@ -206,7 +269,7 @@ pub fn system_sprite_atlas_animation(mut commands: Commands, dt: Res<DeltaTime>,
}; };
commands.insert(en, sprite); commands.insert(en, sprite);
continue; return;
} }
if active.timer >= anim.frame_time { if active.timer >= anim.frame_time {
@ -232,7 +295,4 @@ pub fn system_sprite_atlas_animation(mut commands: Commands, dt: Res<DeltaTime>,
} else { } else {
error!("Unknown active animation: '{}'", active.name); error!("Unknown active animation: '{}'", active.name);
} }
}
Ok(())
} }

@ -1 +1 @@
Subproject commit 70daf320827f64b325a77718df07177d74d7ea58 Subproject commit 76a1c98abd375dbaca694f9728e3d6c105ef3f1b