door sounds, ambient tracks

This commit is contained in:
Llywelwyn 2023-10-12 15:22:13 +01:00
parent 0c74531c5b
commit b105a415d5
13 changed files with 71 additions and 5 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -3,7 +3,7 @@ use notan::prelude::*;
use specs::prelude::*;
use std::sync::Mutex;
use std::collections::HashMap;
use super::{ EffectSpawner, EffectType };
use super::{ EffectSpawner, EffectType, Targets, add_effect };
use crate::Map;
lazy_static::lazy_static! {
@ -53,6 +53,17 @@ pub fn play_sound(app: &mut App, ecs: &mut World, effect: &EffectSpawner, target
}
}
pub fn stop(app: &mut App) {
let mut ambience = AMBIENCE.lock().unwrap();
if let Some(old) = ambience.take() {
app.audio.stop(&old);
}
}
pub fn ambience(sound: &str) {
add_effect(None, EffectType::Sound { sound: sound.to_string() }, Targets::Tile { target: 0 })
}
pub fn replace_ambience(app: &mut App, sound: &Sound) {
let mut ambience = AMBIENCE.lock().unwrap();
if let Some(old) = ambience.take() {
@ -62,12 +73,21 @@ pub fn replace_ambience(app: &mut App, sound: &Sound) {
}
pub fn init_sounds(app: &mut App) {
let list: Vec<(&str, (&[u8], AudioType))> = vec![
//key, (bytes, type) - audiotype determines final volume, looping, etc.
("hit", (include_bytes!("../../resources/sounds/hit.wav"), AudioType::SFX))
let sound_data: &[(&str, &[u8], AudioType)] = &[
// (key, file_path, audiotype)
("a_relax", include_bytes!("../../resources/sounds/amb/relaxed.wav"), AudioType::Ambient),
("d_blocked1", include_bytes!("../../resources/sounds/door/blocked1.wav"), AudioType::SFX),
("d_blocked2", include_bytes!("../../resources/sounds/door/blocked2.wav"), AudioType::SFX),
("d_blocked3", include_bytes!("../../resources/sounds/door/blocked3.wav"), AudioType::SFX),
("d_open1", include_bytes!("../../resources/sounds/door/open1.wav"), AudioType::SFX),
("d_open2", include_bytes!("../../resources/sounds/door/open2.wav"), AudioType::SFX),
("d_open3", include_bytes!("../../resources/sounds/door/open3.wav"), AudioType::SFX),
("d_close1", include_bytes!("../../resources/sounds/door/close1.wav"), AudioType::SFX),
("d_close2", include_bytes!("../../resources/sounds/door/close2.wav"), AudioType::SFX),
("d_close3", include_bytes!("../../resources/sounds/door/close3.wav"), AudioType::SFX),
];
let mut sounds = SOUNDS.lock().unwrap();
for (k, (bytes, audiotype)) in list.iter() {
for (k, bytes, audiotype) in sound_data {
sounds.insert(k.to_string(), (app.audio.create_source(bytes).unwrap(), *audiotype));
}
}
@ -76,3 +96,42 @@ pub fn set_volume(vol: f32) {
let mut volume = VOLUME.lock().unwrap();
*volume = vol;
}
pub fn clean(app: &mut App) {
app.audio.clean();
}
// Shorthand functions for adding generic, frequent SFX to the effect queue.
pub fn door_open(idx: usize) {
let mut rng = RandomNumberGenerator::new();
let sound = (
match rng.range(0, 3) {
0 => "d_open1",
1 => "d_open2",
_ => "d_open3",
}
).to_string();
super::add_effect(None, EffectType::Sound { sound }, Targets::Tile { target: idx });
}
pub fn door_resist(idx: usize) {
let mut rng = RandomNumberGenerator::new();
let sound = (
match rng.range(0, 3) {
0 => "d_blocked1",
1 => "d_blocked2",
_ => "d_blocked3",
}
).to_string();
add_effect(None, EffectType::Sound { sound }, Targets::Tile { target: idx });
}
pub fn door_close(idx: usize) {
let mut rng = RandomNumberGenerator::new();
let sound = (
match rng.range(0, 3) {
0 => "d_close1",
1 => "d_close2",
_ => "d_close3",
}
).to_string();
add_effect(None, EffectType::Sound { sound }, Targets::Tile { target: idx });
}

View file

@ -35,6 +35,7 @@ fn main() -> Result<(), String> {
fn setup(app: &mut App, gfx: &mut Graphics) -> State {
effects::sound::init_sounds(app);
effects::sound::ambience("a_relax");
let texture = gfx
.create_texture()
.from_image(include_bytes!("../resources/atlas.png"))

View file

@ -34,6 +34,7 @@ use super::{
get_dest,
Destination,
DamageType,
effects::sound,
};
use bracket_lib::prelude::*;
use specs::prelude::*;
@ -93,6 +94,7 @@ pub fn try_door(i: i32, j: i32, ecs: &mut World) -> RunState {
if door.open == true {
let renderables = ecs.read_storage::<Renderable>();
if multiple_tile_content {
sound::door_resist(destination_idx);
if let Some(name) = names.get(potential_target) {
gamelog::Logger
::new()
@ -104,6 +106,7 @@ pub fn try_door(i: i32, j: i32, ecs: &mut World) -> RunState {
.log();
}
} else if rng.roll_dice(1, 6) + attributes.strength.modifier() < 2 {
sound::door_resist(destination_idx);
if let Some(name) = names.get(potential_target) {
gamelog::Logger
::new()
@ -115,6 +118,7 @@ pub fn try_door(i: i32, j: i32, ecs: &mut World) -> RunState {
.log();
}
} else {
sound::door_close(destination_idx);
door.open = false;
if door.blocks_vis {
blocks_visibility
@ -210,6 +214,7 @@ pub fn open(i: i32, j: i32, ecs: &mut World) -> RunState {
if door.open == false {
let renderables = ecs.read_storage::<Renderable>();
if rng.roll_dice(1, 6) + attributes.strength.modifier() < 2 {
sound::door_resist(destination_idx);
if let Some(name) = names.get(potential_target) {
gamelog::Logger
::new()
@ -221,6 +226,7 @@ pub fn open(i: i32, j: i32, ecs: &mut World) -> RunState {
.log();
}
} else {
sound::door_open(destination_idx);
door.open = true;
blocks_visibility.remove(potential_target);
blocks_movement.remove(potential_target);