mittens_engine/engine/ecs/component/
audio_output.rs1use super::Component;
2use crate::engine::ecs::ComponentId;
3
4#[derive(Debug, Clone, Copy)]
5pub struct AudioOutputComponent {
6 pub enabled: bool,
8
9 component: Option<ComponentId>,
10}
11
12impl AudioOutputComponent {
13 pub fn new() -> Self {
14 Self {
15 enabled: true,
16 component: None,
17 }
18 }
19
20 pub fn off() -> Self {
21 Self {
22 enabled: false,
23 component: None,
24 }
25 }
26
27 pub fn id(&self) -> Option<ComponentId> {
28 self.component
29 }
30}
31
32impl Default for AudioOutputComponent {
33 fn default() -> Self {
34 Self::new()
35 }
36}
37
38impl Component for AudioOutputComponent {
39 fn set_id(&mut self, component: ComponentId) {
40 self.component = Some(component);
41 }
42
43 fn name(&self) -> &'static str {
44 "audio_output"
45 }
46
47 fn init(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter, component: ComponentId) {
48 if self.enabled {
49 emit.push_intent_now(
50 component,
51 crate::engine::ecs::IntentValue::RegisterAudioOutput {
52 component_ids: vec![component],
53 },
54 );
55 emit.push_intent_now(
56 component,
57 crate::engine::ecs::IntentValue::AudioGraphDirtyImmediate {
58 component_ids: vec![component],
59 },
60 );
61 }
62 }
63
64 fn as_any(&self) -> &dyn std::any::Any {
65 self
66 }
67
68 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
69 self
70 }
71
72 fn encode(&self) -> std::collections::HashMap<String, serde_json::Value> {
73 let mut map = std::collections::HashMap::new();
74 map.insert("enabled".to_string(), serde_json::json!(self.enabled));
75 map
76 }
77
78 fn decode(
79 &mut self,
80 data: &std::collections::HashMap<String, serde_json::Value>,
81 ) -> Result<(), String> {
82 if let Some(enabled) = data.get("enabled") {
83 self.enabled = serde_json::from_value(enabled.clone())
84 .map_err(|e| format!("Failed to decode enabled: {}", e))?;
85 }
86 Ok(())
87 }
88}