Skip to main content

mittens_engine/engine/ecs/component/
clock.rs

1use super::Component;
2use crate::engine::ecs::ComponentId;
3
4/// Controls the global beat clock tempo (BPM).
5///
6/// Intended to be singleton-like: the most recently registered ClockComponent wins.
7#[derive(Debug, Clone, Copy)]
8pub struct ClockComponent {
9    pub bpm: f64,
10
11    component: Option<ComponentId>,
12}
13
14impl ClockComponent {
15    pub fn new() -> Self {
16        Self {
17            bpm: 120.0,
18            component: None,
19        }
20    }
21
22    pub fn with_bpm(mut self, bpm: f64) -> Self {
23        self.bpm = bpm;
24        self
25    }
26
27    pub fn id(&self) -> Option<ComponentId> {
28        self.component
29    }
30}
31
32impl Default for ClockComponent {
33    fn default() -> Self {
34        Self::new()
35    }
36}
37
38impl Component for ClockComponent {
39    fn set_id(&mut self, component: ComponentId) {
40        self.component = Some(component);
41    }
42
43    fn name(&self) -> &'static str {
44        "clock"
45    }
46
47    fn init(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter, component: ComponentId) {
48        emit.push_intent_now(
49            component,
50            crate::engine::ecs::IntentValue::RegisterClock {
51                component_ids: vec![component],
52            },
53        );
54    }
55
56    fn as_any(&self) -> &dyn std::any::Any {
57        self
58    }
59
60    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
61        self
62    }
63
64    fn to_mms_ast(
65        &self,
66        _world: &crate::engine::ecs::World,
67    ) -> crate::scripting::ast::ComponentExpression {
68        use crate::engine::ecs::component::ce_helpers::*;
69        ce_call("Clock", "bpm", vec![num(self.bpm)])
70    }
71}