Sounds: layered synthesis, far variants, delay by the speed of sound
Rockets roar and crackle instead of whistling; blasts are a crack, a falling sub-bass boom and rolling echoes instead of noise; interceptors get their own flight sound. Far blasts, launches and shots play muffled variants, late by their distance at 343 m/s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+40
-1
@@ -47,6 +47,20 @@ struct AirDef {
|
||||
monitor_shown: bool,
|
||||
/// The menu's page while it is open.
|
||||
menu: Option<Page>,
|
||||
/// Where the player is, for the sounds' delay and far variants.
|
||||
listener: DVec3,
|
||||
/// Sounds on their way to the player at the speed of sound: (tick, event, at, pitch).
|
||||
pending: Vec<(u64, String, DVec3, f32)>,
|
||||
}
|
||||
|
||||
/// Blocks a tick the sound travels (343 m/s).
|
||||
const SOUND_SPEED: f64 = 17.15;
|
||||
/// Past this the far variant plays: muffled, more rolling echo.
|
||||
const FAR: f64 = 140.0;
|
||||
|
||||
/// Impulsive sounds (blasts, launches, shots, shock waves) come late from afar and have far variants.
|
||||
fn travels(event: &str) -> bool {
|
||||
["airdef:blast", "airdef:launch", "airdef:gun", "airdef:boom"].iter().any(|p| event.starts_with(p))
|
||||
}
|
||||
|
||||
/// A page of the menu.
|
||||
@@ -238,8 +252,30 @@ impl AirDef {
|
||||
.collect();
|
||||
render::spawn_particles(&spawns);
|
||||
}
|
||||
let now = self.sim.tick;
|
||||
for (event, at, volume, pitch) in out.sounds {
|
||||
render::play_sound(event, at.x, at.y, at.z, volume, pitch);
|
||||
let dist = at.distance(self.listener);
|
||||
if travels(event) {
|
||||
// Seen first, heard later; from afar muffled.
|
||||
let name = if dist > FAR { format!("{event}_far") } else { event.to_string() };
|
||||
let delay = (dist / SOUND_SPEED) as u64;
|
||||
if delay == 0 {
|
||||
render::play_sound(&name, at.x, at.y, at.z, volume, pitch);
|
||||
} else if self.pending.len() < 256 {
|
||||
self.pending.push((now + delay, name, at, pitch));
|
||||
}
|
||||
} else {
|
||||
render::play_sound(event, at.x, at.y, at.z, volume, pitch);
|
||||
}
|
||||
}
|
||||
let mut i = 0;
|
||||
while i < self.pending.len() {
|
||||
if self.pending[i].0 <= now {
|
||||
let (_, name, at, pitch) = self.pending.swap_remove(i);
|
||||
render::play_sound(&name, at.x, at.y, at.z, 1.0, pitch);
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
if !out.commands.is_empty() {
|
||||
command::run_quiet(&out.commands);
|
||||
@@ -419,6 +455,8 @@ impl Mod for AirDef {
|
||||
monitor: None,
|
||||
monitor_shown: false,
|
||||
menu: None,
|
||||
listener: DVec3::ZERO,
|
||||
pending: Vec::new(),
|
||||
};
|
||||
let _ = input::register_key("menu", "Меню AirDef", "key.keyboard.n");
|
||||
let _ = input::register_key("monitor", "Монитор ПВО: следующий комплекс", "key.keyboard.k");
|
||||
@@ -442,6 +480,7 @@ impl Mod for AirDef {
|
||||
let now = self.sim.tick;
|
||||
self.ground.answer(render::ground(), now);
|
||||
let mut out = Out::default();
|
||||
self.listener = DVec3::new(p.x, p.y + 1.6, p.z);
|
||||
self.sim.step(&mut self.ground, Some(DVec3::new(p.x, p.y, p.z)), &mut out);
|
||||
let wanted = self.ground.take_wanted();
|
||||
if !wanted.is_empty() {
|
||||
|
||||
@@ -1655,12 +1655,12 @@ impl Sim {
|
||||
if lit {
|
||||
if (m.age - spec.cold) % 46 == 1 {
|
||||
let pitch = match m.kind {
|
||||
MissileKind::Pac3 => 1.15,
|
||||
MissileKind::M57e6 => 1.45,
|
||||
MissileKind::M48n6 => 0.9,
|
||||
MissileKind::M77n6 => 0.75,
|
||||
MissileKind::Pac3 => 1.0,
|
||||
MissileKind::M57e6 => 1.12,
|
||||
MissileKind::M48n6 => 0.88,
|
||||
MissileKind::M77n6 => 0.8,
|
||||
};
|
||||
out.sound("airdef:drone.rocket", m.pos, 1.0, pitch);
|
||||
out.sound("airdef:missile.flight", m.pos, 1.0, pitch);
|
||||
}
|
||||
let back = m.pos - new * 1.4 * spec.scale;
|
||||
out.particles("minecraft:flame", back, 0.05, -new * 0.1, 0.01, 2);
|
||||
|
||||
Reference in New Issue
Block a user