193 lines
6.8 KiB
Rust
193 lines
6.8 KiB
Rust
use std::{array, iter, time::Duration};
|
|
|
|
use bracket_lib::random::RandomNumberGenerator;
|
|
|
|
use crate::resources::sound_output::{SoundOutput, SoundSamples};
|
|
|
|
type Frequency = u32;
|
|
type StartFrequency = u32;
|
|
type EndFrequency = u32;
|
|
type MinFrequency = u32;
|
|
type MaxFrequency = u32;
|
|
|
|
pub enum SoundType {
|
|
Silence,
|
|
Tone(Frequency),
|
|
Sweep(StartFrequency, EndFrequency),
|
|
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 grab: SoundSamples,
|
|
pub bad_key: SoundSamples,
|
|
pub blocked: SoundSamples,
|
|
pub whipping: SoundSamples,
|
|
pub whipping_hit: SoundSamples,
|
|
pub whipping_hit_end: SoundSamples,
|
|
pub slow_hit: SoundSamples,
|
|
pub medium_hit: SoundSamples,
|
|
pub fast_hit: SoundSamples,
|
|
pub pause: SoundSamples,
|
|
rng: RandomNumberGenerator,
|
|
}
|
|
|
|
impl SoundEffects {
|
|
pub fn new(sound_output: &SoundOutput) -> Self {
|
|
Self {
|
|
startup: sound_output.render_sound_effect(&SoundEffect {
|
|
sounds: vec![Sound {
|
|
sound_type: SoundType::Sweep(1, 350),
|
|
duration: Duration::from_secs(1),
|
|
}],
|
|
}),
|
|
step: sound_output.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),
|
|
},
|
|
],
|
|
}),
|
|
grab: sound_output.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: sound_output.render_sound_effect(&SoundEffect {
|
|
sounds: iter::once(Sound {
|
|
sound_type: SoundType::Tone(540),
|
|
duration: Duration::from_millis(40),
|
|
})
|
|
.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: sound_output.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(),
|
|
}),
|
|
whipping: sound_output.render_sound_effect(&SoundEffect {
|
|
sounds: vec![Sound {
|
|
sound_type: SoundType::Tone(70),
|
|
duration: Duration::from_secs(3),
|
|
}],
|
|
}),
|
|
whipping_hit: sound_output.render_sound_effect(&SoundEffect {
|
|
sounds: vec![
|
|
Sound {
|
|
sound_type: SoundType::Tone(400),
|
|
duration: Duration::from_millis(20),
|
|
},
|
|
Sound {
|
|
sound_type: SoundType::Tone(90),
|
|
duration: Duration::from_secs(3),
|
|
},
|
|
],
|
|
}),
|
|
whipping_hit_end: sound_output.render_sound_effect(&SoundEffect {
|
|
sounds: vec![Sound {
|
|
sound_type: SoundType::Tone(400),
|
|
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),
|
|
}],
|
|
}),
|
|
pause: sound_output.render_sound_effect(&SoundEffect {
|
|
sounds: {
|
|
let mut sounds = vec![Sound {
|
|
sound_type: SoundType::Tone(500),
|
|
duration: Duration::from_millis(100),
|
|
}];
|
|
sounds.extend((100..=200).rev().step_by(10).map(|x| Sound {
|
|
sound_type: SoundType::Tone(x),
|
|
duration: Duration::from_millis(20),
|
|
}));
|
|
sounds
|
|
},
|
|
}),
|
|
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(),
|
|
})
|
|
}
|
|
}
|