veloren/common/src/event.rs

212 lines
5.7 KiB
Rust
Raw Normal View History

use crate::{
comp,
comp::item::{Consumable, ItemKind},
sync::Uid,
util::Dir,
};
2020-05-28 12:07:53 +00:00
use comp::{item::ToolCategory, CharacterAbilityType, InventoryUpdateEvent, Item};
2019-08-15 22:19:54 +00:00
use parking_lot::Mutex;
use serde::Deserialize;
2019-08-07 15:39:16 +00:00
use specs::Entity as EcsEntity;
use std::{collections::VecDeque, convert::TryFrom, ops::DerefMut};
2019-08-07 15:39:16 +00:00
use vek::*;
pub struct SfxEventItem {
pub sfx: SfxEvent,
pub pos: Option<Vec3<f32>>,
pub vol: Option<f32>,
}
impl SfxEventItem {
pub fn new(sfx: SfxEvent, pos: Option<Vec3<f32>>, vol: Option<f32>) -> Self {
Self { sfx, pos, vol }
}
pub fn at_player_position(sfx: SfxEvent) -> Self {
Self {
sfx,
pos: None,
vol: None,
}
}
}
2020-01-18 13:13:25 +00:00
#[derive(Clone, Debug, PartialEq, Deserialize, Hash, Eq)]
pub enum SfxEvent {
Idle,
Run,
Roll,
Climb,
GliderOpen,
Glide,
GliderClose,
Jump,
Fall,
ExperienceGained,
LevelUp,
Attack(CharacterAbilityType, ToolCategory),
Wield(ToolCategory),
Unwield(ToolCategory),
Inventory(SfxInventoryEvent),
}
#[derive(Clone, Debug, PartialEq, Deserialize, Hash, Eq)]
pub enum SfxInventoryEvent {
Collected,
CollectedTool(ToolCategory),
CollectFailed,
Consumed(Consumable),
Debug,
Dropped,
Given,
Swapped,
}
impl From<&InventoryUpdateEvent> for SfxEvent {
fn from(value: &InventoryUpdateEvent) -> Self {
match value {
InventoryUpdateEvent::Collected(item) => {
// Handle sound effects for types of collected items, falling back to the
// default Collected event
match item.kind {
ItemKind::Tool(tool) => SfxEvent::Inventory(SfxInventoryEvent::CollectedTool(
ToolCategory::try_from(tool.kind).unwrap(),
)),
_ => SfxEvent::Inventory(SfxInventoryEvent::Collected),
}
},
InventoryUpdateEvent::CollectFailed => {
SfxEvent::Inventory(SfxInventoryEvent::CollectFailed)
},
InventoryUpdateEvent::Consumed(consumable) => {
SfxEvent::Inventory(SfxInventoryEvent::Consumed(*consumable))
},
InventoryUpdateEvent::Debug => SfxEvent::Inventory(SfxInventoryEvent::Debug),
InventoryUpdateEvent::Dropped => SfxEvent::Inventory(SfxInventoryEvent::Dropped),
InventoryUpdateEvent::Given => SfxEvent::Inventory(SfxInventoryEvent::Given),
InventoryUpdateEvent::Swapped => SfxEvent::Inventory(SfxInventoryEvent::Swapped),
_ => SfxEvent::Inventory(SfxInventoryEvent::Swapped),
}
}
}
pub enum LocalEvent {
2020-03-21 22:55:20 +00:00
/// Applies upward force to entity's `Vel`
Jump(EcsEntity),
/// Applies the `force` to `entity`'s `Vel`
ApplyForce { entity: EcsEntity, force: Vec3<f32> },
2020-03-21 22:55:20 +00:00
/// Applies leaping force to `entity`'s `Vel` away from `wall_dir` direction
WallLeap {
entity: EcsEntity,
2020-03-21 22:55:20 +00:00
wall_dir: Vec3<f32>,
},
2020-03-21 22:55:20 +00:00
/// Applies `vel` velocity to `entity`
Boost { entity: EcsEntity, vel: Vec3<f32> },
}
#[allow(clippy::large_enum_variant)] // TODO: Pending review in #587
pub enum ServerEvent {
Explosion {
pos: Vec3<f32>,
2020-03-22 19:39:50 +00:00
power: f32,
owner: Option<Uid>,
},
2019-09-21 12:43:24 +00:00
Damage {
uid: Uid,
change: comp::HealthChange,
2019-09-21 12:43:24 +00:00
},
2019-09-17 12:43:19 +00:00
Destroy {
entity: EcsEntity,
cause: comp::HealthSource,
},
InventoryManip(EcsEntity, comp::InventoryManip),
Respawn(EcsEntity),
2019-09-28 19:35:28 +00:00
Shoot {
entity: EcsEntity,
dir: Dir,
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,
gravity: Option<comp::Gravity>,
2019-09-28 19:35:28 +00:00
},
LandOnGround {
entity: EcsEntity,
vel: Vec3<f32>,
},
ToggleLantern(EcsEntity),
Mount(EcsEntity, EcsEntity),
Unmount(EcsEntity),
2019-10-11 04:30:34 +00:00
Possess(Uid, Uid),
2020-06-01 09:21:33 +00:00
LevelUp(EcsEntity, u32),
/// Inserts default components for a character when loading into the game
InitCharacterData {
entity: EcsEntity,
character_id: i32,
},
UpdateCharacterData {
entity: EcsEntity,
components: (comp::Body, comp::Stats, comp::Inventory, comp::Loadout),
},
ExitIngame {
entity: EcsEntity,
},
CreateNpc {
pos: comp::Pos,
stats: comp::Stats,
loadout: comp::Loadout,
body: comp::Body,
agent: comp::Agent,
alignment: comp::Alignment,
scale: comp::Scale,
drop_item: Option<Item>,
},
CreateWaypoint(Vec3<f32>),
ClientDisconnect(EcsEntity),
ChunkRequest(EcsEntity, Vec2<i32>),
ChatCmd(EcsEntity, String),
/// Send a chat message to the player from an npc or other player
Chat(comp::ChatMsg),
2019-08-07 15:39:16 +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
}
impl<E> EventBus<E> {
pub fn emitter(&self) -> Emitter<E> {
2019-08-07 15:39:16 +00:00
Emitter {
bus: self,
events: VecDeque::new(),
}
}
2020-03-22 04:49:32 +00:00
pub fn emit_now(&self, event: E) { self.queue.lock().push_back(event); }
2019-08-07 17:17:04 +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
}
}
pub struct Emitter<'a, E> {
bus: &'a EventBus<E>,
events: VecDeque<E>,
2019-08-07 15:39:16 +00:00
}
impl<'a, E> Emitter<'a, E> {
2020-03-22 04:49:32 +00:00
pub fn emit(&mut self, event: E) { self.events.push_back(event); }
pub fn append(&mut self, other: &mut VecDeque<E>) { self.events.append(other) }
2019-08-07 15:39:16 +00:00
}
impl<'a, E> Drop for Emitter<'a, E> {
fn drop(&mut self) { self.bus.queue.lock().append(&mut self.events); }
2019-08-07 15:39:16 +00:00
}