-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve sfx abstractions + preload sfx on startup
- Loading branch information
Showing
12 changed files
with
161 additions
and
41 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Git LFS file not shown
32 changes: 11 additions & 21 deletions
32
canon_collision/src/audio.rs → canon_collision/src/audio/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use std::collections::HashMap; | ||
use std::fs; | ||
use std::path::{Path, PathBuf, MAIN_SEPARATOR}; | ||
|
||
use kira::Value; | ||
use kira::instance::InstanceSettings; | ||
use kira::manager::AudioManager; | ||
use kira::playable::PlayableSettings; | ||
use kira::sound::SoundId; | ||
|
||
use canon_collision_lib::entity_def::EntityDef; | ||
|
||
// TODO: move into hitbox canon_collision_lib hitbox definition | ||
pub enum HitBoxSFX { | ||
Sword, | ||
Punch, | ||
//Explode, etc... | ||
} | ||
|
||
pub enum SFXType { | ||
Walk, | ||
Run, | ||
Dash, | ||
Jump, | ||
Land, | ||
Die, | ||
Hit (HitBoxSFX), | ||
/// TODO: Dont know if the ergonomics and efficiency of this is a good idea. | ||
/// Lets play with it a bit and throw it away if we dont like it. | ||
Custom { | ||
filename: String, | ||
volume: Value<f64>, | ||
pitch: Value<f64> | ||
} | ||
} | ||
|
||
pub struct SFX { | ||
sfx: HashMap<String, SoundId>, | ||
} | ||
|
||
impl SFX { | ||
pub fn new(manager: &mut AudioManager, path: &Path) -> Self { | ||
let mut sfx = HashMap::new(); | ||
let path = path.join("sfx"); | ||
SFX::populate_sfx(manager, &path, None, &mut sfx); | ||
SFX { sfx } | ||
} | ||
|
||
fn populate_sfx(manager: &mut AudioManager, root_path: &Path, search_path: Option<&PathBuf>, sfx: &mut HashMap<String, SoundId>) { | ||
let path = if let Some(search_path) = search_path { | ||
root_path.join(search_path) | ||
} else { | ||
root_path.to_path_buf() | ||
}; | ||
|
||
for file in fs::read_dir(path).unwrap() { | ||
let file = file.unwrap(); | ||
let playable_settings = PlayableSettings::default(); | ||
|
||
let sub_search_path = if let Some(search_path) = search_path { | ||
search_path.join(file.file_name()) | ||
} else { | ||
PathBuf::from(file.file_name()) | ||
}; | ||
|
||
let file_type = file.file_type().unwrap(); | ||
if file_type.is_file() { | ||
let id = manager.load_sound(file.path(), playable_settings).unwrap(); | ||
|
||
let key = sub_search_path.to_str().unwrap().replace(MAIN_SEPARATOR, "/"); | ||
if sfx.contains_key(&key) { | ||
panic!("Duplicate sfx key"); | ||
} | ||
sfx.insert(key, id); | ||
} | ||
else if file_type.is_dir() { | ||
SFX::populate_sfx(manager, root_path, Some(&sub_search_path), sfx); | ||
} | ||
} | ||
} | ||
|
||
/// TODO: How to handle rollback? | ||
pub fn play_sound_effect(&mut self, manager: &mut AudioManager, entity: &EntityDef, sfx: SFXType) { | ||
let entity_name = entity.name.replace(" ", ""); | ||
|
||
let sfx_id = match (&entity_name, &sfx) { | ||
//(_, SFXType::Walk) => ["Common/walk1.ogg", "Common/walk2.ogg"].choose(&mut rand::thread_rng()).unwrap(), // TODO: This is possible | ||
(_, SFXType::Walk) => self.sfx["Common/walk.ogg"], | ||
(_, SFXType::Run) => self.sfx["Common/walk.ogg"], | ||
(_, SFXType::Dash) => self.sfx["Common/dash.ogg"], | ||
(_, SFXType::Jump) => self.sfx["Common/jump.ogg"], | ||
(_, SFXType::Land) => self.sfx["Common/land.ogg"], | ||
(_, SFXType::Die) => self.sfx["Common/die.wav"], | ||
(_, SFXType::Hit (HitBoxSFX::Sword)) => self.sfx["Common/swordHit.ogg"], | ||
(_, SFXType::Hit (HitBoxSFX::Punch)) => self.sfx["Common/hit.wav"], | ||
(folder, SFXType::Custom { filename, .. }) => self.sfx[&format!("{}/{}", folder, filename)], | ||
}.clone(); | ||
|
||
let (volume, pitch) = match (&entity_name, sfx) { | ||
(_, SFXType::Walk) => (Value::Random(0.01, 0.03), Value::Random(0.95, 1.05)), | ||
(_, SFXType::Run) => (Value::Random(0.03, 0.1), Value::Random(0.95, 1.05)), | ||
(_, SFXType::Dash) => (Value::Random(0.15, 0.2), Value::Random(0.95, 1.05)), | ||
(_, SFXType::Jump) => (Value::Random(0.15, 0.2), Value::Random(0.90, 1.1)), | ||
(_, SFXType::Land) => (Value::Random(0.05, 0.1), Value::Random(0.90, 1.1)), | ||
(_, SFXType::Die) => (Value::Random(0.30, 0.4), Value::Random(0.90, 1.1)), | ||
(_, SFXType::Hit (HitBoxSFX::Sword)) => (Value::Random(0.15, 0.2), Value::Random(0.95, 1.05)), | ||
(_, SFXType::Hit (HitBoxSFX::Punch)) => (Value::Random(0.15, 0.2), Value::Random(0.90, 1.1)), | ||
(_, SFXType::Custom { volume, pitch, .. }) => (volume, pitch), | ||
}; | ||
|
||
let instance_settings = InstanceSettings::default().volume(volume).pitch(pitch); | ||
manager.play(sfx_id, instance_settings).map_err(|x| x.to_string()).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters