Add monster impact sfx

This commit is contained in:
Alex Page 2022-02-01 02:54:08 -05:00
parent 73aa491d24
commit b2672904c8
4 changed files with 60 additions and 17 deletions

View file

@ -1,4 +1,7 @@
use crate::vga_color as vga; use crate::{
resources::{sound_output::SoundSamples, SoundEffects},
vga_color as vga,
};
use bracket_lib::prelude::*; use bracket_lib::prelude::*;
use specs::prelude::*; use specs::prelude::*;
use specs_derive::Component; use specs_derive::Component;
@ -47,3 +50,11 @@ pub fn damage_for_kind(kind: MonsterKind) -> u32 {
MonsterKind::Fast => 3, MonsterKind::Fast => 3,
} }
} }
pub fn sound_effect_for_kind(kind: MonsterKind, sound_effects: &SoundEffects) -> SoundSamples {
match kind {
MonsterKind::Slow => sound_effects.slow_hit.clone(),
MonsterKind::Medium => sound_effects.medium_hit.clone(),
MonsterKind::Fast => sound_effects.fast_hit.clone(),
}
}

View file

@ -35,19 +35,22 @@ pub struct SoundEffects {
pub whipping: SoundSamples, pub whipping: SoundSamples,
pub whipping_hit: SoundSamples, pub whipping_hit: SoundSamples,
pub whipping_hit_end: SoundSamples, pub whipping_hit_end: SoundSamples,
pub slow_hit: SoundSamples,
pub medium_hit: SoundSamples,
pub fast_hit: SoundSamples,
rng: RandomNumberGenerator, rng: RandomNumberGenerator,
} }
impl SoundEffects { impl SoundEffects {
pub fn new(ss: &SoundOutput) -> Self { pub fn new(sound_output: &SoundOutput) -> Self {
Self { Self {
startup: ss.render_sound_effect(&SoundEffect { startup: sound_output.render_sound_effect(&SoundEffect {
sounds: vec![Sound { sounds: vec![Sound {
sound_type: SoundType::Sweep(1, 350), sound_type: SoundType::Sweep(1, 350),
duration: Duration::from_secs(1), duration: Duration::from_secs(1),
}], }],
}), }),
step: ss.render_sound_effect(&SoundEffect { step: sound_output.render_sound_effect(&SoundEffect {
sounds: vec![ sounds: vec![
Sound { Sound {
sound_type: SoundType::Noise(350, 900), sound_type: SoundType::Noise(350, 900),
@ -63,7 +66,7 @@ impl SoundEffects {
}, },
], ],
}), }),
pickup: ss.render_sound_effect(&SoundEffect { pickup: sound_output.render_sound_effect(&SoundEffect {
sounds: vec![ sounds: vec![
Sound { Sound {
sound_type: SoundType::Noise(350, 900), sound_type: SoundType::Noise(350, 900),
@ -79,7 +82,7 @@ impl SoundEffects {
}, },
], ],
}), }),
bad_key: ss.render_sound_effect(&SoundEffect { bad_key: sound_output.render_sound_effect(&SoundEffect {
sounds: iter::once(Sound { sounds: iter::once(Sound {
sound_type: SoundType::Tone(540), sound_type: SoundType::Tone(540),
duration: Duration::from_millis(40), duration: Duration::from_millis(40),
@ -98,7 +101,7 @@ impl SoundEffects {
})) }))
.collect(), .collect(),
}), }),
blocked: ss.render_sound_effect(&SoundEffect { blocked: sound_output.render_sound_effect(&SoundEffect {
sounds: (30..=60) sounds: (30..=60)
.rev() .rev()
.step_by(6) .step_by(6)
@ -108,13 +111,13 @@ impl SoundEffects {
}) })
.collect(), .collect(),
}), }),
whipping: ss.render_sound_effect(&SoundEffect { whipping: sound_output.render_sound_effect(&SoundEffect {
sounds: vec![Sound { sounds: vec![Sound {
sound_type: SoundType::Tone(70), sound_type: SoundType::Tone(70),
duration: Duration::from_secs(3), duration: Duration::from_secs(3),
}], }],
}), }),
whipping_hit: ss.render_sound_effect(&SoundEffect { whipping_hit: sound_output.render_sound_effect(&SoundEffect {
sounds: vec![ sounds: vec![
Sound { Sound {
sound_type: SoundType::Tone(400), sound_type: SoundType::Tone(400),
@ -126,12 +129,30 @@ impl SoundEffects {
}, },
], ],
}), }),
whipping_hit_end: ss.render_sound_effect(&SoundEffect { whipping_hit_end: sound_output.render_sound_effect(&SoundEffect {
sounds: vec![Sound { sounds: vec![Sound {
sound_type: SoundType::Tone(400), sound_type: SoundType::Tone(400),
duration: Duration::from_millis(20), duration: Duration::from_millis(20),
}], }],
}), }),
slow_hit: sound_output.render_sound_effect(&SoundEffect {
sounds: vec![Sound {
sound_type: SoundType::Tone(400),
duration: Duration::from_millis(25),
}],
}),
medium_hit: sound_output.render_sound_effect(&SoundEffect {
sounds: vec![Sound {
sound_type: SoundType::Tone(600),
duration: Duration::from_millis(25),
}],
}),
fast_hit: sound_output.render_sound_effect(&SoundEffect {
sounds: vec![Sound {
sound_type: SoundType::Tone(800),
duration: Duration::from_millis(25),
}],
}),
rng: RandomNumberGenerator::new(), rng: RandomNumberGenerator::new(),
} }
} }

View file

@ -1,6 +1,7 @@
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use crate::components::monster::damage_for_kind; use crate::components::monster::damage_for_kind;
use crate::components::monster::sound_effect_for_kind;
use crate::components::*; use crate::components::*;
use crate::resources::*; use crate::resources::*;
use crate::systems::*; use crate::systems::*;
@ -119,7 +120,7 @@ impl State {
let monsters = self.ecs.write_storage::<Monster>(); let monsters = self.ecs.write_storage::<Monster>();
let mut map = self.ecs.write_resource::<Map>(); let mut map = self.ecs.write_resource::<Map>();
let mut stats = self.ecs.write_resource::<Stats>(); let mut stats = self.ecs.write_resource::<Stats>();
let mut sound_system = self.ecs.write_resource::<SoundOutput>(); let mut sound_output = self.ecs.write_resource::<SoundOutput>();
let wants_to_whips = self.ecs.read_storage::<WantsToWhip>(); let wants_to_whips = self.ecs.read_storage::<WantsToWhip>();
for (player_entity, player, pos) in (&entities, &mut players, &mut positions).join() { for (player_entity, player, pos) in (&entities, &mut players, &mut positions).join() {
@ -139,11 +140,15 @@ impl State {
if map.in_bounds(destination) { if map.in_bounds(destination) {
if map.is_solid(destination) { if map.is_solid(destination) {
sound_system.play_sound(sound_effects.blocked.clone()); sound_output.play_sound(sound_effects.blocked.clone());
} else { } else {
if let Some(e) = map.get_tile_content_at(destination) { if let Some(e) = map.get_tile_content_at(destination) {
if let Some(monster) = monsters.get(e) { if let Some(monster) = monsters.get(e) {
stats.take_gems(damage_for_kind(monster.kind)); stats.take_gems(damage_for_kind(monster.kind));
sound_output.play_sound(sound_effect_for_kind(
monster.kind,
&sound_effects,
));
let _ = entities.delete(e); let _ = entities.delete(e);
} }
} }
@ -161,11 +166,11 @@ impl State {
self.ecs.write_resource::<Clock>().force_tick(); self.ecs.write_resource::<Clock>().force_tick();
sound_system.play_sound(sound_effects.step.clone()); sound_output.play_sound(sound_effects.step.clone());
} }
} else { } else {
let static_sound = sound_effects.get_new_static_effect(&sound_system); let static_sound = sound_effects.get_new_static_effect(&sound_output);
sound_system.play_sound(static_sound); sound_output.play_sound(static_sound);
} }
player.last_moved = now; player.last_moved = now;

View file

@ -1,9 +1,9 @@
use crate::{ use crate::{
components::{ components::{
monster::{self, damage_for_kind, ticks_for_kind}, monster::{self, damage_for_kind, sound_effect_for_kind, ticks_for_kind},
Monster, Player, Position, Renderable, Monster, Player, Position, Renderable,
}, },
resources::{Clock, Map, Stats}, resources::{Clock, Map, SoundEffects, SoundOutput, Stats},
tile_data::TileType, tile_data::TileType,
}; };
use bracket_lib::{prelude::*, random::RandomNumberGenerator}; use bracket_lib::{prelude::*, random::RandomNumberGenerator};
@ -19,6 +19,8 @@ impl<'a> System<'a> for MonsterAiSystem {
ReadExpect<'a, Point>, ReadExpect<'a, Point>,
WriteExpect<'a, Map>, WriteExpect<'a, Map>,
WriteExpect<'a, Stats>, WriteExpect<'a, Stats>,
ReadExpect<'a, SoundEffects>,
WriteExpect<'a, SoundOutput>,
WriteStorage<'a, Monster>, WriteStorage<'a, Monster>,
WriteStorage<'a, Position>, WriteStorage<'a, Position>,
WriteStorage<'a, Renderable>, WriteStorage<'a, Renderable>,
@ -33,6 +35,8 @@ impl<'a> System<'a> for MonsterAiSystem {
player_pos, player_pos,
mut map, mut map,
mut stats, mut stats,
sound_effects,
mut sound_output,
mut monsters, mut monsters,
mut positions, mut positions,
mut renderables, mut renderables,
@ -79,6 +83,8 @@ impl<'a> System<'a> for MonsterAiSystem {
// TODO: Sound // TODO: Sound
map.clear_tile_content_at(Point::from(**position)); map.clear_tile_content_at(Point::from(**position));
stats.take_gems(damage_for_kind(monster.kind)); stats.take_gems(damage_for_kind(monster.kind));
sound_output
.play_sound(sound_effect_for_kind(monster.kind, &sound_effects));
let _ = entities.delete(*entity); let _ = entities.delete(*entity);
} }
} else { } else {