Skip to main content

mittens_engine/engine/ecs/component/
ambient_light.rs

1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3
4/// Global ambient light.
5///
6/// This is intended to be a singleton-like component (the last registered wins).
7/// The value is linear RGB in 0..1.
8#[derive(Debug, Clone, Copy)]
9pub struct AmbientLightComponent {
10    pub rgb: [f32; 3],
11}
12
13impl AmbientLightComponent {
14    pub fn new() -> Self {
15        Self {
16            rgb: [0.0, 0.0, 0.0],
17        }
18    }
19
20    pub fn rgb(r: f32, g: f32, b: f32) -> Self {
21        Self { rgb: [r, g, b] }
22    }
23
24    pub fn with_rgb(mut self, r: f32, g: f32, b: f32) -> Self {
25        self.rgb = [r, g, b];
26        self
27    }
28}
29
30impl Default for AmbientLightComponent {
31    fn default() -> Self {
32        Self::new()
33    }
34}
35
36impl Component for AmbientLightComponent {
37    fn name(&self) -> &'static str {
38        "ambient_light"
39    }
40
41    fn as_any(&self) -> &dyn std::any::Any {
42        self
43    }
44
45    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
46        self
47    }
48
49    fn init(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter, component: ComponentId) {
50        emit.push_intent_now(
51            component,
52            crate::engine::ecs::IntentValue::RegisterAmbientLight {
53                component_ids: vec![component],
54            },
55        );
56    }
57
58    fn to_mms_ast(
59        &self,
60        _world: &crate::engine::ecs::World,
61    ) -> crate::scripting::ast::ComponentExpression {
62        use crate::engine::ecs::component::ce_helpers::*;
63        ce_call(
64            "AmbientLight",
65            "rgb",
66            nums(self.rgb.iter().map(|&v| v as f64)),
67        )
68    }
69}