use crate::*;
pub type AnimationName = &'static str;
#[derive(Clone, Debug, Copy)]
pub enum AnimationDirectionIndexes {
IndexBased(IndexBasedDirection),
FlipBased(FlipBasedDirection),
FX(FXBasedDirection),
}
impl AnimationDirectionIndexes {
pub fn one_directional() -> Self {
Self::IndexBased(IndexBasedDirection {
left: 1,
right: 1,
up: 1,
down: 1,
})
}
}
impl Default for AnimationDirectionIndexes {
fn default() -> Self {
Self::IndexBased(IndexBasedDirection {
left: 1,
right: 1,
up: 1,
down: 1,
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct FXBasedDirection {
pub index: usize,
}
#[derive(Debug, Clone, Copy)]
pub struct FlipBasedDirection {
pub left_direction_is_flipped: bool,
pub x_direction_index: usize,
}
#[derive(Debug, Clone, Copy)]
pub struct IndexBasedDirection {
pub left: usize,
pub right: usize,
pub up: usize,
pub down: usize,
}
pub enum YIndex {
Index(usize),
Flip(bool, usize),
}
#[derive(Debug, Component, Clone, Default)]
pub struct Animation {
pub handles: Handles,
pub animation: Arc<Mutex<AnimationType>>,
}
#[derive(Debug, Clone, Default)]
pub struct Handles {
image: Handle<Image>,
layout: Handle<TextureAtlasLayout>
}
impl Handles {
pub fn new(image: Handle<Image>, layout: Handle<TextureAtlasLayout>) -> Self {
Self { image, layout }
}
pub fn image(&self) -> Handle<Image> {
self.image.clone()
}
pub fn layout(&self) -> Handle<TextureAtlasLayout> {
self.layout.clone()
}
}
#[derive(Default, Resource, Debug)]
pub struct NewAnimation {
pub handles: Handles,
pub animation: AnimationType,
}
#[derive(Debug, Component, Clone, Default)]
pub enum AnimationType {
Timed(TimedAnimation, AnimationName),
Transform(TransformAnimation, AnimationName),
LinearTimed(LinearTimedAnimation, AnimationName),
LinearTransform(LinearTransformAnimation, AnimationName),
SingleFrame(SingleFrameAnimation, AnimationName),
#[default]
None,
}
impl AnimationType {
pub fn timed_animation(&mut self) -> Option<&mut TimedAnimation> {
match self {
AnimationType::Timed(timed_animation, _) => Some(timed_animation),
_ => None,
}
}
pub fn linear_timed_animation(&mut self) -> Option<&mut LinearTimedAnimation> {
match self {
AnimationType::LinearTimed(linear_timed_animation, _) => Some(linear_timed_animation),
_ => None,
}
}
pub fn linear_transform_animation(&mut self) -> Option<&mut LinearTransformAnimation> {
match self {
AnimationType::LinearTransform(linear_transform_animation, _) => {
Some(linear_transform_animation)
}
_ => None,
}
}
pub fn transform_animation(&mut self) -> Option<&mut TransformAnimation> {
match self {
AnimationType::Transform(transform_animation, _) => Some(transform_animation),
_ => None,
}
}
pub fn single_frame_animation(&mut self) -> Option<&mut SingleFrameAnimation> {
match self {
AnimationType::SingleFrame(single_frame_animation, _) => Some(single_frame_animation),
_ => None,
}
}
pub fn get_name(&self) -> AnimationName {
match self {
AnimationType::Timed(_, name) => name,
AnimationType::Transform(_, name) => name,
AnimationType::LinearTimed(_, name) => name,
AnimationType::LinearTransform(_, name) => name,
AnimationType::SingleFrame(_, name) => name,
AnimationType::None => panic!("Something went terribly wrong"),
}
}
pub fn reset_animation(&mut self) {
match self {
AnimationType::Timed(animation, _) => animation.reset_animation(None, None, None),
AnimationType::Transform(animation, _) => animation.reset_animation(None, None, None),
AnimationType::LinearTimed(animation, _) => animation.reset_animation(None),
AnimationType::LinearTransform(animation, _) => animation.reset_animation(None),
AnimationType::SingleFrame(animation, _) => animation.reset_animation(None, None),
AnimationType::None => panic!("Something went terribly wrong"),
}
}
pub fn is_none(&self) -> bool {
matches!(self, AnimationType::None)
}
}
#[derive(Debug, Event)]
pub struct AnimationEvent(pub AnimationName, pub Entity);
#[derive(Debug, Event)]
pub struct ResetAnimationEvent(pub Entity);
#[derive(Debug, Event)]
pub struct FXAnimationEvent(pub AnimationName, pub Vec3);
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub enum AnimationDirection {
#[default]
Still,
Left,
Right,
Up,
Down,
}
impl AnimationDirection {
const LEFT: Vec2 = Vec2::new(-1., 0.);
const RIGHT: Vec2 = Vec2::new(1., 0.);
const UP: Vec2 = Vec2::new(0., 1.);
const DOWN: Vec2 = Vec2::new(0., -1.);
const STILL: Vec2 = Vec2::new(0., 0.);
pub fn get_direction(direction: &Self) -> Vec2 {
match direction {
AnimationDirection::Left => AnimationDirection::LEFT,
AnimationDirection::Right => AnimationDirection::RIGHT,
AnimationDirection::Up => AnimationDirection::UP,
AnimationDirection::Down => AnimationDirection::DOWN,
AnimationDirection::Still => AnimationDirection::STILL,
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Default, Component)]
pub struct Animator {
pub direction: AnimationDirection,
}
impl Animator {
pub fn change_direction(&mut self, direction: AnimationDirection) -> &mut Self {
self.direction = direction;
self
}
pub fn get_direction(&self) -> &AnimationDirection {
&self.direction
}
}