aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/shot_sound.rs
blob: 12206a32341cc27750a70460883ad27d2699bf8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! Load the spatial one-shot from a weapon's firing effect.

use crate::audio::GameAudio;
use fparkan_assets::load_resource_entry_bytes_from_root;
use fparkan_fx::{ambient_sound::Op2SoundCommand, decode_fxid};
use fparkan_runtime::AutoDemoCombatEvent;
use std::path::Path;

pub(super) fn load(root: &Path, archive: &str, name: &str) -> Option<Op2SoundCommand> {
    let bytes = load_resource_entry_bytes_from_root(root, archive, name).ok()?;
    let effect = decode_fxid(bytes).ok()?;
    effect
        .commands()
        .iter()
        .find_map(Op2SoundCommand::from_command)
}

pub(super) fn play_autonomous(
    sounds: &[Option<Op2SoundCommand>],
    events: &[AutoDemoCombatEvent],
    audio: &mut GameAudio,
) {
    for event in events {
        let AutoDemoCombatEvent::Shot {
            effect: Some(effect),
            muzzle_bits,
            ..
        } = *event
        else {
            continue;
        };
        let Some(sound) = sounds
            .get(effect)
            .and_then(Option::as_ref)
            .and_then(|command| command.one_shot_at(muzzle_bits.map(f32::from_bits)))
        else {
            continue;
        };
        if let Err(error) = audio.handle_sound_event(&sound) {
            eprintln!(
                "sim_coverage autonomous_weapon_sound_stub=1 effect={}:{} reason={error}",
                sound.archive, sound.name
            );
        }
    }
}