2019-08-23 10:11:37 +00:00
|
|
|
use crate::comp;
|
2019-08-15 22:19:54 +00:00
|
|
|
use parking_lot::Mutex;
|
2019-08-07 15:39:16 +00:00
|
|
|
use specs::Entity as EcsEntity;
|
2019-08-15 22:19:54 +00:00
|
|
|
use std::{collections::VecDeque, ops::DerefMut};
|
2019-08-07 15:39:16 +00:00
|
|
|
use vek::*;
|
|
|
|
|
2019-08-25 14:49:54 +00:00
|
|
|
pub enum LocalEvent {
|
|
|
|
Jump(EcsEntity),
|
2019-08-28 20:47:52 +00:00
|
|
|
Boost { entity: EcsEntity, vel: Vec3<f32> },
|
2019-08-25 18:31:56 +00:00
|
|
|
LandOnGround { entity: EcsEntity, vel: Vec3<f32> },
|
2019-08-25 14:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ServerEvent {
|
2019-08-23 10:11:37 +00:00
|
|
|
Explosion {
|
|
|
|
pos: Vec3<f32>,
|
|
|
|
radius: f32,
|
|
|
|
},
|
|
|
|
Die {
|
|
|
|
entity: EcsEntity,
|
|
|
|
cause: comp::HealthSource,
|
|
|
|
},
|
|
|
|
Respawn(EcsEntity),
|
2019-08-25 12:27:17 +00:00
|
|
|
Shoot(EcsEntity),
|
2019-08-07 15:39:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 14:49:54 +00:00
|
|
|
pub struct EventBus<E> {
|
|
|
|
queue: Mutex<VecDeque<E>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E> Default for EventBus<E> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
queue: Mutex::new(VecDeque::new()),
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 15:39:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 14:49:54 +00:00
|
|
|
impl<E> EventBus<E> {
|
|
|
|
pub fn emitter(&self) -> Emitter<E> {
|
2019-08-07 15:39:16 +00:00
|
|
|
Emitter {
|
|
|
|
bus: self,
|
|
|
|
events: VecDeque::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 14:49:54 +00:00
|
|
|
pub fn emit(&self, event: E) {
|
2019-08-15 22:19:54 +00:00
|
|
|
self.queue.lock().push_front(event);
|
2019-08-07 17:17:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 14:49:54 +00:00
|
|
|
pub fn recv_all(&self) -> impl ExactSizeIterator<Item = E> {
|
2019-08-15 22:19:54 +00:00
|
|
|
std::mem::replace(self.queue.lock().deref_mut(), VecDeque::new()).into_iter()
|
2019-08-07 15:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 14:49:54 +00:00
|
|
|
pub struct Emitter<'a, E> {
|
|
|
|
bus: &'a EventBus<E>,
|
|
|
|
events: VecDeque<E>,
|
2019-08-07 15:39:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 14:49:54 +00:00
|
|
|
impl<'a, E> Emitter<'a, E> {
|
|
|
|
pub fn emit(&mut self, event: E) {
|
2019-08-07 15:39:16 +00:00
|
|
|
self.events.push_front(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 14:49:54 +00:00
|
|
|
impl<'a, E> Drop for Emitter<'a, E> {
|
2019-08-07 15:39:16 +00:00
|
|
|
fn drop(&mut self) {
|
2019-08-15 22:19:54 +00:00
|
|
|
self.bus.queue.lock().append(&mut self.events);
|
2019-08-07 15:39:16 +00:00
|
|
|
}
|
|
|
|
}
|