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 lyra_ecs::query::filter::Or;
use lyra_ecs::query::{Entities, Res, View};
use lyra_ecs::{Commands, Component};
use lyra_ecs::{Commands, Component, Entity};
use lyra_math::URect;
use lyra_reflect::Reflect;
use lyra_resource::{ResHandle, ResourceStorage};
@ -38,7 +40,7 @@ impl SpriteAnimation {
where
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 {
let r = atlas.index_rect(i);
@ -81,15 +83,16 @@ pub struct AtlasAnimations {
}
impl AtlasAnimations {
pub fn from_animations(atlas: ResHandle<TextureAtlas>, animations: Vec<SpriteAnimation>) -> Self {
let animations = animations.into_iter()
pub fn from_animations(
atlas: ResHandle<TextureAtlas>,
animations: Vec<SpriteAnimation>,
) -> Self {
let animations = animations
.into_iter()
.map(|a| (a.name.clone(), a))
.collect::<HashMap<_, _>>();
Self {
atlas,
animations,
}
Self { atlas, animations }
}
/// Helper for creating [`AtlasAnimations`].
@ -111,12 +114,13 @@ impl AtlasAnimations {
/// ```
pub fn new<A>(atlas: ResHandle<TextureAtlas>, animations: &[A]) -> Self
where
A: IntoSpriteAnimation
A: IntoSpriteAnimation,
{
let animations = {
let atlas = atlas.data_ref().unwrap();
animations.into_iter()
animations
.into_iter()
.map(|a| {
let a = a.into_animation(&atlas);
(a.name.clone(), a)
@ -125,22 +129,36 @@ impl AtlasAnimations {
.collect::<HashMap<_, _>>()
};
Self {
atlas,
animations,
}
Self { atlas, animations }
}
/// Get the [`ActiveAtlasAnimation`] for an animation with `name`.
///
/// > NOTE: this asserts that the animation exists in self in debug builds (uses `debug_assert`).
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)
}
}
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`].
#[derive(Clone, Component, Reflect)]
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;
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;
}
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 animations.atlas.is_loaded() {
active.timer += dt;
@ -206,7 +269,7 @@ pub fn system_sprite_atlas_animation(mut commands: Commands, dt: Res<DeltaTime>,
};
commands.insert(en, sprite);
continue;
return;
}
if active.timer >= anim.frame_time {
@ -232,7 +295,4 @@ pub fn system_sprite_atlas_animation(mut commands: Commands, dt: Res<DeltaTime>,
} else {
error!("Unknown active animation: '{}'", active.name);
}
}
Ok(())
}

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