nightshade-api 0.44.0

Procedural high level API for the nightshade game engine
Documentation
//! The kinematic character controller behind [`first_person`](crate::prelude::first_person):
//! read its grounded state and velocity, tune its movement, or drive it from
//! your own code instead of the built in input.

use nightshade::prelude::*;

/// Whether the character controller entity is standing on the ground this frame.
pub fn is_grounded(world: &World, entity: Entity) -> bool {
    world
        .core
        .get_character_controller(entity)
        .map(|controller| controller.grounded)
        .unwrap_or(false)
}

/// The character controller's current velocity, or zero if it has none.
pub fn controller_velocity(world: &World, entity: Entity) -> Vec3 {
    world
        .core
        .get_character_controller(entity)
        .map(|controller| controller.velocity)
        .unwrap_or_else(Vec3::zeros)
}

/// Sets the character's maximum move speed in units per second.
pub fn set_controller_speed(world: &mut World, entity: Entity, speed: f32) {
    if let Some(controller) = world.core.get_character_controller_mut(entity) {
        controller.max_speed = speed;
    }
}

/// Sets the character's jump strength.
pub fn set_controller_jump(world: &mut World, entity: Entity, jump_impulse: f32) {
    if let Some(controller) = world.core.get_character_controller_mut(entity) {
        controller.jump_impulse = jump_impulse;
    }
}

/// Drives the character from your own code instead of the engine input:
/// `movement` is the desired motion on the ground plane (x and z), and `jump`
/// requests a jump this frame. Useful for AI agents and networked players.
pub fn move_character(world: &mut World, entity: Entity, movement: Vec2, jump: bool) {
    if let Some(controller) = world.core.get_character_controller_mut(entity) {
        controller.external_movement = movement;
        controller.external_jump = jump;
    }
}