veloren/common/src/event.rs

189 lines
4.7 KiB
Rust
Raw Normal View History

use crate::{
character::CharacterId,
comp,
rtsim::RtSimEntity,
trade::{TradeAction, TradeId},
uid::Uid,
util::Dir,
Explosion,
};
use comp::{
invite::{InviteKind, InviteResponse},
item::{Item, Reagent},
Ori, Pos,
};
2019-08-07 15:39:16 +00:00
use specs::Entity as EcsEntity;
use std::{collections::VecDeque, ops::DerefMut, sync::Mutex};
2019-08-07 15:39:16 +00:00
use vek::*;
pub enum LocalEvent {
2020-03-21 22:55:20 +00:00
/// Applies upward force to entity's `Vel`
Jump(EcsEntity),
2020-09-19 16:55:31 +00:00
/// Applies the `impulse` to `entity`'s `Vel`
ApplyImpulse {
entity: EcsEntity,
impulse: 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>,
explosion: Explosion,
2020-03-22 19:39:50 +00:00
owner: Option<Uid>,
},
2019-09-21 12:43:24 +00:00
Damage {
entity: EcsEntity,
2020-12-06 02:29:46 +00:00
change: comp::HealthChange,
},
PoiseChange {
entity: EcsEntity,
change: comp::PoiseChange,
2020-12-10 00:32:24 +00:00
kb_dir: Vec3<f32>,
2019-09-21 12:43:24 +00:00
},
2020-11-11 11:42:22 +00:00
Delete(EcsEntity),
2019-09-17 12:43:19 +00:00
Destroy {
entity: EcsEntity,
cause: comp::HealthSource,
},
InventoryManip(EcsEntity, comp::SlotManip),
GroupManip(EcsEntity, comp::GroupManip),
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>,
speed: f32,
2019-09-28 19:35:28 +00:00
},
Shockwave {
properties: comp::shockwave::Properties,
pos: Pos,
ori: Ori,
},
Knockback {
entity: EcsEntity,
2020-09-19 16:55:31 +00:00
impulse: Vec3<f32>,
},
BeamSegment {
properties: comp::beam::Properties,
pos: Pos,
ori: Ori,
},
LandOnGround {
entity: EcsEntity,
vel: Vec3<f32>,
},
2020-10-07 02:23:20 +00:00
EnableLantern(EcsEntity),
DisableLantern(EcsEntity),
NpcInteract(EcsEntity, EcsEntity),
InviteResponse(EcsEntity, InviteResponse),
InitiateInvite(EcsEntity, Uid, InviteKind),
ProcessTradeAction(EcsEntity, TradeId, TradeAction),
Mount(EcsEntity, EcsEntity),
Unmount(EcsEntity),
2019-10-11 04:30:34 +00:00
Possess(Uid, Uid),
/// Inserts default components for a character when loading into the game
InitCharacterData {
entity: EcsEntity,
character_id: CharacterId,
},
UpdateCharacterData {
entity: EcsEntity,
2020-11-03 00:12:49 +00:00
components: (
comp::Body,
comp::Stats,
comp::Inventory,
Option<comp::Waypoint>,
),
},
ExitIngame {
entity: EcsEntity,
},
// TODO: to avoid breakage when adding new fields, perhaps have an `NpcBuilder` type?
CreateNpc {
pos: comp::Pos,
stats: comp::Stats,
health: comp::Health,
poise: comp::Poise,
loadout: comp::inventory::loadout::Loadout,
body: comp::Body,
2020-07-05 12:39:28 +00:00
agent: Option<comp::Agent>,
alignment: comp::Alignment,
scale: comp::Scale,
home_chunk: Option<comp::HomeChunk>,
drop_item: Option<Item>,
rtsim_entity: Option<RtSimEntity>,
},
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
2020-07-12 20:18:57 +00:00
Chat(comp::UnresolvedChatMsg),
2020-12-04 22:24:56 +00:00
Aura {
entity: EcsEntity,
aura_change: comp::AuraChange,
},
Buff {
entity: EcsEntity,
buff_change: comp::BuffChange,
},
2020-10-30 21:49:58 +00:00
EnergyChange {
entity: EcsEntity,
2020-10-30 21:49:58 +00:00
change: comp::EnergyChange,
},
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(),
}
}
pub fn emit_now(&self, event: E) { self.queue.lock().unwrap().push_back(event); }
2019-08-07 17:17:04 +00:00
pub fn recv_all(&self) -> impl ExactSizeIterator<Item = E> {
std::mem::replace(self.queue.lock().unwrap().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().unwrap().append(&mut self.events); }
2019-08-07 15:39:16 +00:00
}