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
use std::sync::Arc;
use bevy::prelude::*;
use crate::thinker::{ActionEnt, Actor};
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ActionState {
Init,
Requested,
Executing,
Cancelled,
Success,
Failure,
}
impl Default for ActionState {
fn default() -> Self {
Self::Init
}
}
impl ActionState {
pub fn new() -> Self {
Self::default()
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct ActionBuilderId;
#[derive(Debug, Clone)]
pub(crate) struct ActionBuilderWrapper(pub Arc<ActionBuilderId>, pub Arc<dyn ActionBuilder>);
impl ActionBuilderWrapper {
pub fn new(builder: Arc<dyn ActionBuilder>) -> Self {
ActionBuilderWrapper(Arc::new(ActionBuilderId), builder)
}
}
pub trait ActionBuilder: std::fmt::Debug + Send + Sync {
fn build(&self, cmd: &mut Commands, action: Entity, actor: Entity);
fn attach(&self, cmd: &mut Commands, actor: Entity) -> Entity {
let action_ent = ActionEnt(cmd.spawn().id());
cmd.entity(action_ent.0)
.insert(Name::new("Action"))
.insert(ActionState::new())
.insert(Actor(actor));
self.build(cmd, action_ent.0, actor);
action_ent.0
}
}
#[derive(Debug)]
pub struct StepsBuilder {
steps: Vec<Arc<dyn ActionBuilder>>,
}
impl StepsBuilder {
pub fn step(&mut self, action_builder: impl ActionBuilder + 'static) -> &mut Self {
self.steps.push(Arc::new(action_builder));
self
}
}
impl ActionBuilder for StepsBuilder {
fn build(&self, cmd: &mut Commands, action: Entity, actor: Entity) {
if let Some(step) = self.steps.get(0) {
let child_action = step.attach(cmd, actor);
cmd.entity(action)
.insert(Name::new("Steps Action"))
.insert(Steps {
active_step: 0,
active_ent: ActionEnt(child_action),
steps: self.steps.clone(),
})
.push_children(&[child_action]);
}
}
}
#[derive(Debug)]
pub struct Steps {
steps: Vec<Arc<dyn ActionBuilder>>,
active_step: usize,
active_ent: ActionEnt,
}
impl Steps {
pub fn build() -> StepsBuilder {
StepsBuilder { steps: Vec::new() }
}
}
pub fn steps_system(
mut cmd: Commands,
mut steps_q: Query<(Entity, &Actor, &mut Steps)>,
mut states: Query<&mut ActionState>,
) {
use ActionState::*;
for (seq_ent, Actor(actor), mut steps_action) in steps_q.iter_mut() {
let current_state = states.get_mut(seq_ent).expect("uh oh").clone();
match current_state {
Requested => {
let mut step_state = states.get_mut(steps_action.active_ent.0).expect("oops");
*step_state = Requested;
let mut current_state = states.get_mut(seq_ent).expect("uh oh");
*current_state = Executing;
}
Executing => {
let mut step_state = states.get_mut(steps_action.active_ent.0).expect("bug");
match *step_state {
Init => {
*step_state = Requested;
}
Executing | Requested => {
}
Cancelled | Failure => {
let step_state = step_state.clone();
let mut seq_state = states.get_mut(seq_ent).expect("idk");
*seq_state = step_state;
cmd.entity(steps_action.active_ent.0).despawn_recursive();
}
Success if steps_action.active_step == steps_action.steps.len() - 1 => {
let step_state = step_state.clone();
let mut seq_state = states.get_mut(seq_ent).expect("idk");
*seq_state = step_state;
cmd.entity(steps_action.active_ent.0).despawn_recursive();
}
Success => {
cmd.entity(steps_action.active_ent.0).despawn_recursive();
steps_action.active_step += 1;
let step_builder = steps_action.steps[steps_action.active_step].clone();
let step_ent = step_builder.attach(&mut cmd, *actor);
cmd.entity(seq_ent).push_children(&[step_ent]);
let mut step_state = states.get_mut(step_ent).expect("oops");
*step_state = ActionState::Requested;
}
}
}
Cancelled => {
let mut step_state = states.get_mut(steps_action.active_ent.0).expect("oops");
*step_state = ActionState::Cancelled;
}
Init | Success | Failure => {
}
}
}
}