logo
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
use std::cell::RefCell;

use crate::engine::d2::Entity;

use super::Action;

struct ParallelProps {
    running_actions: Vec<Box<dyn Action>>,
    completed_actions: Vec<Box<dyn Action>>,
}

/// An action that manages a list of other actions, running them together in parallel until they all
/// finish.
pub struct Parallel {
    props: RefCell<ParallelProps>,
}

impl Parallel {
    pub fn new<A: Action>(actions: Option<Vec<Box<dyn Action>>>) -> Self {
        Self {
            props: RefCell::new(ParallelProps {
                completed_actions: Vec::new(),
                running_actions: actions.unwrap_or_default(),
            }),
        }
    }

    pub fn add(&self, action: Box<dyn Action>) {
        let mut props = self.props.borrow_mut();
        props.running_actions.push(action);
    }

    // pub fn remove(&self, action: Box<dyn Action>) -> bool {
    //     match self.running_actions.iter().position(|&item| item == action) {
    //         Some(idx) => {
    //             self.running_actions.remove(idx);
    //             true
    //         }
    //         None => false
    //     }
    // }

    pub fn remove_all(&mut self) {
        let mut props = self.props.borrow_mut();
        props.running_actions.clear();
        props.completed_actions.clear();
    }
}

impl Action for Parallel {
    fn update(&self, dt: f32, actor: &mut Entity) -> f32 {
        let mut props = self.props.borrow_mut();
        let mut done = true;
        let mut max_spent = 0.0;
        for idx in 0..props.running_actions.len() {
            if let Some(action) = props.running_actions.get_mut(idx) {
                let spent = action.update(dt, actor);
                if spent >= 0.0 {
                    props.running_actions.remove(idx);
                    // self.completed_actions.push(action); // DV
                    if spent > max_spent {
                        max_spent = spent;
                    }
                } else {
                    // We can't possibly finish this frame, but continue ticking the rest of the
                    // actions anyways
                    done = false;
                }
            }
        }

        if done {
            // self.running_actions = self.completed_actions; // DV
            props.completed_actions.clear();

            max_spent
        } else {
            -1.0
        }
    }
}