From dc1844b489fbf84e91c06b8930aec66cc63f6908 Mon Sep 17 00:00:00 2001 From: Ben Wallis Date: Sun, 28 Jun 2020 16:21:12 +0100 Subject: [PATCH] Moved sfx from common to voxygen --- client/src/lib.rs | 29 +----- common/src/event.rs | 91 +------------------ common/src/state.rs | 3 +- .../src/audio/sfx/event_mapper/combat/mod.rs | 4 +- .../audio/sfx/event_mapper/combat/tests.rs | 2 +- .../audio/sfx/event_mapper/movement/mod.rs | 5 +- .../audio/sfx/event_mapper/movement/tests.rs | 2 +- .../audio/sfx/event_mapper/progression/mod.rs | 8 +- .../sfx/event_mapper/progression/tests.rs | 2 +- voxygen/src/audio/sfx/mod.rs | 88 +++++++++++++++++- voxygen/src/ecs/mod.rs | 5 + voxygen/src/session.rs | 34 ++++++- 12 files changed, 140 insertions(+), 133 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index cca7a0d6a6..d83eedc9ff 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -21,7 +21,6 @@ use common::{ self, ControlAction, ControlEvent, Controller, ControllerInputs, InventoryManip, InventoryUpdateEvent, }, - event::{EventBus, SfxEvent, SfxEventItem}, msg::{ validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, Notification, PlayerInfo, PlayerListUpdate, RegisterError, RequestStateError, ServerInfo, ServerMsg, @@ -57,6 +56,7 @@ pub enum Event { Chat(comp::ChatMsg), Disconnect, DisconnectionNotification(u64), + InventoryUpdated(InventoryUpdateEvent), Notification(Notification), SetViewDistance(u32), } @@ -883,34 +883,15 @@ impl Client { self.clean_state(); }, ServerMsg::InventoryUpdate(inventory, event) => { - // TODO: Move this SFX code to Voxygen - let sfx_event = SfxEvent::from(&event); - self.state - .ecs() - .read_resource::>() - .emit_now(SfxEventItem::at_player_position(sfx_event)); - match event { - InventoryUpdateEvent::CollectFailed => { - // TODO This might not be the best way to show an error - frontend_events.push(Event::Chat(comp::ChatMsg { - message: String::from( - "Failed to collect item. Your inventory may be full!", - ), - chat_type: comp::ChatType::CommandError, - })) - }, + InventoryUpdateEvent::CollectFailed => {}, _ => { - if let InventoryUpdateEvent::Collected(item) = event { - frontend_events.push(Event::Chat(comp::ChatMsg { - message: format!("Picked up {}", item.name()), - chat_type: comp::ChatType::Meta, - })); - } - + // Push the updated inventory component to the client self.state.write_component(self.entity, inventory); }, } + + frontend_events.push(Event::InventoryUpdated(event)); }, ServerMsg::TerrainChunkUpdate { key, chunk } => { if let Ok(chunk) = chunk { diff --git a/common/src/event.rs b/common/src/event.rs index 92bd244411..47a6e34df8 100644 --- a/common/src/event.rs +++ b/common/src/event.rs @@ -1,95 +1,10 @@ -use crate::{ - comp, - comp::item::{Consumable, ItemKind}, - sync::Uid, - util::Dir, -}; -use comp::{item::ToolCategory, CharacterAbilityType, InventoryUpdateEvent, Item}; +use crate::{comp, sync::Uid, util::Dir}; +use comp::item::Item; use parking_lot::Mutex; -use serde::Deserialize; use specs::Entity as EcsEntity; -use std::{collections::VecDeque, convert::TryFrom, ops::DerefMut}; +use std::{collections::VecDeque, ops::DerefMut}; use vek::*; -pub struct SfxEventItem { - pub sfx: SfxEvent, - pub pos: Option>, - pub vol: Option, -} - -impl SfxEventItem { - pub fn new(sfx: SfxEvent, pos: Option>, vol: Option) -> Self { - Self { sfx, pos, vol } - } - - pub fn at_player_position(sfx: SfxEvent) -> Self { - Self { - sfx, - pos: None, - vol: None, - } - } -} - -#[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 { /// Applies upward force to entity's `Vel` Jump(EcsEntity), diff --git a/common/src/state.rs b/common/src/state.rs index 167e75c0d1..ce8834a179 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -1,6 +1,6 @@ use crate::{ comp, - event::{EventBus, LocalEvent, ServerEvent, SfxEventItem}, + event::{EventBus, LocalEvent, ServerEvent}, region::RegionMap, sync::WorldSyncExt, sys, @@ -170,7 +170,6 @@ impl State { // TODO: only register on the server ecs.insert(EventBus::::default()); ecs.insert(EventBus::::default()); - ecs.insert(EventBus::::default()); ecs.insert(RegionMap::new()); ecs diff --git a/voxygen/src/audio/sfx/event_mapper/combat/mod.rs b/voxygen/src/audio/sfx/event_mapper/combat/mod.rs index 1898d6dd8e..6126ebc36d 100644 --- a/voxygen/src/audio/sfx/event_mapper/combat/mod.rs +++ b/voxygen/src/audio/sfx/event_mapper/combat/mod.rs @@ -1,6 +1,6 @@ /// EventMapper::Combat watches the combat states of surrounding entities' and /// emits sfx related to weapons and attacks/abilities -use crate::audio::sfx::{SfxTriggerItem, SfxTriggers, SFX_DIST_LIMIT_SQR}; +use crate::audio::sfx::{SfxEvent, SfxEventItem, SfxTriggerItem, SfxTriggers, SFX_DIST_LIMIT_SQR}; use super::EventMapper; @@ -9,7 +9,7 @@ use common::{ item::{Item, ItemKind, ToolCategory}, CharacterAbilityType, CharacterState, ItemConfig, Loadout, Pos, }, - event::{EventBus, SfxEvent, SfxEventItem}, + event::EventBus, state::State, }; use hashbrown::HashMap; diff --git a/voxygen/src/audio/sfx/event_mapper/combat/tests.rs b/voxygen/src/audio/sfx/event_mapper/combat/tests.rs index baaa66d8ad..71fdc1097a 100644 --- a/voxygen/src/audio/sfx/event_mapper/combat/tests.rs +++ b/voxygen/src/audio/sfx/event_mapper/combat/tests.rs @@ -1,8 +1,8 @@ use super::*; +use crate::audio::sfx::SfxEvent; use common::{ assets, comp::{item::tool::ToolCategory, CharacterAbilityType, CharacterState, ItemConfig, Loadout}, - event::SfxEvent, states, }; use std::time::{Duration, Instant}; diff --git a/voxygen/src/audio/sfx/event_mapper/movement/mod.rs b/voxygen/src/audio/sfx/event_mapper/movement/mod.rs index 8a5e0b74ff..fd4263ed1b 100644 --- a/voxygen/src/audio/sfx/event_mapper/movement/mod.rs +++ b/voxygen/src/audio/sfx/event_mapper/movement/mod.rs @@ -2,11 +2,10 @@ /// and triggers sfx related to running, climbing and gliding, at a volume /// proportionate to the extity's size use super::EventMapper; - -use crate::audio::sfx::{SfxTriggerItem, SfxTriggers, SFX_DIST_LIMIT_SQR}; +use crate::audio::sfx::{SfxEvent, SfxEventItem, SfxTriggerItem, SfxTriggers, SFX_DIST_LIMIT_SQR}; use common::{ comp::{Body, CharacterState, PhysicsState, Pos, Vel}, - event::{EventBus, SfxEvent, SfxEventItem}, + event::EventBus, state::State, }; use hashbrown::HashMap; diff --git a/voxygen/src/audio/sfx/event_mapper/movement/tests.rs b/voxygen/src/audio/sfx/event_mapper/movement/tests.rs index 836973b645..f407231ab6 100644 --- a/voxygen/src/audio/sfx/event_mapper/movement/tests.rs +++ b/voxygen/src/audio/sfx/event_mapper/movement/tests.rs @@ -1,9 +1,9 @@ use super::*; +use crate::audio::sfx::SfxEvent; use common::{ comp::{ bird_small, humanoid, quadruped_medium, quadruped_small, Body, CharacterState, PhysicsState, }, - event::SfxEvent, states, }; use std::time::{Duration, Instant}; diff --git a/voxygen/src/audio/sfx/event_mapper/progression/mod.rs b/voxygen/src/audio/sfx/event_mapper/progression/mod.rs index 6126a78cb7..0b845e03a9 100644 --- a/voxygen/src/audio/sfx/event_mapper/progression/mod.rs +++ b/voxygen/src/audio/sfx/event_mapper/progression/mod.rs @@ -2,13 +2,9 @@ /// and triggers sfx for gaining experience and levelling up use super::EventMapper; -use crate::audio::sfx::SfxTriggers; +use crate::audio::sfx::{SfxEvent, SfxEventItem, SfxTriggers}; -use common::{ - comp::Stats, - event::{EventBus, SfxEvent, SfxEventItem}, - state::State, -}; +use common::{comp::Stats, event::EventBus, state::State}; use specs::WorldExt; #[derive(Clone, PartialEq)] diff --git a/voxygen/src/audio/sfx/event_mapper/progression/tests.rs b/voxygen/src/audio/sfx/event_mapper/progression/tests.rs index b56dcf2bc2..dd7778f42f 100644 --- a/voxygen/src/audio/sfx/event_mapper/progression/tests.rs +++ b/voxygen/src/audio/sfx/event_mapper/progression/tests.rs @@ -1,5 +1,5 @@ use super::*; -use common::event::SfxEvent; +use crate::audio::sfx::SfxEvent; #[test] fn no_change_returns_none() { diff --git a/voxygen/src/audio/sfx/mod.rs b/voxygen/src/audio/sfx/mod.rs index 63f5ef9619..0cdfebbbe3 100644 --- a/voxygen/src/audio/sfx/mod.rs +++ b/voxygen/src/audio/sfx/mod.rs @@ -84,16 +84,21 @@ mod event_mapper; use crate::audio::AudioFrontend; + use common::{ assets, - comp::{Ori, Pos}, - event::{EventBus, SfxEvent, SfxEventItem}, + comp::{ + item::{Consumable, ItemKind, ToolCategory}, + CharacterAbilityType, InventoryUpdateEvent, Ori, Pos, + }, + event::EventBus, state::State, }; use event_mapper::SfxEventMapper; use hashbrown::HashMap; use serde::Deserialize; use specs::WorldExt; +use std::convert::TryFrom; use tracing::warn; use vek::*; @@ -104,6 +109,85 @@ use vek::*; /// player. const SFX_DIST_LIMIT_SQR: f32 = 20000.0; +pub struct SfxEventItem { + pub sfx: SfxEvent, + pub pos: Option>, + pub vol: Option, +} + +impl SfxEventItem { + pub fn new(sfx: SfxEvent, pos: Option>, vol: Option) -> Self { + Self { sfx, pos, vol } + } + + pub fn at_player_position(sfx: SfxEvent) -> Self { + Self { + sfx, + pos: None, + vol: None, + } + } +} + +#[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), + } + } +} + #[derive(Deserialize)] pub struct SfxTriggerItem { pub files: Vec, diff --git a/voxygen/src/ecs/mod.rs b/voxygen/src/ecs/mod.rs index 779bcbc2b6..8e7b4cf08e 100644 --- a/voxygen/src/ecs/mod.rs +++ b/voxygen/src/ecs/mod.rs @@ -1,6 +1,8 @@ pub mod comp; pub mod sys; +use crate::audio::sfx::SfxEventItem; +use common::event::EventBus; use specs::{Entity, World, WorldExt}; #[derive(Copy, Clone, Debug)] @@ -25,4 +27,7 @@ pub fn init(world: &mut World) { world.register::(); world.register::(); world.insert(MyExpFloaterList::default()); + + // Voxygen event buses + world.insert(EventBus::::default()); } diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 4b19d39795..1df6d69822 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -1,4 +1,5 @@ use crate::{ + audio::sfx::{SfxEvent, SfxEventItem}, ecs::MyEntity, hud::{DebugInfo, Event as HudEvent, Hud, HudInfo, PressBehavior}, i18n::{i18n_asset_key, VoxygenLocalization}, @@ -14,7 +15,8 @@ use common::{ assets::{load_watched, watch}, clock::Clock, comp, - comp::{Pos, Vel, MAX_PICKUP_RANGE_SQR}, + comp::{ChatMsg, ChatType, InventoryUpdateEvent, Pos, Vel, MAX_PICKUP_RANGE_SQR}, + event::EventBus, msg::ClientState, terrain::{Block, BlockKind}, util::Dir, @@ -83,6 +85,32 @@ impl SessionState { client::Event::Chat(m) => { self.hud.new_message(m); }, + client::Event::InventoryUpdated(inv_event) => { + let sfx_event = SfxEvent::from(&inv_event); + client + .state() + .ecs() + .read_resource::>() + .emit_now(SfxEventItem::at_player_position(sfx_event)); + + match inv_event { + InventoryUpdateEvent::CollectFailed => { + self.hud.new_message(ChatMsg { + message: String::from( + "Failed to collect item. Your inventory may be full!", + ), + chat_type: ChatType::CommandError, + }); + }, + InventoryUpdateEvent::Collected(item) => { + self.hud.new_message(ChatMsg { + message: format!("Picked up {}", item.name()), + chat_type: ChatType::CommandInfo, + }); + }, + _ => {}, + }; + }, client::Event::Disconnect => return Ok(TickAction::Disconnect), client::Event::DisconnectionNotification(time) => { let message = match time { @@ -90,8 +118,8 @@ impl SessionState { _ => format!("Connection lost. Kicking in {} seconds", time), }; - self.hud.new_message(comp::ChatMsg { - chat_type: comp::ChatType::CommandError, + self.hud.new_message(ChatMsg { + chat_type: ChatType::CommandError, message, }); },