Made level up sounds use outcomes instead of emitting an event

This commit is contained in:
jiminycrick 2020-11-14 00:23:57 -08:00
parent 9c87345135
commit 77d624f640
8 changed files with 31 additions and 132 deletions

View File

@ -1,5 +1,8 @@
use crate::comp;
use comp::item::Reagent;
use comp::{
item::{tool::ToolKind, Reagent},
CharacterAbilityType,
};
use serde::{Deserialize, Serialize};
use vek::*;
@ -22,6 +25,14 @@ pub enum Outcome {
body: comp::Body,
vel: Vec3<f32>,
},
Swing {
pos: Vec3<f32>,
ability: CharacterAbilityType,
tool: ToolKind,
},
LevelUp {
pos: Vec3<f32>,
},
}
impl Outcome {
@ -29,6 +40,8 @@ impl Outcome {
match self {
Outcome::Explosion { pos, .. } => Some(*pos),
Outcome::ProjectileShot { pos, .. } => Some(*pos),
Outcome::Swing { pos, .. } => Some(*pos),
Outcome::LevelUp { pos } => Some(*pos),
}
}
}

View File

@ -686,14 +686,24 @@ pub fn handle_explosion(
}
pub fn handle_level_up(server: &mut Server, entity: EcsEntity, new_level: u32) {
let ecs = &server.state.ecs();
let uids = server.state.ecs().read_storage::<Uid>();
let uid = uids
.get(entity)
.expect("Failed to fetch uid component for entity.");
let pos = server
.state
.ecs()
.read_storage::<Pos>()
.get(entity)
.expect("Failed to fetch position component for the entity.")
.0;
server.state.notify_players(ServerGeneral::PlayerListUpdate(
PlayerListUpdate::LevelChange(*uid, new_level),
));
ecs.write_resource::<Vec<Outcome>>()
.push(Outcome::LevelUp { pos });
}
pub fn handle_buff(server: &mut Server, entity: EcsEntity, buff_change: buff::BuffChange) {

View File

@ -2,7 +2,6 @@ mod block;
mod campfire;
mod combat;
mod movement;
mod progression;
use client::Client;
use common::{state::State, terrain::TerrainChunk};
@ -11,7 +10,6 @@ use block::BlockEventMapper;
use campfire::CampfireEventMapper;
use combat::CombatEventMapper;
use movement::MovementEventMapper;
use progression::ProgressionEventMapper;
use super::SfxTriggers;
use crate::scene::{Camera, Terrain};
@ -38,7 +36,6 @@ impl SfxEventMapper {
mappers: vec![
Box::new(CombatEventMapper::new()),
Box::new(MovementEventMapper::new()),
Box::new(ProgressionEventMapper::new()),
Box::new(BlockEventMapper::new()),
Box::new(CampfireEventMapper::new()),
],

View File

@ -1,85 +0,0 @@
/// EventMapper::Progress watches the player entity's stats
/// and triggers sfx for gaining experience and levelling up
use super::EventMapper;
use crate::{
audio::sfx::{SfxEvent, SfxEventItem, SfxTriggers},
scene::{Camera, Terrain},
};
use client::Client;
use common::{comp::Stats, event::EventBus, state::State, terrain::TerrainChunk};
use specs::WorldExt;
#[derive(Clone, PartialEq)]
struct ProgressionState {
level: u32,
exp: u32,
}
impl Default for ProgressionState {
fn default() -> Self { Self { level: 1, exp: 0 } }
}
pub struct ProgressionEventMapper {
state: ProgressionState,
}
impl EventMapper for ProgressionEventMapper {
#[allow(clippy::op_ref)] // TODO: Pending review in #587
fn maintain(
&mut self,
state: &State,
player_entity: specs::Entity,
_camera: &Camera,
triggers: &SfxTriggers,
_terrain: &Terrain<TerrainChunk>,
_client: &Client,
) {
let ecs = state.ecs();
let next_state = ecs.read_storage::<Stats>().get(player_entity).map_or(
ProgressionState::default(),
|stats| ProgressionState {
level: stats.level.level(),
exp: stats.exp.current(),
},
);
if &self.state != &next_state {
if let Some(mapped_event) = self.map_event(&next_state) {
let sfx_trigger_item = triggers.get_trigger(&mapped_event);
if sfx_trigger_item.is_some() {
ecs.read_resource::<EventBus<SfxEventItem>>()
.emit_now(SfxEventItem::at_player_position(mapped_event));
}
}
self.state = next_state;
}
}
}
impl ProgressionEventMapper {
pub fn new() -> Self {
Self {
state: ProgressionState::default(),
}
}
#[allow(clippy::let_and_return)] // TODO: Pending review in #587
fn map_event(&mut self, next_state: &ProgressionState) -> Option<SfxEvent> {
let sfx_event = if next_state.level > self.state.level {
Some(SfxEvent::LevelUp)
} else if next_state.exp > self.state.exp {
Some(SfxEvent::ExperienceGained)
} else {
None
};
sfx_event
}
}
#[cfg(test)] mod tests;

View File

@ -1,43 +0,0 @@
use super::*;
use crate::audio::sfx::SfxEvent;
#[test]
fn no_change_returns_none() {
let mut mapper = ProgressionEventMapper::new();
let next_client_state = ProgressionState::default();
assert_eq!(mapper.map_event(&next_client_state), None);
}
#[test]
fn change_level_returns_levelup() {
let mut mapper = ProgressionEventMapper::new();
let next_client_state = ProgressionState { level: 2, exp: 0 };
assert_eq!(
mapper.map_event(&next_client_state),
Some(SfxEvent::LevelUp)
);
}
#[test]
fn change_exp_returns_expup() {
let mut mapper = ProgressionEventMapper::new();
let next_client_state = ProgressionState { level: 1, exp: 100 };
assert_eq!(
mapper.map_event(&next_client_state),
Some(SfxEvent::ExperienceGained)
);
}
#[test]
fn level_up_and_gained_exp_prioritises_levelup() {
let mut mapper = ProgressionEventMapper::new();
let next_client_state = ProgressionState { level: 2, exp: 100 };
assert_eq!(
mapper.map_event(&next_client_state),
Some(SfxEvent::LevelUp)
);
}

View File

@ -378,6 +378,11 @@ impl SfxMgr {
},
}
},
Outcome::LevelUp { pos } => {
let file_ref = "voxygen.audio.sfx.character.level_up_sound_-_shorter_wind_up";
audio.play_sfx(file_ref, *pos, None);
},
_ => {},
}
}

View File

@ -433,6 +433,7 @@ impl Scene {
fadeout: |timeout| timeout * 2.0,
}),
Outcome::ProjectileShot { .. } => {},
_ => {},
}
}

View File

@ -124,6 +124,7 @@ impl ParticleMgr {
}
},
Outcome::ProjectileShot { .. } => {},
_ => {},
}
}