|
use bevy::prelude::*;
|
|
|
|
use super::health::Health;
|
|
|
|
|
|
#[derive(Component, Clone)]
|
|
struct AnimationIndices {
|
|
first: usize,
|
|
last: usize,
|
|
current_index: usize,
|
|
}
|
|
|
|
#[derive(Component, Deref, DerefMut)]
|
|
struct AnimationTimer(Timer);
|
|
|
|
#[derive(Component)]
|
|
struct DeathAnimationPlayed(bool);
|
|
|
|
|
|
fn play_death_animation(
|
|
mut query: Query<(&mut AnimationIndices, &Health, &mut DeathAnimationPlayed, &mut TextureAtlas)>
|
|
) {
|
|
|
|
for (mut animation_indices, health, mut death_animation_played, mut atlas) in &mut query {
|
|
|
|
if health.current == 0 && !death_animation_played.0 {
|
|
|
|
animation_indices.first = 4;
|
|
animation_indices.last = 4;
|
|
animation_indices.current_index = 4;
|
|
|
|
atlas.index = animation_indices.current_index;
|
|
|
|
death_animation_played.0 = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
fn animate_sprite<T: Component>(
|
|
time: Res<Time>,
|
|
mut query: Query<(&mut AnimationIndices, &mut AnimationTimer, &mut TextureAtlas), With<T>>
|
|
) {
|
|
|
|
for (mut indices, mut timer, mut atlas) in &mut query {
|
|
|
|
timer.tick(time.delta());
|
|
|
|
if timer.just_finished() {
|
|
|
|
indices.current_index = if indices.current_index == indices.last {
|
|
if indices.first == 4 {
|
|
|
|
4
|
|
} else {
|
|
|
|
indices.first
|
|
}
|
|
} else {
|
|
|
|
indices.current_index + 1
|
|
};
|
|
|
|
atlas.index = indices.current_index;
|
|
}
|
|
}
|
|
}
|
|
|