From f6f10087606bf54fa35846462feb2579b794b4b8 Mon Sep 17 00:00:00 2001
From: Louis Capitanchik <contact@louiscap.co>
Date: Sun, 1 Sep 2024 05:39:55 +0100
Subject: [PATCH] Fix missing system for directional override animations

---
 CHANGELOG.md   | 28 +++++++++++++++++++++----
 Cargo.toml     |  4 ++--
 src/query.rs   | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/systems.rs |  7 ++++---
 4 files changed, 85 insertions(+), 9 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6e2a32b..8336f4e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,14 +1,29 @@
 # Changelog
+
 All notable changes to this project will be documented in this file.
 
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [0.9.2]
+
+### Fixed
+
+- Directional override animations were being skipped by the override animation filters
+- Relational sync was being scheduled in the same set as animation ticks, potentially causing 1-frame desync based on
+  Bevy scheduling
+
+## [0.9.0]
+
+### Changed
+
+- Required Bevy version is now 0.14
+
 ## [0.8.0]
 
 ### Changed
 
-- Required Bevy version is not 0.13
+- Required Bevy version is now 0.13
 
 ## [0.7.0]
 
@@ -23,10 +38,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Removed
 
-- `AnimationQuery` has been removed; instead, the ergonomics of controlling animations by manipulating components 
-directly is much more ergonomic
+- `AnimationQuery` has been removed; instead, the ergonomics of controlling animations by manipulating components
+  directly is much more ergonomic
 - Support for `bevy_ecs_tilemap` has been dropped
-- 
+-
 
 ## [0.6.0]
 
@@ -45,19 +60,23 @@ directly is much more ergonomic
 ## [0.3.1]
 
 ### Fixed
+
 - Child animations would not play correct animation when parent has an override
 
 ## [0.2.1]
 
 ### Fixed
+
 - Action animation frames overflow
 
 ## [0.2.0]
 
 ### Added
+
 - Constructor functions for `AnimationOverride`
 
 ### Changed
+
 - `bevy` version `0.9`
 - `bevy_ecs_tilemap` version `0.9`
 - `AnimationOverride` now contains its own state
@@ -69,4 +88,5 @@ directly is much more ergonomic
 ## [0.1.1]
 
 ### Fixed
+
 - "play once and then" animations could overflow frame array bounds
\ No newline at end of file
diff --git a/Cargo.toml b/Cargo.toml
index 737343d..0873e26 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,11 +1,11 @@
 [package]
 name = "micro_banimate"
-version = "0.9.1"
+version = "0.9.2"
 edition = "2021"
 license = "Apache-2.0"
 description = "Easily manage complex Bevy 2D sprite animations"
 authors = [
-    "Louis Capitanchik <louis@microhacks.co.uk>"
+	"Louis Capitanchik <louis@microhacks.co.uk>"
 ]
 repository = "https://lab.lcr.gr/microhacks/micro-banimate"
 
diff --git a/src/query.rs b/src/query.rs
index 3898590..aca0e35 100644
--- a/src/query.rs
+++ b/src/query.rs
@@ -37,6 +37,16 @@ pub struct OverrideAnimationComponents {
 	atlas: &'static mut TextureAtlas,
 }
 
+#[derive(QueryData)]
+#[query_data(mutable)]
+pub struct DirectionalOverrideAnimationComponents {
+	handle: &'static Handle<AnimationSet>,
+	direction: &'static Directionality,
+	data: Option<&'static OverrideData>,
+	status: &'static mut AnimationOverride,
+	atlas: &'static mut TextureAtlas,
+}
+
 #[derive(QueryData)]
 #[query_data(mutable)]
 pub struct SimpleAnimationComponents {
@@ -193,6 +203,51 @@ pub fn play_override_animation(
 	}
 }
 
+pub fn play_directional_override_animation(
+	time: Res<Time>,
+	mut commands: Commands,
+	mut anim_query: Query<
+		(Entity, DirectionalOverrideAnimationComponents),
+		OnlyDirectionalOverrideAnimations,
+	>,
+	animations: Res<Assets<AnimationSet>>,
+	mut events: EventWriter<AnimationCompleted>,
+) {
+	let delta = time.delta();
+	for (
+		entity,
+		DirectionalOverrideAnimationComponentsItem {
+			mut status,
+			direction,
+			handle,
+			mut atlas,
+			data,
+		},
+	) in &mut anim_query
+	{
+		let anim = get_current_anim!(
+			animations,
+			handle,
+			format!("{}_{}", status.active_name, direction),
+			status.active_name
+		);
+
+		let looped = tick_animation!(delta, anim, status, atlas);
+		if looped {
+			commands
+				.entity(entity)
+				.remove::<(AnimationOverride, OverrideData)>();
+
+			if let Some(data) = data {
+				events.send(AnimationCompleted {
+					entity,
+					user_data: **data,
+				});
+			}
+		}
+	}
+}
+
 pub fn play_simple_animation(
 	time: Res<Time>,
 	mut anim_query: Query<SimpleAnimationComponents, Without<AnimationPaused>>,
diff --git a/src/systems.rs b/src/systems.rs
index f9fbe1b..a5cffa3 100644
--- a/src/systems.rs
+++ b/src/systems.rs
@@ -1,6 +1,6 @@
 use crate::query::{
-	play_animations, play_directional_animations, play_override_animation, play_simple_animation,
-	sync_child_animation,
+	play_animations, play_directional_animations, play_directional_override_animation,
+	play_override_animation, play_simple_animation, sync_child_animation,
 };
 use bevy::prelude::*;
 
@@ -34,9 +34,10 @@ impl Plugin for AnimationSystemsPlugin {
 				play_animations,
 				play_override_animation,
 				play_directional_animations,
+				play_directional_override_animation,
 				play_simple_animation,
 			)
-				.in_set(AnimationSystems::SyncAnimations),
+				.in_set(AnimationSystems::TickAnimations),
 		)
 		.add_systems(
 			PostUpdate,
-- 
GitLab