Skip to main content

mittens_engine/engine/ecs/component/
mirror.rs

1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3
4/// Mirror component.
5///
6/// Attached to a `RenderableComponent` (or its ancestor) to enable planar reflections.
7/// The mirror plane is defined by the nearest ancestor `TransformComponent`'s XY plane
8/// (+Z is the reflection normal).
9#[derive(Debug, Clone)]
10pub struct MirrorComponent {
11    /// Resolution of the mirror texture (e.g. 512, 1024).
12    pub quality: i32,
13    component: Option<ComponentId>,
14}
15
16impl MirrorComponent {
17    pub fn new(quality: i32) -> Self {
18        Self {
19            quality: quality.clamp(64, 2048),
20            component: None,
21        }
22    }
23}
24
25impl Default for MirrorComponent {
26    fn default() -> Self {
27        Self::new(512)
28    }
29}
30
31impl Component for MirrorComponent {
32    fn name(&self) -> &'static str {
33        "mirror"
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 to_mms_ast(
49        &self,
50        _world: &crate::engine::ecs::World,
51    ) -> crate::scripting::ast::ComponentExpression {
52        use crate::engine::ecs::component::ce_helpers::*;
53        ce_call("Mirror", "quality", vec![num(self.quality as f64)])
54    }
55}