1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use bevy::prelude::*;

use crate::*;

/// A plugin for Bevy that adds support for animations.
/// You Can Add This Plugin To Your Bevy Game Like This
/// ```
/// use bevy_animations::AnimationsPlugin;
/// use bevy::prelude::*;
/// 
/// fn main() {
///     App::new()
///         .add_plugins(DefaultPlugins)
///         .add_plugins(AnimationsPlugin {
///             pixels_per_meter: 20. // your desired pixels_per_meter
///         })
///         .run()
/// }
/// ```
/// Note that the `pixels_per_meter` field will be used for your [`TransformAnimation`](crate::animations::TimedAnimation)
#[derive(Debug, Default)]
pub struct AnimationsPlugin {
    /// The number of pixels per meter.
    pub pixels_per_meter: f32,
}

impl Plugin for AnimationsPlugin {
    /// Builds the plugin.
    fn build(&self, app: &mut App) {
        app
            .insert_resource(AnimationsConfig {
                pixels_per_meter: self.pixels_per_meter,
            })
            .add_event::<AnimationEvent>()
            .add_event::<ResetAnimationEvent>()
            .add_event::<FXAnimationEvent>()
            .insert_resource(Animations::default())
            .insert_resource(EntitesToRemove::default())
            .add_systems(Update, (
                catch_fx_animation_events,
                catch_animation_events,
                catch_reset_events,
                remove_entites
            ).chain())
            ;
    }
}

/// Main System That Checks for Incoming events
/// If any incoming events are found they are checked to make sure they are new and if they are the Handle<TextureAtlas> is changed for the entity
/// After checking the events. All the [`AnimatingEntities`](crate::AnimatingEntities) have their current animations cycled
fn catch_animation_events(
    time: Res<Time>,
    mut query: Query<(
        &mut Handle<TextureAtlas>,
        &mut TextureAtlasSprite,
        &mut Transform,
        &AnimationDirection
    )>,
    mut animations: ResMut<Animations>,
    mut entities_to_remove: ResMut<EntitesToRemove>,
    config: Res<AnimationsConfig>,
    mut animation_events: EventReader<AnimationEvent>
) {
    // our main event loop
    for event in animation_events.read() {
        // get the animating entity from the entity passed in from our event
        let animating_entity = animations.entities.get_mut(&event.1).expect(format!("Entity Not Found in Map For {} animation make sure your adding every necessary component to the entity i.e `AnimationDirection`", event.0).as_str());
        // query the texture the sprite and the current direction of the entity
        let (mut texture_atlas, mut sprite, _, direction) = match query.get_mut(animating_entity.entity) {
            Ok(handle) => handle,
            Err(_) => {
                // if we didn't find the entity from the query it doesn't exist anymore and should be removed via the remove_entites system
                entities_to_remove.0.push(event.1);
                continue;
            }
        };
        // if incoming event is new 
        if animating_entity.curr_animation.lock().unwrap().get_name() != event.0 {
            // get the Arc pointer for the animation
            let new_animation_arc = animating_entity.animations.get(event.0).expect(format!("No Animation Found For `{}` make sure the name matches your configuration", event.0).as_str());
            // unlock the animation if we don't do this we will hit a deadlock whenever we try to unlock the Arc<Mutex<>>
            let mut new_animation = new_animation_arc.lock().unwrap();
            let mut blocking = false;
            let mut new_priority = 0;
            let mut sprite_index = 0;
            // get the temp variables above so we don't need to twice
            if let Some(new_timed_animation) = new_animation.timed_animation() {
                blocking = new_timed_animation.blocking;
                new_priority = new_timed_animation.blocking_priority;
                sprite_index = new_timed_animation.sprite_index(&animating_entity.last_valid_direction);
            }
            else if let Some(new_singe_frame_animation) = new_animation.single_frame_animation() {
                blocking = new_singe_frame_animation.blocking;
                new_priority = new_singe_frame_animation.blocking_priority;
            }
            // if the new animation isn't a timed or single_frame one we don't care about blocking or priority
            else if let Some(new_transform_animation) = new_animation.transform_animation() {
                sprite_index = new_transform_animation.sprite_index(&animating_entity.last_valid_direction);
            }
            // if we are in a blocking animation we don't want to changed our animation state
            // info!("{}", animating_entity.in_blocking_animation);
            if animating_entity.in_blocking_animation {
                // check the new animations priority from the current one
                let mut curr_animation = animating_entity.curr_animation.lock().unwrap();
                if let Some(curr_timed_animation) = curr_animation.timed_animation() {
                    if curr_timed_animation.blocking_priority > new_priority {
                        // info!("blocking animation");
                        continue;
                    }
                }
                else if let Some(curr_single_frame_animation) = curr_animation.single_frame_animation() {
                    // info!("{} {} {}", curr_single_frame_animation.blocking_priority, new_priority, curr_single_frame_animation.blocking_finished);
                    if curr_single_frame_animation.blocking_priority > new_priority  && !curr_single_frame_animation.blocking_finished {
                        // info!("blocking animation");
                        continue;
                    }
                }
                else {
                    // info!("blocking animation");
                    continue;
                }
            }
            animating_entity.curr_animation.lock().unwrap().reset_animation();
            animating_entity.curr_animation = new_animation_arc.clone();
            animating_entity.in_blocking_animation = blocking;
            
            sprite.index = sprite_index;
            *texture_atlas = new_animation.get_atlas();
        }
        animating_entity.curr_animation_called = true;
        // if our direction is changed we can set the current direction
        if animating_entity.curr_direction != *direction {
            animating_entity.curr_direction = direction.clone();
            // we don't want to set a Still direction to our last valid direction field because our animations won't be right
            if *direction != AnimationDirection::Still {
                animating_entity.last_valid_direction = direction.clone();
            }
        }
    } 
    // our main animating loop
    for (entity, animation_entity) in animations.entities.iter_mut() {
        // query the sprite and transform from the entity
        let (_, sprite, transform, _) = match query.get_mut(*entity) {
            Ok(query) => query,
            Err(_) => {
                // if we didn't find the entity in the query we should remove it as it doesn't exist anymore
                entities_to_remove.0.push(*entity);
                continue;
            }
        };
        // unlock the current animation once so we don't hit a deadlock
        let mut curr_animation = animation_entity.curr_animation.lock().unwrap();

        // if the current animation wasn't started via an `AnimationEvent`
        if !animation_entity.curr_animation_called {
            continue;
        }

        // if the current animation is transform based we should cycle it
        if let Some(transform_animation) = curr_animation.transform_animation() {
            if animation_entity.in_blocking_animation {
                continue;
            }
            if let None = transform_animation.cycle_animation(
                sprite, 
                &animation_entity.last_valid_direction, 
                transform, 
                config.pixels_per_meter) {
                animation_entity.curr_animation_called = false;
            }
        } 
        // if our current animation is timed based we should cycle it
        else if let Some(timed_animation) = curr_animation.timed_animation() {
            if let None = timed_animation.cycle_animation(sprite, &animation_entity.last_valid_direction, time.delta()) {
                animation_entity.in_blocking_animation = false;
                animation_entity.curr_animation_called = false;
            }
        }
        // if the current animation is linear time based we should cycle it
        else if let Some(linear_timed_animation) = curr_animation.linear_timed_animation() {
            if let None = linear_timed_animation.cycle_animation(sprite, time.delta()) {
                animation_entity.in_blocking_animation = false;
                animation_entity.curr_animation_called = false;
            }
        }
        // if the current animation is linear transform based we should cycle it 
        else if let Some(linear_transform_animation) = curr_animation.linear_transform_animation() {
            if let None = linear_transform_animation.cycle_animation(
                sprite,
                &animation_entity.last_valid_direction,
                transform,
                config.pixels_per_meter) {
                animation_entity.curr_animation_called = false;
            }
        }
        // if the current animation is a single frame animation
        else if let Some(single_frame_animation) = curr_animation.single_frame_animation() {
            single_frame_animation.cycle_animation(sprite, &animation_entity.last_valid_direction, time.delta())
        }
        // if we get here something bad happened it will most likely never hit as the typing is pretty strong
        else {
            panic!("Something Went Terribly Wrong Animating {} Check Your Configurations", curr_animation.get_name());
        } 
    }
}

fn catch_reset_events(
    mut query: Query<(
        &mut TextureAtlasSprite,
        &AnimationDirection
    )>,
    mut animations: ResMut<Animations>,
    mut entities_to_remove: ResMut<EntitesToRemove>,
    mut animation_events: EventReader<ResetAnimationEvent>
) {
    for event in animation_events.read() {
        // if the entity wasn't found in the query we want to remove it from our data structure
        let (sprite, direction) = match query.get_mut(event.0) {
            Ok(q) => q,
            Err(_) => {
                entities_to_remove.0.push(event.0);
                continue;
            }
        };
        let mut curr_animation = animations.entities
            .get_mut(&event.0)
            .expect("Entity Not Found from `ResetAnimationEvent`")
            .curr_animation
            .lock()
            .unwrap()
        ;
        // try and get the current animation
        // if it is time based
        if let Some(timed_animation) = curr_animation.timed_animation() {
            timed_animation.reset_animation(Some(sprite), Some(direction));
        }
        // if it is transform based
        else if let Some(transform_animation) = curr_animation.transform_animation() {
            transform_animation.reset_animation(Some(sprite), Some(direction));
        }
        // if it is linear time based
        else if let Some(linear_timed_animation) = curr_animation.linear_timed_animation() {
            linear_timed_animation.reset_animation(Some(sprite));
        }
        // if it is linear transform based
        else if let Some(linear_transform_animation) = curr_animation.linear_transform_animation() {
            linear_transform_animation.reset_animation(Some(sprite), Some(direction));
        }
        else {
            panic!("Something went terribly wrong getting the current animation");
        }
    }
}

fn catch_fx_animation_events(
    mut event_reader: EventReader<FXAnimationEvent>,
    mut commands: Commands,
    mut animations: ResMut<Animations>
) {
    for event in event_reader.read() {
        let _ = animations.start_fx_animation(event.0, &mut commands, event.1);
    }
}

/// This system is for any cleanup of despawned entities
fn remove_entites(
    mut animations: ResMut<Animations>,
    mut entities_to_remove: ResMut<EntitesToRemove>
) {
    for entity in entities_to_remove.0.iter() {
        animations.entities.remove(&entity);
    }
    entities_to_remove.0.clear();
}