Skip to main content

mittens_engine/engine/ecs/component/
input.rs

1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3
4/// Input component that responds to keyboard input (WASD).
5#[derive(Debug, Clone, Default)]
6pub struct InputComponent {
7    pub speed: f32,
8}
9
10impl InputComponent {
11    pub fn new() -> Self {
12        Self { speed: 0.02 }
13    }
14
15    pub fn with_speed(mut self, speed: f32) -> Self {
16        self.speed = speed;
17        self
18    }
19}
20
21impl Component for InputComponent {
22    fn name(&self) -> &'static str {
23        "input"
24    }
25
26    fn as_any(&self) -> &dyn std::any::Any {
27        self
28    }
29
30    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
31        self
32    }
33
34    fn init(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter, component: ComponentId) {
35        emit.push_intent_now(
36            component,
37            crate::engine::ecs::IntentValue::RegisterInput {
38                component_ids: vec![component],
39            },
40        );
41    }
42
43    fn to_mms_ast(
44        &self,
45        _world: &crate::engine::ecs::World,
46    ) -> crate::scripting::ast::ComponentExpression {
47        use crate::engine::ecs::component::ce_helpers::*;
48        ce_call("Input", "speed", vec![num(self.speed as f64)])
49    }
50}