nightshade 0.8.2

A cross-platform data-oriented game engine.
Documentation
use crate::ecs::bounding_volume::components::BoundingVolume;
use crate::ecs::generational_registry::registry_entry_by_name;
use crate::prelude::*;

fn resolve_bounding_volume(world: &World, mesh_name: &str) -> BoundingVolume {
    if let Some(mesh) = registry_entry_by_name(&world.resources.mesh_cache.registry, mesh_name)
        && let Some(bv) = &mesh.bounding_volume
    {
        return *bv;
    }
    BoundingVolume::from_mesh_type(mesh_name)
}

pub fn ensure_bounding_volumes(world: &mut World) {
    let render_mesh_entities: Vec<Entity> = world
        .query_entities(RENDER_MESH)
        .filter(|entity| !world.entity_has_components(*entity, BOUNDING_VOLUME))
        .collect();

    for entity in render_mesh_entities {
        let mesh_name = world
            .get_render_mesh(entity)
            .map(|rm| rm.name.clone())
            .unwrap_or_default();

        let bounding_volume = resolve_bounding_volume(world, &mesh_name);
        world.add_components(entity, BOUNDING_VOLUME);
        world.set_bounding_volume(entity, bounding_volume);
    }

    let instanced_mesh_entities: Vec<Entity> = world
        .query_entities(INSTANCED_MESH)
        .filter(|entity| !world.entity_has_components(*entity, BOUNDING_VOLUME))
        .collect();

    for entity in instanced_mesh_entities {
        let mesh_name = world
            .get_instanced_mesh(entity)
            .map(|im| im.mesh_name.clone())
            .unwrap_or_default();

        let bounding_volume = resolve_bounding_volume(world, &mesh_name);
        world.add_components(entity, BOUNDING_VOLUME);
        world.set_bounding_volume(entity, bounding_volume);
    }
}

pub fn query_number_of_lines(world: &World) -> usize {
    world
        .query_entities(LINES)
        .map(|entity| {
            world
                .get_lines(entity)
                .map(|lines| lines.lines.len())
                .unwrap_or(0)
        })
        .sum::<usize>()
}