2019-11-24 20:12:03 +00:00
|
|
|
use crate::{comp, sync::Uid};
|
2019-08-15 22:19:54 +00:00
|
|
|
use parking_lot::Mutex;
|
2019-11-23 08:26:39 +00:00
|
|
|
use serde::Deserialize;
|
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-11-23 08:26:39 +00:00
|
|
|
pub struct SfxEventItem {
|
|
|
|
pub sfx: SfxEvent,
|
|
|
|
pub pos: Option<Vec3<f32>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SfxEventItem {
|
|
|
|
pub fn new(sfx: SfxEvent, pos: Option<Vec3<f32>>) -> Self {
|
|
|
|
Self { sfx, pos }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn at_player_position(sfx: SfxEvent) -> Self {
|
|
|
|
Self { sfx, pos: None }
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 13:28:45 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Hash, Eq)]
|
|
|
|
/// Which Kind of Attack Sfx to play,
|
|
|
|
pub enum AttackSFX {
|
|
|
|
// Decouples attack sfx assets from attack kinds
|
|
|
|
Melee,
|
|
|
|
Bow,
|
|
|
|
}
|
2019-11-23 08:26:39 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Hash, Eq)]
|
|
|
|
pub enum SfxEvent {
|
|
|
|
Idle,
|
|
|
|
PlaceBlock,
|
|
|
|
RemoveBlock,
|
|
|
|
OpenChest,
|
2019-12-08 10:03:40 +00:00
|
|
|
ChatTellReceived,
|
2019-11-23 08:26:39 +00:00
|
|
|
OpenBag,
|
2020-01-01 02:55:48 +00:00
|
|
|
Run,
|
2019-11-23 08:26:39 +00:00
|
|
|
Roll,
|
|
|
|
Climb,
|
|
|
|
Swim,
|
|
|
|
GliderOpen,
|
|
|
|
Glide,
|
|
|
|
GliderClose,
|
|
|
|
Jump,
|
|
|
|
Fall,
|
2020-01-01 02:55:48 +00:00
|
|
|
ExperienceGained,
|
|
|
|
LevelUp,
|
2019-11-23 08:26:39 +00:00
|
|
|
LightLantern,
|
|
|
|
ExtinguishLantern,
|
2020-01-16 13:28:45 +00:00
|
|
|
Attack(AttackSFX),
|
2019-11-23 08:26:39 +00:00
|
|
|
AttackWolf,
|
|
|
|
}
|
|
|
|
|
2019-08-25 14:49:54 +00:00
|
|
|
pub enum LocalEvent {
|
|
|
|
Jump(EcsEntity),
|
2019-09-09 19:11:40 +00:00
|
|
|
WallLeap {
|
|
|
|
entity: EcsEntity,
|
|
|
|
wall_dir: Vec3<f32>,
|
|
|
|
},
|
|
|
|
Boost {
|
|
|
|
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,
|
|
|
|
},
|
2019-09-21 12:43:24 +00:00
|
|
|
Damage {
|
|
|
|
uid: Uid,
|
2019-10-17 20:59:36 +00:00
|
|
|
change: comp::HealthChange,
|
2019-09-21 12:43:24 +00:00
|
|
|
},
|
2019-09-17 12:43:19 +00:00
|
|
|
Destroy {
|
2019-08-23 10:11:37 +00:00
|
|
|
entity: EcsEntity,
|
|
|
|
cause: comp::HealthSource,
|
|
|
|
},
|
2019-10-15 04:06:14 +00:00
|
|
|
InventoryManip(EcsEntity, comp::InventoryManip),
|
2019-08-23 10:11:37 +00:00
|
|
|
Respawn(EcsEntity),
|
2019-09-28 19:35:28 +00:00
|
|
|
Shoot {
|
|
|
|
entity: EcsEntity,
|
|
|
|
dir: Vec3<f32>,
|
2019-10-11 04:30:34 +00:00
|
|
|
body: comp::Body,
|
|
|
|
light: Option<comp::LightEmitter>,
|
2019-09-28 19:35:28 +00:00
|
|
|
projectile: comp::Projectile,
|
2019-10-17 20:59:36 +00:00
|
|
|
gravity: Option<comp::Gravity>,
|
2019-09-28 19:35:28 +00:00
|
|
|
},
|
2019-10-08 16:50:26 +00:00
|
|
|
LandOnGround {
|
|
|
|
entity: EcsEntity,
|
|
|
|
vel: Vec3<f32>,
|
|
|
|
},
|
2019-09-09 19:11:40 +00:00
|
|
|
Mount(EcsEntity, EcsEntity),
|
|
|
|
Unmount(EcsEntity),
|
2019-10-11 04:30:34 +00:00
|
|
|
Possess(Uid, Uid),
|
2019-12-31 08:10:51 +00:00
|
|
|
CreateCharacter {
|
2019-10-15 04:06:14 +00:00
|
|
|
entity: EcsEntity,
|
|
|
|
name: String,
|
|
|
|
body: comp::Body,
|
2019-10-22 18:18:40 +00:00
|
|
|
main: Option<String>,
|
2019-10-15 04:06:14 +00:00
|
|
|
},
|
2019-12-31 08:10:51 +00:00
|
|
|
ExitIngame {
|
|
|
|
entity: EcsEntity,
|
|
|
|
},
|
2019-10-20 05:19:50 +00:00
|
|
|
CreateNpc {
|
|
|
|
pos: comp::Pos,
|
|
|
|
stats: comp::Stats,
|
|
|
|
body: comp::Body,
|
|
|
|
agent: comp::Agent,
|
|
|
|
scale: comp::Scale,
|
|
|
|
},
|
2019-10-15 04:06:14 +00:00
|
|
|
ClientDisconnect(EcsEntity),
|
|
|
|
ChunkRequest(EcsEntity, Vec2<i32>),
|
|
|
|
ChatCmd(EcsEntity, String),
|
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
|
|
|
}
|
|
|
|
}
|