This commit is contained in:
2025-03-27 13:44:13 +08:00
parent 82ce62f24a
commit f3358b9511
4 changed files with 43 additions and 28 deletions

View File

@@ -32,6 +32,7 @@ use bevy_trenchbroom::prelude::*;
use control::controller::CharacterControllerPlugin; use control::controller::CharacterControllerPlugin;
use loading_assets::AudioAssets; use loading_assets::AudioAssets;
use utils::billboards; use utils::billboards;
use utils::sprite_3d_animation;
use utils::squish_animation; use utils::squish_animation;
#[derive(Resource, Reflect, Debug)] #[derive(Resource, Reflect, Debug)]
@@ -112,6 +113,7 @@ fn main() {
app.add_plugins(backpack::plugin); app.add_plugins(backpack::plugin);
app.add_plugins(loading_assets::LoadingPlugin); app.add_plugins(loading_assets::LoadingPlugin);
app.add_plugins(loading_map::plugin); app.add_plugins(loading_map::plugin);
app.add_plugins(sprite_3d_animation::plugin);
app.init_state::<GameState>(); app.init_state::<GameState>();

View File

@@ -8,13 +8,14 @@ use crate::{
player::{Player, PlayerRig}, player::{Player, PlayerRig},
sounds::PlaySound, sounds::PlaySound,
tb_entities::EnemySpawn, tb_entities::EnemySpawn,
utils::sprite_3d_animation::AnimationTimer,
}; };
use avian3d::prelude::{ use avian3d::prelude::{
Collider, CollisionLayers, CollisionStarted, LayerMask, PhysicsLayer, Sensor, Collider, CollisionLayers, CollisionStarted, LayerMask, PhysicsLayer, Sensor,
}; };
use bevy::{pbr::NotShadowCaster, prelude::*}; use bevy::{pbr::NotShadowCaster, prelude::*};
use bevy_polyline::prelude::*; use bevy_polyline::prelude::*;
use bevy_sprite3d::{Sprite3d, Sprite3dBuilder, Sprite3dParams}; use bevy_sprite3d::{Sprite3dBuilder, Sprite3dParams};
use std::f32::consts::PI; use std::f32::consts::PI;
#[derive(Component)] #[derive(Component)]
@@ -36,16 +37,11 @@ struct ShotAssets {
layout: Handle<TextureAtlasLayout>, layout: Handle<TextureAtlasLayout>,
} }
//TODO: give its own plugin
#[derive(Component, Deref, DerefMut)]
struct AnimationTimer(Timer);
pub fn plugin(app: &mut App) { pub fn plugin(app: &mut App) {
app.add_systems(OnEnter(GameState::Playing), setup); app.add_systems(OnEnter(GameState::Playing), setup);
app.add_systems( app.add_systems(
Update, Update,
(update, shot_collision, enemy_hit, timeout, animate_sprite) (update, shot_collision, enemy_hit, timeout).run_if(in_state(GameState::Playing)),
.run_if(in_state(GameState::Playing)),
); );
app.add_observer(on_trigger_state); app.add_observer(on_trigger_state);
} }
@@ -155,26 +151,6 @@ fn enemy_hit(
} }
} }
fn animate_sprite(
mut commands: Commands,
time: Res<Time>,
mut query: Query<(Entity, &mut AnimationTimer, &mut Sprite3d)>,
) {
for (e, mut timer, mut sprite_3d) in query.iter_mut() {
timer.tick(time.delta());
if timer.just_finished() {
let length = sprite_3d.texture_atlas_keys.as_ref().unwrap().len();
let atlas = sprite_3d.texture_atlas.as_mut().unwrap();
if atlas.index < length - 1 {
atlas.index = atlas.index.saturating_add(1) % length;
} else {
commands.entity(e).despawn_recursive();
}
}
}
}
fn shot_collision( fn shot_collision(
mut commands: Commands, mut commands: Commands,
mut collision_event_reader: EventReader<CollisionStarted>, mut collision_event_reader: EventReader<CollisionStarted>,
@@ -219,7 +195,7 @@ fn shot_collision(
Billboard, Billboard,
Transform::from_translation(shot_pos), Transform::from_translation(shot_pos),
NotShadowCaster, NotShadowCaster,
AnimationTimer(Timer::from_seconds(0.01, TimerMode::Repeating)), AnimationTimer::new(Timer::from_seconds(0.01, TimerMode::Repeating)),
)); ));
} }
} }

View File

@@ -1,2 +1,3 @@
pub mod billboards; pub mod billboards;
pub mod sprite_3d_animation;
pub mod squish_animation; pub mod squish_animation;

View File

@@ -0,0 +1,36 @@
use bevy::prelude::*;
use bevy_sprite3d::Sprite3d;
#[derive(Component, Reflect, Deref, DerefMut)]
#[reflect(Component)]
pub struct AnimationTimer(Timer);
impl AnimationTimer {
pub fn new(t: Timer) -> Self {
Self(t)
}
}
pub fn plugin(app: &mut App) {
app.add_systems(Update, animate_sprite);
}
fn animate_sprite(
mut commands: Commands,
time: Res<Time>,
mut query: Query<(Entity, &mut AnimationTimer, &mut Sprite3d)>,
) {
for (e, mut timer, mut sprite_3d) in query.iter_mut() {
timer.tick(time.delta());
if timer.just_finished() {
let length = sprite_3d.texture_atlas_keys.as_ref().unwrap().len();
let atlas = sprite_3d.texture_atlas.as_mut().unwrap();
if atlas.index < length - 1 {
atlas.index = atlas.index.saturating_add(1) % length;
} else {
commands.entity(e).despawn_recursive();
}
}
}
}