lotussim-script 0.8.4

A library for creating LOTUS-Simulator scripts.
Documentation
/// Registriert einen LOTUS-Script-Typ und exportiert die WASM-Einstiegspunkte.
///
/// Registers a LOTUS script type and exports the WASM entry points.
///
/// Expandiert zu `init`, `register_actions`, `tick` und `late_tick`, die an die [`crate::Script`]-Implementierung
/// des angegebenen Typs delegieren. Eingehende Engine-Nachrichten werden in `late_tick` über [`crate::Script::on_message`] zugestellt.
/// Der Typ muss [`Default`] implementieren.
///
/// Expands to `init`, `register_actions`, `tick`, and `late_tick` functions that
/// delegate to the [`crate::Script`] implementation on the given type. Incoming engine
/// messages are delivered in `late_tick` via [`crate::Script::on_message`].
/// The type must implement [`Default`].
///
/// # Example
///
/// ```no_run
/// # #[cfg(target_arch = "wasm32")]
/// # {
/// use lotus_script::{script, Script};
///
/// #[derive(Default)]
/// struct MyScript;
///
/// impl Script for MyScript {
///     fn tick(&mut self) {
///         // ...
///     }
/// }
///
/// script!(MyScript);
/// # }
/// ```
#[macro_export]
macro_rules! script {
    ($t:ident) => {
        thread_local! {
            static SCRIPT: ::std::sync::LazyLock<::std::sync::Mutex<$t>> =
                ::std::sync::LazyLock::new(Default::default);
        }

        #[no_mangle]
        pub fn init() {
            SCRIPT.with(|s| s.lock().unwrap().init());
        }

        #[no_mangle]
        pub fn register_actions() {
            let actions = $t::actions();
            $crate::action::register_many(&actions);
        }

        #[no_mangle]
        pub fn tick() {
            SCRIPT.with(|s| s.lock().unwrap().tick());
        }

        #[no_mangle]
        pub fn late_tick() {
            for message in $crate::message::get() {
                SCRIPT.with(|s| s.lock().unwrap().on_message(message));
            }
        }
    };
}