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
//! Generic event. Repeats an event for a given number of times sequentially.

use amethyst::ecs::World;

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

use std::marker::PhantomData;

pub struct RepeatedEvent<T>
where
    T: 'static + GameEvent + Sync + Send
{
    chain: ChainedEvents,
    phantom_data: PhantomData<T>,
}

impl<T> RepeatedEvent<T>
where
    T: 'static + GameEvent + Sync + Send + Clone
{
    pub fn from_prototype(prototype: &T, repetitions: usize) -> RepeatedEvent<T> {
        let mut chain = ChainedEvents::default();
        for _ in 0..repetitions {
            chain.add_event(Box::new(prototype.clone()));
        }

        RepeatedEvent {
            chain,
            phantom_data: PhantomData,
        }
    }
}

impl<T> Clone for RepeatedEvent<T>
where
    T: 'static + GameEvent + Sync + Send
{
    fn clone(&self) -> RepeatedEvent<T> {
        RepeatedEvent::<T> {
            chain: self.chain.clone(),
            phantom_data: PhantomData,
        }
    }
}

impl<T> GameEvent for RepeatedEvent<T>
where
    T: 'static + GameEvent + Sync + Send
{
    fn boxed_clone(&self) -> BoxedGameEvent {
        Box::new(RepeatedEvent::<T> {
            chain: self.chain.clone(),
            phantom_data: PhantomData,
        })
    }

    fn get_execution_conditions(&self) -> ExecutionConditions {
        self.chain.get_execution_conditions()
    }

    fn start(&mut self, world: &mut World) {
        self.chain.start(world);
    }

    fn tick(&mut self, world: &mut World, disabled_inputs: bool) {
        self.chain.tick(world, disabled_inputs);
    }

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