use super::{Sprite, TextAlign2D};
use crate::core::{CanDraw, Modifier, Vec2D};
pub struct AnimatedSprite {
pub pos: Vec2D,
pub frames: Vec<String>,
current_frame: usize,
pub modifier: Modifier,
pub align: TextAlign2D,
}
impl AnimatedSprite {
#[must_use]
pub fn new(pos: Vec2D, frames: &[&str], modifier: Modifier) -> Self {
let processed_frames: Vec<String> = frames
.iter()
.map(|frame| frame.trim_start_matches('\n').into())
.collect();
Self {
pos,
frames: processed_frames,
current_frame: 0,
modifier,
align: TextAlign2D::default(),
}
}
#[must_use]
pub const fn get_current_frame(&self) -> usize {
self.current_frame
}
pub fn set_current_frame(&mut self, value: usize) {
self.current_frame = value;
self.current_frame = self.current_frame.rem_euclid(self.frames.len());
}
pub fn next_frame(&mut self) {
self.current_frame += 1;
if self.current_frame >= self.frames.len() {
self.current_frame = 0;
}
}
}
impl CanDraw for AnimatedSprite {
fn draw_to(&self, canvas: &mut impl crate::core::Canvas) {
Sprite::new(self.pos, &self.frames[self.current_frame], self.modifier)
.with_align(self.align)
.draw_to(canvas);
}
}