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
//! A simple event queue. This is a resource that stays inside the World and
//! is used to store events before they're processed by the game state.

use super::GameEvent;

use std::collections::VecDeque;

#[derive(Default)]
pub struct EventQueue {
    events: VecDeque<Box<dyn GameEvent + Sync + Send>>,
}

impl EventQueue {
    pub fn push<T>(&mut self, event: T)
    where
        T: GameEvent + Sync + Send + 'static
    {
        self.push_boxed(Box::new(event));
    }

    pub fn push_boxed(&mut self, event: Box<dyn GameEvent + Sync + Send>) {
        self.events.push_back(event);
    }

    pub fn pop(&mut self) -> Option<Box<dyn GameEvent + Sync + Send>> {
        self.events.pop_front()
    }
}