nightshade-api 0.43.0

Procedural high level API for the nightshade game engine
Documentation
//! One call particle effects over the engine's GPU particle presets. Each
//! returns the emitter entity, which moves with [`set_position`]
//! (crate::prelude::set_position), parents like anything else, and stops with
//! [`despawn`](crate::prelude::despawn).

use nightshade::prelude::*;

/// A continuous fire at `position`. Pairs well with
/// [`emit_smoke`](emit_smoke) just above it and a [`point_light`]
/// (crate::prelude::point_light) for the glow.
pub fn emit_fire(world: &mut World, position: Vec3) -> Entity {
    spawn_emitter(world, ParticleEmitter::fire(position))
}

/// A continuous smoke column at `position`.
pub fn emit_smoke(world: &mut World, position: Vec3) -> Entity {
    spawn_emitter(world, ParticleEmitter::smoke(position))
}

/// A one shot burst of `count` particles in the given color at `position`.
/// The particles fall under gravity and fade. The emitter entity stays after
/// the burst, so despawn it or reuse it for the next burst.
pub fn emit_burst(world: &mut World, position: Vec3, color: [f32; 4], count: u32) -> Entity {
    spawn_emitter(
        world,
        ParticleEmitter::firework_explosion(
            position,
            Vec3::new(color[0], color[1], color[2]),
            count,
        ),
    )
}

fn spawn_emitter(world: &mut World, emitter: ParticleEmitter) -> Entity {
    let entity = spawn_entities(
        world,
        PARTICLE_EMITTER | LOCAL_TRANSFORM | LOCAL_TRANSFORM_DIRTY | GLOBAL_TRANSFORM | NAME,
        1,
    )[0];
    world.core.set_name(entity, Name("api::effect".to_string()));
    assign_local_transform(
        world,
        entity,
        LocalTransform {
            translation: emitter.position,
            ..Default::default()
        },
    );
    world.core.set_particle_emitter(entity, emitter);
    entity
}