nightshade-api 0.53.0

Procedural high level API for the nightshade game engine
Documentation
//! Reading input: the WASD helper and key axes, the mouse, and a gamepad. The
//! cube moves by the left stick when a pad is connected and by WASD otherwise,
//! and a one-shot Timer ticked on real time gates the jump so it cannot spam.

use nightshade_api::prelude::*;

struct Controls {
    player: Entity,
    hud: Entity,
    jump_cooldown: Timer,
}

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

fn setup(world: &mut World) -> Controls {
    spawn_floor(world, 16.0);
    let player = spawn_cube(world, vec3(0.0, 0.5, 0.0));
    set_color(world, player, TEAL);
    orbit_camera(world, vec3(0.0, 0.5, 0.0), 14.0);

    let panel = spawn_panel(world, ScreenAnchor::TopLeft, 480.0, 96.0);
    let hud = panel_label(world, panel, "input");
    set_text_size(world, hud, 15.0);
    spawn_text(
        world,
        "WASD / left stick move   Q/E spin   Space / south jump   mouse look",
        ScreenAnchor::BottomLeft,
    );

    Controls {
        player,
        hud,
        jump_cooldown: Timer::once(0.6),
    }
}

fn update(world: &mut World, controls: &mut Controls) {
    let step = delta_time(world);
    let pad = gamepad_connected(world);

    let move_vector = if pad {
        let stick = gamepad_left_stick(world);
        vec3(stick.x, 0.0, -stick.y)
    } else {
        wasd(world)
    };
    let here = position(world, controls.player);
    set_position(world, controls.player, here + move_vector * step * 6.0);

    let spin = axis(world, KeyCode::KeyQ, KeyCode::KeyE);
    rotate(world, controls.player, Vec3::y(), spin * step * 3.0);

    let jump =
        key_pressed(world, KeyCode::Space) || gamepad_button_pressed(world, GamepadButton::South);
    controls.jump_cooldown.tick(real_delta_time(world));
    if jump && controls.jump_cooldown.finished() {
        let burst_at = position(world, controls.player);
        emit_burst(world, burst_at, GOLD, 30);
        controls.jump_cooldown.reset();
    }

    let pointer = mouse_position(world);
    let look = if pad {
        gamepad_right_stick(world)
    } else {
        mouse_delta(world)
    };
    let dragging = mouse_down(world, MouseButton::Left);
    let east_held = gamepad_button_down(world, GamepadButton::East);
    let trigger = gamepad_axis(world, GamepadAxis::RightZ);

    set_text(
        world,
        controls.hud,
        &format!(
            "pad {pad}  pointer {:.0},{:.0}  move {:.2},{:.2}  look {:.2},{:.2}  drag {dragging}  east {east_held}  rz {trigger:.2}  frame {}",
            pointer.x,
            pointer.y,
            move_vector.x,
            move_vector.z,
            look.x,
            look.y,
            frame_count(world),
        ),
    );
}