oxeon 0.0.6

Rust-like scripting language for modding Bevy games, compiled to sandboxed WebAssembly.
Documentation
//! A headless game whose health regeneration is written as an Oxeon mod
//! (`examples/assets/mods/regen.ox`).
//!
//! ```sh
//! cargo run -p oxeon --example regen
//! cargo run -p oxeon --example regen --features hot-reload   # edit the mod while it runs
//! ```

use std::time::Duration;

use bevy_app::{App, AppExit, ScheduleRunnerPlugin, Startup, TaskPoolPlugin, Update};
use bevy_asset::{AssetPlugin, AssetServer};
use bevy_ecs::prelude::*;
use bevy_log::LogPlugin;
use bevy_reflect::Reflect;
use bevy_time::{Time, TimePlugin};
use oxeon::prelude::*;

#[derive(Component, Reflect)]
#[reflect(Component)]
struct Health {
    current: f32,
    max: f32,
}

#[derive(Component, Reflect)]
#[reflect(Component)]
struct Regen {
    per_sec: f32,
}

#[derive(Component)]
struct Name(&'static str);

fn setup(mut commands: Commands, assets: Res<AssetServer>) {
    commands.spawn((
        Name("knight"),
        Health {
            current: 20.0,
            max: 100.0,
        },
        Regen { per_sec: 15.0 },
    ));
    commands.spawn((
        Name("troll"),
        Health {
            current: 50.0,
            max: 200.0,
        },
        Regen { per_sec: 40.0 },
    ));
    commands.spawn((
        Name("statue"),
        Health {
            current: 10.0,
            max: 10.0,
        },
    ));
    commands.spawn(OxeonMod(assets.load("mods/regen.ox")));
}

/// Prints everyone's health twice a second, and stops after a few seconds
/// (unless hot reloading is on).
fn report(
    time: Res<Time>,
    mut next: Local<f32>,
    q: Query<(&Name, &Health)>,
    mut exit: MessageWriter<AppExit>,
) {
    if time.elapsed_secs() < *next {
        return;
    }
    *next += 0.5;
    let line: Vec<String> = q
        .iter()
        .map(|(name, hp)| format!("{} {:>5.1}/{:<5.1}", name.0, hp.current, hp.max))
        .collect();
    println!("{:>4.1}s  {}", time.elapsed_secs(), line.join("   "));
    if !cfg!(feature = "hot-reload") && time.elapsed_secs() >= 5.0 {
        exit.write(AppExit::Success);
    }
}

fn main() -> AppExit {
    App::new()
        .add_plugins((
            TaskPoolPlugin::default(),
            TimePlugin,
            ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(1.0 / 30.0)),
            LogPlugin::default(),
            AssetPlugin {
                file_path: "examples/assets".into(),
                ..Default::default()
            },
            OxeonPlugin {
                // Editors read this to know the game's types in the mods.
                bindings_file: Some(
                    std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                        .join("examples/assets/oxeon-bindings.ox"),
                ),
                ..Default::default()
            },
        ))
        .expose_to_mods::<Health>()
        .expose_to_mods::<Regen>()
        .add_systems(Startup, setup)
        .add_systems(Update, report)
        .run()
}