Skip to content

Commit

Permalink
Merge pull request #36 from Byteron/animation_loop_fix
Browse files Browse the repository at this point in the history
Animation Loop
  • Loading branch information
odecay authored Jul 1, 2022
2 parents e1a98d3 + 5d0b56d commit 08b1300
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 30 deletions.
20 changes: 14 additions & 6 deletions assets/fighters/bandit/bandit.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ spritesheet:

animation_fps: 0.12
animations:
idle: [0, 7]
running: [8, 15]
knocked_right: [40, 46]
knocked_left: [40, 46]
dying: [40, 46]
attacking: [16, 23]
idle:
frames: [0, 7]
repeat: true
running:
frames: [8, 15]
repeat: true
knocked_right:
frames: [40, 46]
knocked_left:
frames: [40, 46]
dying:
frames: [40, 46]
attacking:
frames: [16, 23]
20 changes: 14 additions & 6 deletions assets/fighters/fishy/fishy.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ spritesheet:

animation_fps: 0.12
animations:
idle: [0, 13]
running: [14, 19]
knocked_right: [85, 90]
knocked_left: [71, 76]
dying: [71, 76]
attacking: [85, 90]
idle:
frames: [0, 13]
repeat: true
running:
frames: [14, 19]
repeat: true
knocked_right:
frames: [85, 90]
knocked_left:
frames: [71, 76]
dying:
frames: [71, 76]
attacking:
frames: [85, 90]
20 changes: 14 additions & 6 deletions assets/fighters/sharky/sharky.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ spritesheet:

animation_fps: 0.12
animations:
idle: [0, 13]
running: [14, 19]
knocked_right: [85, 90]
knocked_left: [71, 76]
dying: [71, 76]
attacking: [85, 90]
idle:
frames: [0, 13]
repeat: true
running:
frames: [14, 19]
repeat: true
knocked_right:
frames: [85, 90]
knocked_left:
frames: [71, 76]
dying:
frames: [71, 76]
attacking:
frames: [85, 90]
20 changes: 14 additions & 6 deletions assets/fighters/slinger/slinger.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ spritesheet:

animation_fps: 0.12
animations:
idle: [0, 3]
running: [8, 11]
knocked_right: [40, 46]
knocked_left: [40, 46]
dying: [40, 46]
attacking: [16, 20]
idle:
frames: [0, 3]
repeat: true
running:
frames: [8, 11]
repeat: true
knocked_right:
frames: [40, 46]
knocked_left:
frames: [40, 46]
dying:
frames: [40, 46]
attacking:
frames: [16, 20]
36 changes: 32 additions & 4 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@ impl Facing {
}
}

#[derive(serde::Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct Clip {
pub frames: Range<usize>,
#[serde(default)]
pub repeat: bool,
}

#[derive(Component)]
pub struct Animation {
pub animations: HashMap<State, Range<usize>>,
pub animations: HashMap<State, Clip>,
pub current_frame: usize,
current_state: Option<State>,
pub timer: Timer,
pub played_once: bool,
}

impl Animation {
pub fn new(fps: f32, animations: HashMap<State, Range<usize>>) -> Self {
pub fn new(fps: f32, animations: HashMap<State, Clip>) -> Self {
Self {
animations,
current_frame: 0,
Expand All @@ -66,6 +74,16 @@ impl Animation {
self.played_once
}

pub fn is_repeating(&self) -> bool {
if let Some(state) = self.current_state {
if let Some(clip) = self.animations.get(&state) {
return clip.repeat;
}
}

false
}

pub fn is_last_frame(&self) -> bool {
if let Some(indices) = self.get_current_indices() {
if let Some(index) = self.get_current_index() {
Expand All @@ -78,7 +96,10 @@ impl Animation {

pub fn get_current_indices(&self) -> Option<&Range<usize>> {
if let Some(state) = self.current_state {
return self.animations.get(&state);
match self.animations.get(&state) {
Some(clip) => return Some(&clip.frames),
None => return None,
}
}

None
Expand Down Expand Up @@ -107,14 +128,21 @@ fn animate_on_state_changed(mut query: Query<(&mut Animation, &State), Changed<S
fn animation_cycling(mut query: Query<(&mut TextureAtlasSprite, &mut Animation)>, time: Res<Time>) {
//TODO: Add a tick method on Animation
for (mut texture_atlas_sprite, mut animation) in query.iter_mut() {
if animation.is_finished() && !animation.is_repeating() {
return;
}

animation.timer.tick(time.delta());

if animation.timer.finished() {
animation.timer.reset();

if animation.is_last_frame() {
animation.played_once = true; // Check if animation player here because we need to wait the last frame
animation.current_frame = 0;

if animation.is_repeating() {
animation.current_frame = 0;
}
} else {
animation.current_frame += 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::{
use bevy_parallax::{LayerData, ParallaxResource};
use serde::Deserialize;

use crate::{state::State, Stats};
use crate::{animation::Clip, state::State, Stats};

#[derive(TypeUuid, Clone, Debug)]
#[uuid = "eb28180f-ef68-44a0-8479-a299a3cef66e"]
Expand Down Expand Up @@ -74,7 +74,7 @@ pub struct FighterSpritesheetMeta {
pub columns: usize,
pub rows: usize,
pub animation_fps: f32,
pub animations: HashMap<State, std::ops::Range<usize>>,
pub animations: HashMap<State, Clip>,
}

#[derive(Deserialize, Clone, Debug)]
Expand Down

0 comments on commit 08b1300

Please sign in to comment.