use crate::StaticVulkanApp; use fparkan_world::ObjectHandle; use std::collections::HashSet; #[derive(Default)] pub(super) struct NightVision { cameras: HashSet, 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)); }