Skip to main content

mittens_engine/engine/ecs/component/
point_light.rs

1use super::Component;
2use crate::engine::ecs::ComponentId;
3
4/// Point light (local omnidirectional light).
5///
6/// This is purely an ECS representation for now; renderer integration will come later.
7#[derive(Debug, Clone, Copy)]
8pub struct PointLightComponent {
9    pub intensity: f32,
10    pub distance: f32,
11    /// Linear RGB color in 0..1.
12    pub color: [f32; 3],
13
14    component: Option<ComponentId>,
15}
16
17impl PointLightComponent {
18    pub fn new() -> Self {
19        Self {
20            intensity: 1.0,
21            distance: 10.0,
22            color: [1.0, 1.0, 1.0],
23            component: None,
24        }
25    }
26
27    pub fn with_intensity(mut self, intensity: f32) -> Self {
28        self.intensity = intensity;
29        self
30    }
31
32    pub fn with_distance(mut self, distance: f32) -> Self {
33        self.distance = distance;
34        self
35    }
36
37    pub fn with_color(mut self, r: f32, g: f32, b: f32) -> Self {
38        self.color = [r, g, b];
39        self
40    }
41
42    pub fn id(&self) -> Option<ComponentId> {
43        self.component
44    }
45}
46
47impl Default for PointLightComponent {
48    fn default() -> Self {
49        Self::new()
50    }
51}
52
53impl Component for PointLightComponent {
54    fn set_id(&mut self, component: ComponentId) {
55        self.component = Some(component);
56    }
57
58    fn name(&self) -> &'static str {
59        "point_light"
60    }
61
62    fn init(
63        &mut self,
64        emit: &mut dyn crate::engine::ecs::SignalEmitter,
65        component: crate::engine::ecs::ComponentId,
66    ) {
67        emit.push_intent_now(
68            component,
69            crate::engine::ecs::IntentValue::RegisterLight {
70                component_ids: vec![component],
71            },
72        );
73    }
74
75    fn as_any(&self) -> &dyn std::any::Any {
76        self
77    }
78
79    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
80        self
81    }
82
83    fn to_mms_ast(
84        &self,
85        _world: &crate::engine::ecs::World,
86    ) -> crate::scripting::ast::ComponentExpression {
87        use crate::engine::ecs::component::ce_helpers::*;
88        ce("PointLight")
89            .with_call("intensity", vec![num(self.intensity as f64)])
90            .with_call("distance", vec![num(self.distance as f64)])
91            .with_call("color", nums(self.color.iter().map(|&v| v as f64)))
92    }
93}