use crate::comp; use parking_lot::Mutex; use specs::Entity as EcsEntity; use sphynx::Uid; use std::{collections::VecDeque, ops::DerefMut}; use vek::*; pub enum LocalEvent { Jump(EcsEntity), WallLeap { entity: EcsEntity, wall_dir: Vec3, }, Boost { entity: EcsEntity, vel: Vec3, }, } pub enum ServerEvent { Explosion { pos: Vec3, radius: f32, }, Damage { uid: Uid, dmg: u32, cause: comp::HealthSource, }, Destroy { entity: EcsEntity, cause: comp::HealthSource, }, Respawn(EcsEntity), Shoot { entity: EcsEntity, dir: Vec3, body: comp::Body, light: Option, projectile: comp::Projectile, }, LandOnGround { entity: EcsEntity, vel: Vec3, }, Mount(EcsEntity, EcsEntity), Unmount(EcsEntity), Possess(Uid, Uid), } pub struct EventBus { queue: Mutex>, } impl Default for EventBus { fn default() -> Self { Self { queue: Mutex::new(VecDeque::new()), } } } impl EventBus { pub fn emitter(&self) -> Emitter { Emitter { bus: self, events: VecDeque::new(), } } pub fn emit(&self, event: E) { self.queue.lock().push_front(event); } pub fn recv_all(&self) -> impl ExactSizeIterator { std::mem::replace(self.queue.lock().deref_mut(), VecDeque::new()).into_iter() } } pub struct Emitter<'a, E> { bus: &'a EventBus, events: VecDeque, } impl<'a, E> Emitter<'a, E> { pub fn emit(&mut self, event: E) { self.events.push_front(event); } } impl<'a, E> Drop for Emitter<'a, E> { fn drop(&mut self) { self.bus.queue.lock().append(&mut self.events); } }