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
use std::cell::RefCell;
use crate::engine::d2::Entity;
use super::Action;
struct SequenceProps {
running_actions: Vec<Box<dyn Action>>,
idx: usize,
}
/// An action that manages a list of other actions, running them one-by-one sequentially until they
/// all finish.
pub struct Sequence {
props: RefCell<SequenceProps>,
}
impl Sequence {
pub fn new(actions: Option<Vec<Box<dyn Action>>>) -> Self {
Self {
props: RefCell::new(SequenceProps {
idx: 0,
running_actions: actions.unwrap_or_default(),
}),
}
}
pub fn add(&self, action: Box<dyn Action>) -> &Self {
let mut props = self.props.borrow_mut();
props.running_actions.push(action);
self
}
// pub fn remove(&self, action: Box<dyn Action>) -> bool {
// let mut props = self.props.borrow_mut();
// match props.running_actions.iter().position(|&item| item == action) {
// Some(idx) => {
// props.running_actions.remove(idx);
// true
// },
// None => false
// }
// }
pub fn remove_all(&self) {
let mut props = self.props.borrow_mut();
props.idx = 0;
props.running_actions.clear();
}
}
impl Action for Sequence {
fn update(&self, dt: f32, actor: &mut Entity) -> f32 {
let mut props = self.props.borrow_mut();
// The total time taken by the actions updated this frame
let mut total = 0.0;
loop {
let idx = props.idx;
if let Some(action) = props.running_actions.get_mut(idx) {
let spent = action.update(dt - total, actor);
if spent >= 0.0 {
// This action completed, add it to the total time
total += spent;
} else {
// This action didn't complete, so neither will this sequence
return -1.0;
}
}
props.idx += 1;
if props.idx >= props.running_actions.len() {
// If this is the last action, reset to the starting position and finish
props.idx = 0;
break;
} else if total > dt {
// Otherwise, if there are still actions but not enough time to complete them
return -1.0;
}
}
total
}
}