nightshade-api 0.49.0

Procedural high level API for the nightshade game engine
Documentation
//! Reacting to the engine event stream and a typed in-process message bus.
//! Animation markers fire footstep events; B broadcasts a Ping read next frame.

use nightshade_api::prelude::*;

struct Ping(u32);

struct Events {
    fox: Entity,
    hud: Entity,
    footsteps: u32,
    pings: u32,
    next_ping: u32,
}

fn main() {
    run(setup, update).unwrap();
}

fn setup(world: &mut World) -> Events {
    set_window_title(world, "Events");
    spawn_floor(world, 10.0);

    let fox = spawn_model(
        world,
        include_bytes!("../../../assets/gltf/fox.glb"),
        vec3(0.0, 0.0, 0.0),
    );
    set_scale(world, fox, vec3(0.02, 0.02, 0.02));
    if !play_animation_named(world, fox, "Walk") {
        play_animation(world, fox, 0);
    }
    set_animation_looping(world, fox, true);

    if !animation_clips(world, fox).is_empty() {
        add_animation_event(world, fox, 0, 0.5, "footstep");
    }

    watch_events(world, true);

    let hud = spawn_text(world, "footsteps 0   pings 0", ScreenAnchor::TopLeft);
    set_text_size(world, hud, 26.0);

    Events {
        fox,
        hud,
        footsteps: 0,
        pings: 0,
        next_ping: 0,
    }
}

fn update(world: &mut World, state: &mut Events) {
    for event in drain_events(world) {
        match event {
            Event::AnimationEvent { entity, name } if entity == state.fox && name == "footstep" => {
                state.footsteps += 1;
                emit_burst(world, position(world, state.fox), GOLD, 24);
            }
            _ => {}
        }
    }

    for ping in drain_messages::<Ping>(world) {
        state.pings += ping.0;
    }

    if key_pressed(world, KeyCode::KeyB) {
        state.next_ping += 1;
        broadcast(world, Ping(state.next_ping));
    }

    let cycle = current_cycle(world);
    set_text(
        world,
        state.hud,
        &format!(
            "footsteps {}   pings {}   cycle {cycle}",
            state.footsteps, state.pings
        ),
    );
}