Skip to main content

mittens_engine/engine/ecs/component/
emissive.rs

1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3
4#[derive(Debug, Clone, Copy)]
5pub struct EmissiveComponent {
6    pub intensity: f32,
7}
8
9impl Default for EmissiveComponent {
10    fn default() -> Self {
11        Self::on()
12    }
13}
14
15impl EmissiveComponent {
16    pub fn new(intensity: f32) -> Self {
17        Self {
18            intensity: if intensity.is_finite() {
19                intensity.max(0.0)
20            } else {
21                0.0
22            },
23        }
24    }
25
26    pub fn on() -> Self {
27        Self { intensity: 1.0 }
28    }
29
30    pub fn off() -> Self {
31        Self { intensity: 0.0 }
32    }
33}
34
35impl Component for EmissiveComponent {
36    fn as_any(&self) -> &dyn std::any::Any {
37        self
38    }
39
40    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
41        self
42    }
43
44    fn name(&self) -> &'static str {
45        "emissive"
46    }
47
48    fn init(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter, component: ComponentId) {
49        emit.push_intent_now(
50            component,
51            crate::engine::ecs::IntentValue::RegisterEmissive {
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 ctor = if self.intensity == 0.0 { "off" } else { "on" };
63        let mut ce = ce_call("Emissive", ctor, vec![]);
64        if self.intensity != 0.0 && self.intensity != 1.0 {
65            ce = ce.with_call("intensity", vec![num(self.intensity as f64)]);
66        }
67        ce
68    }
69}