1use behaviour_result::BehaviourResult;
2use behaviour_node::BehaviourNode;
3
4pub struct Sequence<'a, T> {
13 pub name: &'static str,
14 pub children: Vec<Box<BehaviourNode<T> + 'a>>
15}
16
17impl <'a, T: 'a> Sequence<'a, T> {
18
19 pub fn new(name: &'static str) -> Sequence<'a, T> {
20 Sequence {
21 name: name,
22 children: vec![]
23 }
24 }
25
26 pub fn with_capacity(name: &'static str, capacity: usize) -> Sequence<'a, T> {
27 Sequence {
28 name: name,
29 children: Vec::with_capacity(capacity)
30 }
31 }
32
33 pub fn with_children(name: &'static str, children: Vec<Box<BehaviourNode<T> + 'a>>) -> Sequence<'a, T> {
34 Sequence {
35 name: name,
36 children: children
37 }
38 }
39
40 pub fn children(&mut self, new_children: Vec<Box<BehaviourNode<T> + 'a>>) {
41 self.children = new_children;
42 }
43
44 pub fn add(&mut self, new_child: Box<BehaviourNode<T> + 'a>) {
45 self.children.push(new_child);
46 }
47
48}
49
50impl <'a, T: 'a> BehaviourNode<T> for Sequence<'a, T> {
51
52 fn evaluate(&mut self, target: &mut T) -> BehaviourResult {
53
54 for child in self.children.iter_mut() {
55 let result = child.evaluate(target);
56 match result {
57 BehaviourResult::Success => { },
58 _ => { return result; }
59 }
60 }
61
62 BehaviourResult::Success
63 }
64
65}