nightshade-api 0.53.0

Procedural high level API for the nightshade game engine
Documentation
//! Building a prefab from a configured entity subtree, then stamping copies of
//! it across a grid. The template also round-trips through bytes to prove it is
//! self contained.

use nightshade_api::prelude::*;

fn main() {
    run_scene(|world| {
        set_window_title(world, "Prefabs");
        set_background(world, Background::Nebula);
        spawn_floor(world, 16.0);

        let totem = build_totem(world);
        let prefab = make_prefab(world, totem);
        despawn(world, totem);

        for row in 0..3 {
            for column in 0..3 {
                let position = vec3(column as f32 * 4.0 - 4.0, 0.0, row as f32 * 4.0 - 4.0);
                spawn_prefab(world, &prefab, position);
            }
        }

        let bytes = save_prefab(&prefab).expect("prefab serializes");
        let restored = load_prefab(&bytes).expect("prefab loads");
        spawn_prefab(world, &restored, vec3(0.0, 0.0, 8.0));

        spawn_text(world, "9 stamped + 1 from bytes", ScreenAnchor::BottomLeft);
    })
    .unwrap();
}

fn build_totem(world: &mut World) -> Entity {
    let root = spawn_group(world, vec3(0.0, 0.0, 0.0));

    let base = spawn_object(
        world,
        Object {
            shape: Shape::Cylinder,
            position: vec3(0.0, 0.5, 0.0),
            scale: vec3(0.8, 0.5, 0.8),
            color: STEEL,
            ..Object::default()
        },
    );
    set_parent(world, base, Some(root));

    let crystal = spawn_object(
        world,
        Object {
            shape: Shape::Cone,
            position: vec3(0.0, 1.6, 0.0),
            scale: vec3(0.5, 1.2, 0.5),
            color: TEAL,
            ..Object::default()
        },
    );
    set_emissive(world, crystal, [0.1, 0.7, 0.9], 2.5);
    set_parent(world, crystal, Some(root));

    let halo = spawn_object(
        world,
        Object {
            shape: Shape::Torus,
            position: vec3(0.0, 2.6, 0.0),
            scale: vec3(0.5, 0.5, 0.5),
            color: GOLD,
            ..Object::default()
        },
    );
    set_emissive(world, halo, [1.0, 0.8, 0.2], 2.0);
    set_parent(world, halo, Some(root));

    root
}