Skip to main content

mittens_engine/engine/ecs/component/
signal_route_upward.rs

1use super::Component;
2use crate::engine::ecs::{ComponentId, IntentValue, SignalEmitter};
3
4/// Pipeline operator: route matching intents upward to the first ancestor whose type matches.
5#[derive(Debug, Clone, Default)]
6pub struct SignalRouteUpwardComponent {
7    pub intent_kind: String,
8    pub parent_type: String,
9}
10
11impl SignalRouteUpwardComponent {
12    pub fn new(intent_kind: impl Into<String>, parent_type: impl Into<String>) -> Self {
13        Self {
14            intent_kind: intent_kind.into(),
15            parent_type: parent_type.into(),
16        }
17    }
18}
19
20impl Component for SignalRouteUpwardComponent {
21    fn name(&self) -> &'static str {
22        "signal_route_upward"
23    }
24
25    fn as_any(&self) -> &dyn std::any::Any {
26        self
27    }
28
29    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
30        self
31    }
32
33    fn init(&mut self, emit: &mut dyn SignalEmitter, component: ComponentId) {
34        emit.push_intent_now(
35            component,
36            IntentValue::RegisterSignalRouteUpward {
37                component_ids: vec![component],
38            },
39        );
40    }
41
42    fn cleanup(&mut self, emit: &mut dyn SignalEmitter, component: ComponentId) {
43        emit.push_intent_now(
44            component,
45            IntentValue::RemoveSignalRouteUpward {
46                component_ids: vec![component],
47            },
48        );
49    }
50
51    fn to_mms_ast(
52        &self,
53        _world: &crate::engine::ecs::World,
54    ) -> crate::scripting::ast::ComponentExpression {
55        use crate::engine::ecs::component::ce_helpers::*;
56        ce_call(
57            "SignalRouteUpward",
58            "new",
59            vec![s(&self.intent_kind), s(&self.parent_type)],
60        )
61    }
62}