use bevy::prelude::*;
use crate::animation::Animation;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
#[reflect(Debug, Default, PartialEq, Hash)]
pub struct AnimationProgress {
pub frame: usize,
pub repetition: usize,
}
impl AnimationProgress {
pub fn with_frame(frame: usize) -> Self {
Self {
frame,
repetition: 0,
}
}
pub fn with_frame_repetition(frame: usize, repetition: usize) -> Self {
Self { frame, repetition }
}
}
#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component, Debug)]
pub struct SpritesheetAnimation {
pub animation: Handle<Animation>,
pub progress: AnimationProgress,
pub playing: bool,
pub speed_factor: f32,
}
impl SpritesheetAnimation {
pub fn new(animation: Handle<Animation>) -> Self {
Self {
animation,
progress: AnimationProgress {
frame: 0,
repetition: 0,
},
playing: true,
speed_factor: 1.0,
}
}
pub fn with_progress(mut self, progress: AnimationProgress) -> Self {
self.progress = progress;
self
}
pub fn with_playing(mut self, playing: bool) -> Self {
self.playing = playing;
self
}
pub fn with_speed_factor(mut self, speed_factor: f32) -> Self {
self.speed_factor = speed_factor;
self
}
pub fn play(&mut self) {
self.playing = true;
}
pub fn pause(&mut self) {
self.playing = false;
}
pub fn reset(&mut self) {
self.progress.frame = 0;
self.progress.repetition = 0;
}
pub fn switch(&mut self, animation: Handle<Animation>) {
self.animation = animation;
self.reset();
}
}