use std::collections::{HashMap, VecDeque};
use lotus_proc_macros::Component;
use super::super::{
texture::sprite_sheet::SpriteSheet,
animation::animation_state::AnimationState
};
#[derive(Clone, Component)]
pub struct Animation {
pub sprite_sheets: HashMap<String, SpriteSheet>,
pub playing_stack: VecDeque<String>
}
impl Animation {
pub fn new(sprite_sheets: HashMap<String, SpriteSheet>) -> Self {
return Self {
sprite_sheets,
playing_stack: VecDeque::new()
};
}
pub fn add_sprite_sheet(&mut self, title: String, sprite_sheet: SpriteSheet) {
self.sprite_sheets.insert(title, sprite_sheet);
}
pub fn add_sprite_sheets(&mut self, sprite_sheets: HashMap<String, SpriteSheet>) {
self.sprite_sheets.extend(sprite_sheets);
}
pub fn get_sprite_sheet(&self, title: String) -> Option<&SpriteSheet> {
return self.sprite_sheets.get(&title);
}
pub fn get_sprite_sheet_as_mut(&mut self, title: String) -> Option<&mut SpriteSheet> {
return self.sprite_sheets.get_mut(&title);
}
pub fn get_playing_animation_now(&self) -> Option<&SpriteSheet> {
for title in &self.playing_stack {
if let Some(sprite_sheet) = self.sprite_sheets.get(title) {
if sprite_sheet.animation_state == AnimationState::Playing {
return Some(sprite_sheet);
}
}
}
return None;
}
pub fn play(&mut self, title: String) {
if
self.get_playing_animation_now().is_some() &&
self.playing_stack.front().map(|t| t == &title).unwrap_or(false)
{
return;
}
if let Some(sprite_sheet) = self.sprite_sheets.get_mut(&title) {
sprite_sheet.play();
self.playing_stack.retain(|t| t != &title);
self.playing_stack.push_front(title);
}
}
pub fn stop(&mut self, title: String) {
if let Some(sprite_sheet) = self.sprite_sheets.get_mut(&title) {
sprite_sheet.stop();
self.playing_stack.retain(|t| t != &title);
}
}
pub fn pause(&mut self, title: String) {
if let Some(sprite_sheet) = self.sprite_sheets.get_mut(&title) {
sprite_sheet.pause();
}
}
pub fn resume(&mut self, title: String) {
if let Some(sprite_sheet) = self.sprite_sheets.get_mut(&title) {
sprite_sheet.resume();
}
}
}
impl Default for Animation {
fn default() -> Self {
return Self {
sprite_sheets: HashMap::new(),
playing_stack: VecDeque::new()
};
}
}