rust-sokoban/src/audio.rs

33 lines
925 B
Rust
Raw Normal View History

2020-07-27 09:59:14 -04:00
use ggez::{Context, audio};
use ggez::audio::SoundSource;
use specs::{World, WorldExt};
use std::collections::HashMap;
#[derive(Default)]
pub struct AudioStore {
pub sounds: HashMap<String, audio::Source>,
}
impl AudioStore {
pub fn play_sound(&mut self, sound: &String) {
let _ = self
.sounds
.get_mut(sound)
.expect("expected sound")
.play_detached();
}
}
pub fn initialize_sounds(world: &mut World, context: &mut Context) {
let mut audio_store = world.write_resource::<AudioStore>();
let sounds = ["correct", "incorrect", "wall"];
for sound in sounds.iter() {
let sound_name = sound.to_string();
let sound_path = format!("/sounds/{}.wav", sound_name);
let sound_source = audio::Source::new(context, sound_path).expect("expected sound loaded");
audio_store.sounds.insert(sound_name, sound_source);
}
}