Skip to main content

mittens_engine/engine/ecs/component/
bounds.rs

1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3use crate::engine::graphics::bounds::Aabb;
4
5/// Cached local-space AABB for a sibling `RenderableComponent`.
6///
7/// Attached automatically as a child of a renderable during
8/// `SystemWorld::register_renderable` whenever the renderable's mesh has a
9/// known local AABB (see `graphics::bounds::mesh_local_aabb`). Layout reads
10/// this to size containers around renderable children without having to mutate
11/// the renderable's own transform.
12pub struct BoundsComponent {
13    pub local: Aabb,
14}
15
16impl BoundsComponent {
17    pub fn new(local: Aabb) -> Self {
18        Self { local }
19    }
20}
21
22impl Component for BoundsComponent {
23    fn name(&self) -> &'static str {
24        "bounds"
25    }
26
27    fn set_id(&mut self, _component: ComponentId) {}
28
29    fn as_any(&self) -> &dyn std::any::Any {
30        self
31    }
32
33    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
34        self
35    }
36
37    fn to_mms_ast(
38        &self,
39        _world: &crate::engine::ecs::World,
40    ) -> crate::scripting::ast::ComponentExpression {
41        use crate::engine::ecs::component::ce_helpers::*;
42        ce_call(
43            "Bounds",
44            "aabb",
45            vec![
46                array(nums(self.local.min.iter().map(|&v| v as f64))),
47                array(nums(self.local.max.iter().map(|&v| v as f64))),
48            ],
49        )
50    }
51}