aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/night_vision.rs
blob: 179555804f20c5ab8811d0dd389625ae0f8990ca (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::StaticVulkanApp;
use fparkan_world::ObjectHandle;
use std::collections::HashSet;

#[derive(Default)]
pub(super) struct NightVision {
    cameras: HashSet<ObjectHandle>,
    previous: bool,
}

impl NightVision {
    fn toggle(&mut self, camera: ObjectHandle) {
        if !self.cameras.remove(&camera) {
            self.cameras.insert(camera);
        }
    }

    pub(super) fn enabled(&self, camera: ObjectHandle) -> bool {
        self.cameras.contains(&camera)
    }
}

impl StaticVulkanApp {
    pub(super) fn toggle_night_vision(&mut self) {
        if let Some(cockpit) = self.cockpit {
            self.night_vision.toggle(cockpit.handle);
        }
    }

    pub(super) fn update_night_vision(&mut self) -> bool {
        let cockpit = self
            .cockpit
            .filter(|_| self.command_view.is_none() && self.demo_camera.is_none());
        let enabled = cockpit.is_some_and(|cockpit| self.night_vision.enabled(cockpit.handle));
        if std::mem::replace(&mut self.night_vision.previous, enabled) != enabled
            && cockpit.is_some()
        {
            if let Some(audio) = &mut self.audio {
                let voice = if enabled {
                    "VOICE_INFRA_SYS_ON"
                } else {
                    "VOICE_INFRA_SYS_OFF"
                };
                if let Err(error) = audio.queue_game_voice(voice) {
                    eprintln!("night_vision_voice_unavailable voice={voice} reason={error}");
                }
            }
        }
        enabled
    }
}

#[cfg(test)]
#[test]
fn infrared_is_kept_by_each_camera_when_control_moves_between_units() {
    let mut state = NightVision::default();
    let hero = ObjectHandle {
        slot: 0,
        generation: 1,
    };
    let warbot = ObjectHandle {
        slot: 1,
        generation: 1,
    };
    assert!(!state.enabled(hero));
    state.toggle(hero);
    assert!(state.enabled(hero));
    assert!(!state.enabled(warbot));
    state.toggle(warbot);
    state.toggle(hero);
    assert!(!state.enabled(hero));
    assert!(state.enabled(warbot));
}