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
use std::cmp::Ordering;
use bevy_app::{App, CoreStage, Plugin};
use crate::*;
pub struct SequentialActionsPlugin;
impl SequentialActionsPlugin {
pub fn get_systems() -> SystemSet {
SystemSet::new()
.with_system(check_actions)
.with_system(reset_count.after(check_actions))
}
}
impl Plugin for SequentialActionsPlugin {
fn build(&self, app: &mut App) {
app.add_system_set_to_stage(CoreStage::Last, Self::get_systems());
}
}
fn check_actions(
action_q: Query<(Entity, &CurrentAction, &ActionFinished), Changed<ActionFinished>>,
mut commands: Commands,
) {
for (agent, current_action, finished) in action_q.iter() {
if let Some((current_action, _)) = ¤t_action.0 {
let finished_count = finished.total();
let active_count = current_action.len();
match finished_count.cmp(&active_count) {
Ordering::Less => {}
Ordering::Equal => {
commands.add(move |world: &mut World| {
world.finish_action(agent);
});
}
Ordering::Greater => {
panic!(
"Finished actions exceeds active. \
Entity {agent:?} has {active_count} active action(s), \
but a total of {finished_count} action(s) have been confirmed finished."
);
}
}
}
}
}
fn reset_count(mut finished_q: Query<&mut ActionFinished, Changed<ActionFinished>>) {
for mut finished in finished_q.iter_mut() {
finished.bypass_change_detection().reset_count = 0;
}
}