Skip to main content

lotus_script/
macros.rs

1/// Registriert einen LOTUS-Script-Typ und exportiert die WASM-Einstiegspunkte.
2///
3/// Registers a LOTUS script type and exports the WASM entry points.
4///
5/// Expandiert zu `init`, `register_actions`, `tick` und `late_tick`, die an die [`crate::Script`]-Implementierung
6/// des angegebenen Typs delegieren. Eingehende Engine-Nachrichten werden in `late_tick` über [`crate::Script::on_message`] zugestellt.
7/// Der Typ muss [`Default`] implementieren.
8///
9/// Expands to `init`, `register_actions`, `tick`, and `late_tick` functions that
10/// delegate to the [`crate::Script`] implementation on the given type. Incoming engine
11/// messages are delivered in `late_tick` via [`crate::Script::on_message`].
12/// The type must implement [`Default`].
13///
14/// # Example
15///
16/// ```no_run
17/// # #[cfg(target_arch = "wasm32")]
18/// # {
19/// use lotus_script::{script, Script};
20///
21/// #[derive(Default)]
22/// struct MyScript;
23///
24/// impl Script for MyScript {
25///     fn tick(&mut self) {
26///         // ...
27///     }
28/// }
29///
30/// script!(MyScript);
31/// # }
32/// ```
33#[macro_export]
34macro_rules! script {
35    ($t:ident) => {
36        thread_local! {
37            static SCRIPT: ::std::sync::LazyLock<::std::sync::Mutex<$t>> =
38                ::std::sync::LazyLock::new(Default::default);
39        }
40
41        #[no_mangle]
42        pub fn init() {
43            SCRIPT.with(|s| s.lock().unwrap().init());
44        }
45
46        #[no_mangle]
47        pub fn register_actions() {
48            let actions = $t::actions();
49            $crate::action::register_many(&actions);
50        }
51
52        #[no_mangle]
53        pub fn tick() {
54            SCRIPT.with(|s| s.lock().unwrap().tick());
55        }
56
57        #[no_mangle]
58        pub fn late_tick() {
59            for message in $crate::message::get() {
60                SCRIPT.with(|s| s.lock().unwrap().on_message(message));
61            }
62        }
63    };
64}