From 9ab2833056f57972bb02ca2bdd88d2e52150505f Mon Sep 17 00:00:00 2001 From: S Handley Date: Fri, 21 Feb 2020 02:56:54 +0000 Subject: [PATCH] Add weapon wield/unwield sfx support --- CHANGELOG.md | 1 + assets/voxygen/audio/sfx.ron | 12 + assets/voxygen/audio/sfx/weapon/sword_in.wav | 3 + assets/voxygen/audio/sfx/weapon/sword_out.wav | 3 + common/src/event.rs | 11 +- .../src/audio/sfx/event_mapper/movement.rs | 399 ----------------- .../audio/sfx/event_mapper/movement/mod.rs | 207 +++++++++ .../audio/sfx/event_mapper/movement/tests.rs | 419 ++++++++++++++++++ 8 files changed, 647 insertions(+), 408 deletions(-) create mode 100644 assets/voxygen/audio/sfx/weapon/sword_in.wav create mode 100644 assets/voxygen/audio/sfx/weapon/sword_out.wav delete mode 100644 voxygen/src/audio/sfx/event_mapper/movement.rs create mode 100644 voxygen/src/audio/sfx/event_mapper/movement/mod.rs create mode 100644 voxygen/src/audio/sfx/event_mapper/movement/tests.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index f1dc7924ad..086125408c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Tanslation status tracking - Added gamma setting - Added new orc hairstyles +- Added sfx for wielding/unwielding weapons ### Changed diff --git a/assets/voxygen/audio/sfx.ron b/assets/voxygen/audio/sfx.ron index 8310f26c09..38349f287e 100644 --- a/assets/voxygen/audio/sfx.ron +++ b/assets/voxygen/audio/sfx.ron @@ -23,5 +23,17 @@ ], threshold: 0.5, ), + Wield(Sword): ( + files: [ + "voxygen.audio.sfx.weapon.sword_out", + ], + threshold: 0.5, + ), + Unwield(Sword): ( + files: [ + "voxygen.audio.sfx.weapon.sword_in", + ], + threshold: 0.5, + ), } ) \ No newline at end of file diff --git a/assets/voxygen/audio/sfx/weapon/sword_in.wav b/assets/voxygen/audio/sfx/weapon/sword_in.wav new file mode 100644 index 0000000000..eb5a2ea8fa --- /dev/null +++ b/assets/voxygen/audio/sfx/weapon/sword_in.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2c3c0a27e129f9d7274e332b30aa59ac8d78401587f582c9d2fa2a8f06b8c292 +size 87596 diff --git a/assets/voxygen/audio/sfx/weapon/sword_out.wav b/assets/voxygen/audio/sfx/weapon/sword_out.wav new file mode 100644 index 0000000000..f16fdbf6b5 --- /dev/null +++ b/assets/voxygen/audio/sfx/weapon/sword_out.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c882be88cd3f2528ba167054b1a98ea5386f420d96dc8ab9340a2adf1b02e7cc +size 64556 diff --git a/common/src/event.rs b/common/src/event.rs index 919ca04c45..d06f912a7b 100644 --- a/common/src/event.rs +++ b/common/src/event.rs @@ -20,11 +20,6 @@ impl SfxEventItem { #[derive(Copy, Clone, Debug, PartialEq, Deserialize, Hash, Eq)] pub enum SfxEvent { Idle, - PlaceBlock, - RemoveBlock, - OpenChest, - ChatTellReceived, - OpenBag, Run, Roll, Climb, @@ -36,10 +31,8 @@ pub enum SfxEvent { Fall, ExperienceGained, LevelUp, - LightLantern, - ExtinguishLantern, - Attack(Tool), - AttackWolf, + Wield(Tool), + Unwield(Tool), } pub enum LocalEvent { diff --git a/voxygen/src/audio/sfx/event_mapper/movement.rs b/voxygen/src/audio/sfx/event_mapper/movement.rs deleted file mode 100644 index 4a1c96934b..0000000000 --- a/voxygen/src/audio/sfx/event_mapper/movement.rs +++ /dev/null @@ -1,399 +0,0 @@ -/// event_mapper::movement watches all local entities movements and determines -/// which sfx to emit, and the position at which the sound should be emitted -/// from -use crate::audio::sfx::{SfxTriggerItem, SfxTriggers}; - -use client::Client; -use common::{ - comp::{ActionState, Body, CharacterState, MovementState, Pos, Vel}, - event::{EventBus, SfxEvent, SfxEventItem}, -}; -use hashbrown::HashMap; -use specs::{Entity as EcsEntity, Join, WorldExt}; -use std::time::{Duration, Instant}; -use vek::*; - -#[derive(Clone)] -struct LastSfxEvent { - event: SfxEvent, - time: Instant, -} - -pub struct MovementEventMapper { - event_history: HashMap, -} - -impl MovementEventMapper { - pub fn new() -> Self { - Self { - event_history: HashMap::new(), - } - } - - pub fn maintain(&mut self, client: &Client, triggers: &SfxTriggers) { - const SFX_DIST_LIMIT_SQR: f32 = 22500.0; - let ecs = client.state().ecs(); - - let player_position = ecs - .read_storage::() - .get(client.entity()) - .map_or(Vec3::zero(), |pos| pos.0); - - for (entity, pos, vel, body, character) in ( - &ecs.entities(), - &ecs.read_storage::(), - &ecs.read_storage::(), - &ecs.read_storage::(), - ecs.read_storage::().maybe(), - ) - .join() - .filter(|(_, e_pos, ..)| { - (e_pos.0.distance_squared(player_position)) < SFX_DIST_LIMIT_SQR - }) - { - if let Some(character) = character { - let state = self - .event_history - .entry(entity) - .or_insert_with(|| LastSfxEvent { - event: SfxEvent::Idle, - time: Instant::now(), - }); - - let mapped_event = match body { - Body::Humanoid(_) => { - Self::map_movement_event(character, state.event.clone(), vel.0) - }, - Body::QuadrupedMedium(_) => { - // TODO: Quadriped running sfx - SfxEvent::Idle - }, - _ => SfxEvent::Idle, - }; - - // Check for SFX config entry for this movement - if Self::should_emit(state, triggers.get_key_value(&mapped_event)) { - ecs.read_resource::>() - .emitter() - .emit(SfxEventItem::new(mapped_event, Some(pos.0))); - - // Update the last play time - state.event = mapped_event; - state.time = Instant::now(); - } else { - // Keep the last event, it may not have an SFX trigger but it helps us determine - // the next one - state.event = mapped_event; - } - } - } - - self.cleanup(client.entity()); - } - - /// As the player explores the world, we track the last event of the nearby - /// entities to determine the correct SFX item to play next based on - /// their activity. `cleanup` will remove entities from event tracking if - /// they have not triggered an event for > n seconds. This prevents - /// stale records from bloating the Map size. - fn cleanup(&mut self, player: EcsEntity) { - const TRACKING_TIMEOUT: u64 = 15; - - let now = Instant::now(); - self.event_history.retain(|entity, event| { - now.duration_since(event.time) < Duration::from_secs(TRACKING_TIMEOUT) - || entity.id() == player.id() - }); - } - - /// When specific entity movements are detected, the associated sound (if - /// any) needs to satisfy two conditions to be allowed to play: - /// 1. An sfx.ron entry exists for the movement (we need to know which sound - /// file(s) to play) 2. The sfx has not been played since it's timeout - /// threshold has elapsed, which prevents firing every tick - fn should_emit( - last_play_entry: &LastSfxEvent, - sfx_trigger_item: Option<(&SfxEvent, &SfxTriggerItem)>, - ) -> bool { - if let Some((event, item)) = sfx_trigger_item { - if &last_play_entry.event == event { - last_play_entry.time.elapsed().as_secs_f64() >= item.threshold - } else { - true - } - } else { - false - } - } - - /// Voxygen has an existing list of character states via `MovementState::*` - /// and `ActionState::*` however that list does not provide enough - /// resolution to target specific entity events, such as opening or - /// closing the glider. These methods translate those entity states with - /// some additional data into more specific `SfxEvent`'s which we attach - /// sounds to - fn map_movement_event( - current_event: &CharacterState, - previous_event: SfxEvent, - vel: Vec3, - ) -> SfxEvent { - match (current_event.movement, current_event.action, previous_event) { - (_, ActionState::Roll { .. }, _) => SfxEvent::Roll, - (MovementState::Climb, ..) => SfxEvent::Climb, - (MovementState::Swim, ..) => SfxEvent::Swim, - (MovementState::Run, ..) => { - // If the entitys's velocity is very low, they may be stuck, or walking into a - // solid object. We should not trigger the run SFX in this case, - // even if their move state indicates running. The 0.1 value is - // an approximation from playtesting scenarios where this can occur. - if vel.magnitude() > 0.1 { - SfxEvent::Run - } else { - SfxEvent::Idle - } - }, - (MovementState::Jump, ..) => SfxEvent::Jump, - (MovementState::Fall, _, SfxEvent::Glide) => SfxEvent::GliderClose, - (MovementState::Stand, _, SfxEvent::Fall) => SfxEvent::Run, - (MovementState::Fall, _, SfxEvent::Jump) => SfxEvent::Idle, - (MovementState::Fall, _, _) => SfxEvent::Fall, - (MovementState::Glide, _, previous_event) => { - if previous_event != SfxEvent::GliderOpen && previous_event != SfxEvent::Glide { - SfxEvent::GliderOpen - } else { - SfxEvent::Glide - } - }, - (MovementState::Stand, _, SfxEvent::Glide) => SfxEvent::GliderClose, - _ => SfxEvent::Idle, - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use common::{ - comp::{ActionState, MovementState}, - event::SfxEvent, - }; - use std::time::{Duration, Instant}; - - #[test] - fn no_item_config_no_emit() { - let last_sfx_event = LastSfxEvent { - event: SfxEvent::Idle, - time: Instant::now(), - }; - - let result = MovementEventMapper::should_emit(&last_sfx_event, None); - - assert_eq!(result, false); - } - - #[test] - fn config_but_played_since_threshold_no_emit() { - let event = SfxEvent::Run; - - let trigger_item = SfxTriggerItem { - files: vec![String::from("some.path.to.sfx.file")], - threshold: 1.0, - }; - - // Triggered a 'Run' 0 seconds ago - let last_sfx_event = LastSfxEvent { - event: SfxEvent::Run, - time: Instant::now(), - }; - - let result = - MovementEventMapper::should_emit(&last_sfx_event, Some((&event, &trigger_item))); - - assert_eq!(result, false); - } - - #[test] - fn config_and_not_played_since_threshold_emits() { - let event = SfxEvent::Run; - - let trigger_item = SfxTriggerItem { - files: vec![String::from("some.path.to.sfx.file")], - threshold: 0.5, - }; - - let last_sfx_event = LastSfxEvent { - event: SfxEvent::Idle, - time: Instant::now().checked_add(Duration::from_secs(1)).unwrap(), - }; - - let result = - MovementEventMapper::should_emit(&last_sfx_event, Some((&event, &trigger_item))); - - assert_eq!(result, true); - } - - #[test] - fn same_previous_event_elapsed_emits() { - let event = SfxEvent::Run; - - let trigger_item = SfxTriggerItem { - files: vec![String::from("some.path.to.sfx.file")], - threshold: 0.5, - }; - - let last_sfx_event = LastSfxEvent { - event: SfxEvent::Run, - time: Instant::now() - .checked_sub(Duration::from_millis(500)) - .unwrap(), - }; - - let result = - MovementEventMapper::should_emit(&last_sfx_event, Some((&event, &trigger_item))); - - assert_eq!(result, true); - } - - #[test] - fn maps_idle() { - let result = MovementEventMapper::map_movement_event( - &CharacterState { - movement: MovementState::Stand, - action: ActionState::Idle, - }, - SfxEvent::Idle, - Vec3::zero(), - ); - - assert_eq!(result, SfxEvent::Idle); - } - - #[test] - fn maps_run_with_sufficient_velocity() { - let result = MovementEventMapper::map_movement_event( - &CharacterState { - movement: MovementState::Run, - action: ActionState::Idle, - }, - SfxEvent::Idle, - Vec3::new(0.5, 0.8, 0.0), - ); - - assert_eq!(result, SfxEvent::Run); - } - - #[test] - fn does_not_map_run_with_insufficient_velocity() { - let result = MovementEventMapper::map_movement_event( - &CharacterState { - movement: MovementState::Run, - action: ActionState::Idle, - }, - SfxEvent::Idle, - Vec3::new(0.02, 0.0001, 0.0), - ); - - assert_eq!(result, SfxEvent::Idle); - } - - #[test] - fn maps_roll() { - let result = MovementEventMapper::map_movement_event( - &CharacterState { - action: ActionState::Roll { - time_left: Duration::new(1, 0), - was_wielding: false, - }, - movement: MovementState::Run, - }, - SfxEvent::Run, - Vec3::zero(), - ); - - assert_eq!(result, SfxEvent::Roll); - } - - #[test] - fn maps_fall() { - let result = MovementEventMapper::map_movement_event( - &CharacterState { - movement: MovementState::Fall, - action: ActionState::Idle, - }, - SfxEvent::Fall, - Vec3::zero(), - ); - - assert_eq!(result, SfxEvent::Fall); - } - - #[test] - fn maps_land_on_ground_to_run() { - let result = MovementEventMapper::map_movement_event( - &CharacterState { - movement: MovementState::Stand, - action: ActionState::Idle, - }, - SfxEvent::Fall, - Vec3::zero(), - ); - - assert_eq!(result, SfxEvent::Run); - } - - #[test] - fn maps_glider_open() { - let result = MovementEventMapper::map_movement_event( - &CharacterState { - movement: MovementState::Glide, - action: ActionState::Idle, - }, - SfxEvent::Jump, - Vec3::zero(), - ); - - assert_eq!(result, SfxEvent::GliderOpen); - } - - #[test] - fn maps_glide() { - let result = MovementEventMapper::map_movement_event( - &CharacterState { - movement: MovementState::Glide, - action: ActionState::Idle, - }, - SfxEvent::Glide, - Vec3::zero(), - ); - - assert_eq!(result, SfxEvent::Glide); - } - - #[test] - fn maps_glider_close_when_closing_mid_flight() { - let result = MovementEventMapper::map_movement_event( - &CharacterState { - movement: MovementState::Fall, - action: ActionState::Idle, - }, - SfxEvent::Glide, - Vec3::zero(), - ); - - assert_eq!(result, SfxEvent::GliderClose); - } - - #[test] - fn maps_glider_close_when_landing() { - let result = MovementEventMapper::map_movement_event( - &CharacterState { - movement: MovementState::Stand, - action: ActionState::Idle, - }, - SfxEvent::Glide, - Vec3::zero(), - ); - - assert_eq!(result, SfxEvent::GliderClose); - } -} diff --git a/voxygen/src/audio/sfx/event_mapper/movement/mod.rs b/voxygen/src/audio/sfx/event_mapper/movement/mod.rs new file mode 100644 index 0000000000..8c4581c00c --- /dev/null +++ b/voxygen/src/audio/sfx/event_mapper/movement/mod.rs @@ -0,0 +1,207 @@ +/// event_mapper::movement watches all local entities movements and determines +/// which sfx to emit, and the position at which the sound should be emitted +/// from +use crate::audio::sfx::{SfxTriggerItem, SfxTriggers}; + +use client::Client; +use common::{ + comp::{ActionState, Body, CharacterState, Item, ItemKind, MovementState, Pos, Stats, Vel}, + event::{EventBus, SfxEvent, SfxEventItem}, +}; +use hashbrown::HashMap; +use specs::{Entity as EcsEntity, Join, WorldExt}; +use std::time::{Duration, Instant}; +use vek::*; + +#[derive(Clone)] +struct LastSfxEvent { + event: SfxEvent, + weapon_drawn: bool, + time: Instant, +} + +pub struct MovementEventMapper { + event_history: HashMap, +} + +impl MovementEventMapper { + pub fn new() -> Self { + Self { + event_history: HashMap::new(), + } + } + + pub fn maintain(&mut self, client: &Client, triggers: &SfxTriggers) { + const SFX_DIST_LIMIT_SQR: f32 = 22500.0; + let ecs = client.state().ecs(); + + let player_position = ecs + .read_storage::() + .get(client.entity()) + .map_or(Vec3::zero(), |pos| pos.0); + + for (entity, pos, vel, body, stats, character) in ( + &ecs.entities(), + &ecs.read_storage::(), + &ecs.read_storage::(), + &ecs.read_storage::(), + &ecs.read_storage::(), + ecs.read_storage::().maybe(), + ) + .join() + .filter(|(_, e_pos, ..)| { + (e_pos.0.distance_squared(player_position)) < SFX_DIST_LIMIT_SQR + }) + { + if let Some(character) = character { + let state = self + .event_history + .entry(entity) + .or_insert_with(|| LastSfxEvent { + event: SfxEvent::Idle, + weapon_drawn: false, + time: Instant::now(), + }); + + let mapped_event = match body { + Body::Humanoid(_) => Self::map_movement_event(character, state, vel.0, stats), + Body::QuadrupedMedium(_) => { + // TODO: Quadriped running sfx + SfxEvent::Idle + }, + _ => SfxEvent::Idle, + }; + + // Check for SFX config entry for this movement + if Self::should_emit(state, triggers.get_key_value(&mapped_event)) { + ecs.read_resource::>() + .emitter() + .emit(SfxEventItem::new(mapped_event, Some(pos.0))); + + // Update the last play time + state.event = mapped_event; + state.time = Instant::now(); + state.weapon_drawn = Self::has_weapon_drawn(character.action); + } else { + // Keep the last event, it may not have an SFX trigger but it helps us determine + // the next one + state.event = mapped_event; + } + } + } + + self.cleanup(client.entity()); + } + + /// As the player explores the world, we track the last event of the nearby + /// entities to determine the correct SFX item to play next based on + /// their activity. `cleanup` will remove entities from event tracking if + /// they have not triggered an event for > n seconds. This prevents + /// stale records from bloating the Map size. + fn cleanup(&mut self, player: EcsEntity) { + const TRACKING_TIMEOUT: u64 = 15; + + let now = Instant::now(); + self.event_history.retain(|entity, event| { + now.duration_since(event.time) < Duration::from_secs(TRACKING_TIMEOUT) + || entity.id() == player.id() + }); + } + + /// When specific entity movements are detected, the associated sound (if + /// any) needs to satisfy two conditions to be allowed to play: + /// 1. An sfx.ron entry exists for the movement (we need to know which sound + /// file(s) to play) 2. The sfx has not been played since it's timeout + /// threshold has elapsed, which prevents firing every tick + fn should_emit( + last_play_entry: &LastSfxEvent, + sfx_trigger_item: Option<(&SfxEvent, &SfxTriggerItem)>, + ) -> bool { + if let Some((event, item)) = sfx_trigger_item { + if &last_play_entry.event == event { + last_play_entry.time.elapsed().as_secs_f64() >= item.threshold + } else { + true + } + } else { + false + } + } + + /// Voxygen has an existing list of character states via `MovementState::*` + /// and `ActionState::*` however that list does not provide enough + /// resolution to target specific entity events, such as opening or + /// closing the glider. These methods translate those entity states with + /// some additional data into more specific `SfxEvent`'s which we attach + /// sounds to + fn map_movement_event( + current_event: &CharacterState, + previous_event: &LastSfxEvent, + vel: Vec3, + stats: &Stats, + ) -> SfxEvent { + // Handle any weapon wielding changes up front. Doing so here first simplifies + // handling the movement/action state later, since they don't require querying + // stats or previous wield state. + if let Some(Item { + kind: ItemKind::Tool { kind, .. }, + .. + }) = stats.equipment.main + { + if let Some(wield_event) = match ( + previous_event.weapon_drawn, + Self::has_weapon_drawn(current_event.action), + ) { + (false, true) => Some(SfxEvent::Wield(kind)), + (true, false) => Some(SfxEvent::Unwield(kind)), + _ => None, + } { + return wield_event; + } + } + + // Match all other Movemement and Action states + match ( + current_event.movement, + current_event.action, + previous_event.event, + ) { + (_, ActionState::Roll { .. }, _) => SfxEvent::Roll, + (MovementState::Climb, ..) => SfxEvent::Climb, + (MovementState::Swim, ..) => SfxEvent::Swim, + (MovementState::Run, ..) => { + // If the entitys's velocity is very low, they may be stuck, or walking into a + // solid object. We should not trigger the run SFX in this case, + // even if their move state indicates running. The 0.1 value is + // an approximation from playtesting scenarios where this can occur. + if vel.magnitude() > 0.1 { + SfxEvent::Run + } else { + SfxEvent::Idle + } + }, + (MovementState::Jump, ..) => SfxEvent::Jump, + (MovementState::Fall, _, SfxEvent::Glide) => SfxEvent::GliderClose, + (MovementState::Stand, _, SfxEvent::Fall) => SfxEvent::Run, + (MovementState::Fall, _, SfxEvent::Jump) => SfxEvent::Idle, + (MovementState::Fall, _, _) => SfxEvent::Fall, + (MovementState::Glide, _, previous_event) => { + if previous_event != SfxEvent::GliderOpen && previous_event != SfxEvent::Glide { + SfxEvent::GliderOpen + } else { + SfxEvent::Glide + } + }, + (MovementState::Stand, _, SfxEvent::Glide) => SfxEvent::GliderClose, + _ => SfxEvent::Idle, + } + } + + /// Returns true for any state where the player has their weapon drawn. This + /// helps us manage the wield/unwield sfx events + fn has_weapon_drawn(state: ActionState) -> bool { + state.is_wield() | state.is_attack() | state.is_block() | state.is_charge() + } +} + +#[cfg(test)] mod tests; diff --git a/voxygen/src/audio/sfx/event_mapper/movement/tests.rs b/voxygen/src/audio/sfx/event_mapper/movement/tests.rs new file mode 100644 index 0000000000..210f7fe2c1 --- /dev/null +++ b/voxygen/src/audio/sfx/event_mapper/movement/tests.rs @@ -0,0 +1,419 @@ +use super::*; +use common::{ + assets, + comp::{humanoid, item::Tool, ActionState, Body, MovementState, Stats}, + event::SfxEvent, +}; +use std::time::{Duration, Instant}; + +#[test] +fn no_item_config_no_emit() { + let last_sfx_event = LastSfxEvent { + event: SfxEvent::Idle, + weapon_drawn: false, + time: Instant::now(), + }; + + let result = MovementEventMapper::should_emit(&last_sfx_event, None); + + assert_eq!(result, false); +} + +#[test] +fn config_but_played_since_threshold_no_emit() { + let event = SfxEvent::Run; + + let trigger_item = SfxTriggerItem { + files: vec![String::from("some.path.to.sfx.file")], + threshold: 1.0, + }; + + // Triggered a 'Run' 0 seconds ago + let last_sfx_event = LastSfxEvent { + event: SfxEvent::Run, + weapon_drawn: false, + time: Instant::now(), + }; + + let result = MovementEventMapper::should_emit(&last_sfx_event, Some((&event, &trigger_item))); + + assert_eq!(result, false); +} + +#[test] +fn config_and_not_played_since_threshold_emits() { + let event = SfxEvent::Run; + + let trigger_item = SfxTriggerItem { + files: vec![String::from("some.path.to.sfx.file")], + threshold: 0.5, + }; + + let last_sfx_event = LastSfxEvent { + event: SfxEvent::Idle, + weapon_drawn: false, + time: Instant::now().checked_add(Duration::from_secs(1)).unwrap(), + }; + + let result = MovementEventMapper::should_emit(&last_sfx_event, Some((&event, &trigger_item))); + + assert_eq!(result, true); +} + +#[test] +fn same_previous_event_elapsed_emits() { + let event = SfxEvent::Run; + + let trigger_item = SfxTriggerItem { + files: vec![String::from("some.path.to.sfx.file")], + threshold: 0.5, + }; + + let last_sfx_event = LastSfxEvent { + event: SfxEvent::Run, + weapon_drawn: false, + time: Instant::now() + .checked_sub(Duration::from_millis(500)) + .unwrap(), + }; + + let result = MovementEventMapper::should_emit(&last_sfx_event, Some((&event, &trigger_item))); + + assert_eq!(result, true); +} + +#[test] +fn maps_idle() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Stand, + action: ActionState::Idle, + }, + &LastSfxEvent { + event: SfxEvent::Idle, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::zero(), + &stats, + ); + + assert_eq!(result, SfxEvent::Idle); +} + +#[test] +fn maps_run_with_sufficient_velocity() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Run, + action: ActionState::Idle, + }, + &LastSfxEvent { + event: SfxEvent::Idle, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::new(0.5, 0.8, 0.0), + &stats, + ); + + assert_eq!(result, SfxEvent::Run); +} + +#[test] +fn does_not_map_run_with_insufficient_velocity() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Run, + action: ActionState::Idle, + }, + &LastSfxEvent { + event: SfxEvent::Idle, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::new(0.02, 0.0001, 0.0), + &stats, + ); + + assert_eq!(result, SfxEvent::Idle); +} + +#[test] +fn maps_roll() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + action: ActionState::Roll { + time_left: Duration::new(1, 0), + was_wielding: false, + }, + movement: MovementState::Run, + }, + &LastSfxEvent { + event: SfxEvent::Run, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::zero(), + &stats, + ); + + assert_eq!(result, SfxEvent::Roll); +} + +#[test] +fn maps_fall() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Fall, + action: ActionState::Idle, + }, + &LastSfxEvent { + event: SfxEvent::Fall, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::zero(), + &stats, + ); + + assert_eq!(result, SfxEvent::Fall); +} + +#[test] +fn maps_land_on_ground_to_run() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Stand, + action: ActionState::Idle, + }, + &LastSfxEvent { + event: SfxEvent::Fall, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::zero(), + &stats, + ); + + assert_eq!(result, SfxEvent::Run); +} + +#[test] +fn maps_glider_open() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Glide, + action: ActionState::Idle, + }, + &LastSfxEvent { + event: SfxEvent::Jump, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::zero(), + &stats, + ); + + assert_eq!(result, SfxEvent::GliderOpen); +} + +#[test] +fn maps_glide() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Glide, + action: ActionState::Idle, + }, + &LastSfxEvent { + event: SfxEvent::Glide, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::zero(), + &stats, + ); + + assert_eq!(result, SfxEvent::Glide); +} + +#[test] +fn maps_glider_close_when_closing_mid_flight() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Fall, + action: ActionState::Idle, + }, + &LastSfxEvent { + event: SfxEvent::Glide, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::zero(), + &stats, + ); + + assert_eq!(result, SfxEvent::GliderClose); +} + +#[test] +fn maps_glider_close_when_landing() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Stand, + action: ActionState::Idle, + }, + &LastSfxEvent { + event: SfxEvent::Glide, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::zero(), + &stats, + ); + + assert_eq!(result, SfxEvent::GliderClose); +} + +#[test] +fn maps_wield() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + Some(assets::load_expect_cloned( + "common.items.weapons.starter_sword", + )), + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Stand, + action: ActionState::Wield { + time_left: Duration::from_millis(800), + }, + }, + &LastSfxEvent { + event: SfxEvent::Idle, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::zero(), + &stats, + ); + + assert_eq!(result, SfxEvent::Wield(Tool::Sword)); +} + +#[test] +fn maps_unwield() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + Some(assets::load_expect_cloned( + "common.items.weapons.starter_axe", + )), + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Stand, + action: ActionState::Idle, + }, + &LastSfxEvent { + event: SfxEvent::Idle, + weapon_drawn: true, + time: Instant::now(), + }, + Vec3::zero(), + &stats, + ); + + assert_eq!(result, SfxEvent::Unwield(Tool::Axe)); +} + +#[test] +fn does_not_map_wield_when_no_main_weapon() { + let stats = Stats::new( + String::from("test"), + Body::Humanoid(humanoid::Body::random()), + None, + ); + + let result = MovementEventMapper::map_movement_event( + &CharacterState { + movement: MovementState::Run, + action: ActionState::Wield { + time_left: Duration::from_millis(600), + }, + }, + &LastSfxEvent { + event: SfxEvent::Idle, + weapon_drawn: false, + time: Instant::now(), + }, + Vec3::new(0.5, 0.8, 0.0), + &stats, + ); + + assert_eq!(result, SfxEvent::Run); +}