kroz-rs/src/resources/sound_effects.rs
2022-01-27 17:22:14 -05:00

131 lines
4.3 KiB
Rust

use std::{array, iter, time::Duration};
use bracket_lib::random::RandomNumberGenerator;
use crate::resources::sound_output::{SoundOutput, SoundSamples};
type Frequency = u32;
type MinFrequency = u32;
type MaxFrequency = u32;
pub enum SoundType {
Silence,
Tone(Frequency),
Noise(MinFrequency, MaxFrequency),
}
pub struct Sound {
pub sound_type: SoundType,
pub duration: Duration,
}
pub struct SoundEffect {
pub sounds: Vec<Sound>,
}
pub struct SoundEffects {
pub startup: SoundSamples,
pub step: SoundSamples,
pub pickup: SoundSamples,
pub bad_key: SoundSamples,
pub blocked: SoundSamples,
rng: RandomNumberGenerator,
}
impl SoundEffects {
pub fn new(ss: &SoundOutput) -> Self {
Self {
startup: ss.render_sound_effect(&SoundEffect {
sounds: (30..400)
.step_by(8)
.map(|x| Sound {
sound_type: SoundType::Tone(x),
duration: Duration::from_millis(24),
})
.collect(),
}),
step: ss.render_sound_effect(&SoundEffect {
sounds: vec![
Sound {
sound_type: SoundType::Noise(350, 900),
duration: Duration::from_millis(6),
},
Sound {
sound_type: SoundType::Silence,
duration: Duration::from_millis(120),
},
Sound {
sound_type: SoundType::Noise(150, 200),
duration: Duration::from_millis(6),
},
],
}),
pickup: ss.render_sound_effect(&SoundEffect {
sounds: vec![
Sound {
sound_type: SoundType::Noise(350, 900),
duration: Duration::from_millis(6),
},
Sound {
sound_type: SoundType::Silence,
duration: Duration::from_millis(120),
},
Sound {
sound_type: SoundType::Noise(1000, 2000),
duration: Duration::from_millis(20),
},
],
}),
bad_key: ss.render_sound_effect(&SoundEffect {
sounds: iter::once(Sound {
sound_type: SoundType::Tone(400),
duration: Duration::from_millis(20),
})
.chain((0..4).flat_map(|_| {
array::IntoIter::new([
Sound {
sound_type: SoundType::Tone(100),
duration: Duration::from_millis(15),
},
Sound {
sound_type: SoundType::Silence,
duration: Duration::from_millis(15),
},
])
}))
.collect(),
}),
blocked: ss.render_sound_effect(&SoundEffect {
sounds: (30..=60)
.rev()
.step_by(6)
.map(|x| Sound {
sound_type: SoundType::Tone(x),
duration: Duration::from_millis(18),
})
.collect(),
}),
rng: RandomNumberGenerator::new(),
}
}
pub fn get_new_static_effect(&mut self, ss: &SoundOutput) -> SoundSamples {
ss.render_sound_effect(&SoundEffect {
sounds: (1..=33)
.map(|_| {
if self.rng.roll_dice(1, 2) > 1 {
Sound {
sound_type: SoundType::Noise(3000, 7000),
duration: Duration::from_millis(self.rng.range(1, 15)),
}
} else {
Sound {
sound_type: SoundType::Silence,
duration: Duration::from_millis(self.rng.range(0, 30)),
}
}
})
.collect(),
})
}
}