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
//! Generic event. Chains multiple events so that they happen sequentially.

use amethyst::ecs::World;

use super::{BoxedGameEvent, ExecutionConditions, GameEvent};

use std::collections::VecDeque;

#[derive(Default)]
pub struct ChainedEvents {
    chain: VecDeque<Box<dyn GameEvent + Sync + Send>>,
    called_start: bool,
}

impl ChainedEvents {
    pub fn add_event(&mut self, event: Box<dyn GameEvent + Sync + Send>) {
        if self.chain.is_empty() {
            self.called_start = false;
        }

        self.chain.push_back(event);
    }
}

impl Clone for ChainedEvents {
    fn clone(&self) -> ChainedEvents {
        ChainedEvents {
            chain: self.chain.iter().map(|event| event.boxed_clone()).collect(),
            called_start: self.called_start,
        }
    }
}

impl GameEvent for ChainedEvents {
    fn boxed_clone(&self) -> BoxedGameEvent {
        Box::new(self.clone())
    }

    fn get_execution_conditions(&self) -> ExecutionConditions {
        if let Some(event) = self.chain.front() {
            event.get_execution_conditions()
        } else {
            ExecutionConditions {
                requires_disabled_input: false
            }
        }
    }

    fn start(&mut self, world: &mut World) {
        self.called_start = true;

        if let Some(event) = self.chain.front_mut() {
            event.start(world);
        }
    }

    fn tick(&mut self, world: &mut World, disabled_inputs: bool) {
        if let Some(event) = self.chain.front_mut() {
            if !self.called_start {
                event.start(world);
                self.called_start = true;
            }

            event.tick(world, disabled_inputs);

            if event.is_complete(world) {
                self.chain.pop_front();
                self.called_start = false;
            }
        }
    }

    fn is_complete(&self, _world: &mut World) -> bool {
        self.chain.is_empty()
    }
}