Skip to main content

mittens_engine/engine/ecs/component/
collision_shape.rs

1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3
4use crate::engine::ecs::system::model::collision_types::CollisionShape;
5
6/// Explicit collision shape definition.
7///
8/// Intended to be added as a child of a `CollisionComponent`.
9#[derive(Debug, Clone)]
10pub struct CollisionShapeComponent {
11    pub shape: CollisionShape,
12
13    component: Option<ComponentId>,
14}
15
16impl CollisionShapeComponent {
17    pub fn new(shape: CollisionShape) -> Self {
18        Self {
19            shape,
20            component: None,
21        }
22    }
23
24    pub fn cube() -> Self {
25        Self::new(CollisionShape::CUBE())
26    }
27
28    pub fn sphere() -> Self {
29        Self::new(CollisionShape::SPHERE())
30    }
31}
32
33impl Component for CollisionShapeComponent {
34    fn name(&self) -> &'static str {
35        "collision_shape"
36    }
37
38    fn set_id(&mut self, component: ComponentId) {
39        self.component = Some(component);
40    }
41
42    fn as_any(&self) -> &dyn std::any::Any {
43        self
44    }
45
46    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
47        self
48    }
49
50    fn to_mms_ast(
51        &self,
52        _world: &crate::engine::ecs::World,
53    ) -> crate::scripting::ast::ComponentExpression {
54        use crate::engine::ecs::component::ce_helpers::*;
55        match self.shape {
56            CollisionShape::Cube { half_extents } => ce_call(
57                "CollisionShape",
58                "cube",
59                vec![array(nums(half_extents.iter().map(|&v| v as f64)))],
60            ),
61            CollisionShape::Sphere { radius } => {
62                ce_call("CollisionShape", "sphere", vec![num(radius as f64)])
63            }
64        }
65    }
66}