Skip to main content

mittens_engine/engine/ecs/component/
router.rs

1use super::Component;
2use crate::engine::ecs::{ComponentId, IntentValue, SignalEmitter};
3
4#[derive(Debug, Clone, Default)]
5pub struct RouterComponent {
6    pub target_name: Option<String>,
7    pub ignore_names: Vec<String>,
8    component: Option<ComponentId>,
9}
10
11impl RouterComponent {
12    pub fn new() -> Self {
13        Self::default()
14    }
15
16    pub fn with_target_name(mut self, target_name: impl Into<String>) -> Self {
17        self.target_name = Some(target_name.into());
18        self
19    }
20
21    pub fn with_ignored_names<I, S>(mut self, ignore_names: I) -> Self
22    where
23        I: IntoIterator<Item = S>,
24        S: Into<String>,
25    {
26        self.ignore_names = ignore_names.into_iter().map(Into::into).collect();
27        self
28    }
29}
30
31impl Component for RouterComponent {
32    fn name(&self) -> &'static str {
33        "router"
34    }
35
36    fn set_id(&mut self, component: ComponentId) {
37        self.component = Some(component);
38    }
39
40    fn as_any(&self) -> &dyn std::any::Any {
41        self
42    }
43
44    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
45        self
46    }
47
48    fn init(&mut self, emit: &mut dyn SignalEmitter, component: ComponentId) {
49        emit.push_intent_now(
50            component,
51            IntentValue::RegisterRouter {
52                component_ids: vec![component],
53            },
54        );
55    }
56
57    fn to_mms_ast(
58        &self,
59        _world: &crate::engine::ecs::World,
60    ) -> crate::scripting::ast::ComponentExpression {
61        use crate::engine::ecs::component::ce_helpers::*;
62        let mut ce = ce("Router");
63        if let Some(target) = &self.target_name {
64            ce = ce.with_call("target", vec![s(target)]);
65        }
66        if !self.ignore_names.is_empty() {
67            let items: Vec<_> = self.ignore_names.iter().map(|n| s(n)).collect();
68            ce = ce.with_call("ignore", vec![array(items)]);
69        }
70        ce
71    }
72}