Skip to main content

mittens_engine/engine/ecs/component/
grid.rs

1use crate::engine::ecs::{ComponentId, component::Component};
2
3#[derive(Debug, Clone, Copy)]
4pub struct GridComponent {
5    pub spacing: f32,
6    pub size_x: u32,
7    pub size_z: u32,
8    pub enabled: bool,
9    pub hidden: bool,
10    pub selectable: bool,
11    component: Option<ComponentId>,
12}
13
14impl GridComponent {
15    pub const DEFAULT_SIZE_X: u32 = 16;
16    pub const DEFAULT_SIZE_Z: u32 = 16;
17
18    pub fn new(spacing: f32) -> Self {
19        Self {
20            spacing: spacing.max(1e-4),
21            size_x: Self::DEFAULT_SIZE_X,
22            size_z: Self::DEFAULT_SIZE_Z,
23            enabled: true,
24            hidden: false,
25            selectable: true,
26            component: None,
27        }
28    }
29
30    pub fn with_enabled(mut self, enabled: bool) -> Self {
31        self.enabled = enabled;
32        self
33    }
34
35    pub fn with_selectable(mut self, selectable: bool) -> Self {
36        self.selectable = selectable;
37        self
38    }
39
40    pub fn with_hidden(mut self, hidden: bool) -> Self {
41        self.hidden = hidden;
42        self
43    }
44
45    pub fn with_spacing(mut self, spacing: f32) -> Self {
46        self.spacing = spacing.max(1e-4);
47        self
48    }
49
50    pub fn with_size_x(mut self, size_x: u32) -> Self {
51        self.size_x = size_x.max(1);
52        self
53    }
54
55    pub fn with_size_z(mut self, size_z: u32) -> Self {
56        self.size_z = size_z.max(1);
57        self
58    }
59}
60
61impl Default for GridComponent {
62    fn default() -> Self {
63        Self::new(1.0)
64    }
65}
66
67impl Component for GridComponent {
68    fn set_id(&mut self, id: ComponentId) {
69        self.component = Some(id);
70    }
71
72    fn name(&self) -> &'static str {
73        "grid"
74    }
75
76    fn as_any(&self) -> &dyn std::any::Any {
77        self
78    }
79
80    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
81        self
82    }
83
84    fn to_mms_ast(
85        &self,
86        _world: &crate::engine::ecs::World,
87    ) -> crate::scripting::ast::ComponentExpression {
88        use crate::engine::ecs::component::ce_helpers::*;
89        ce_call("Grid", "spacing", vec![num(self.spacing as f64)])
90            .with_call("size_x", vec![num(self.size_x as f64)])
91            .with_call("size_z", vec![num(self.size_z as f64)])
92            .with_call("enabled", vec![b(self.enabled)])
93            .with_call("hidden", vec![b(self.hidden)])
94            .with_call("selectable", vec![b(self.selectable)])
95    }
96}