use crate::ecs::world::World;

pub fn update_particle_emitters(world: &mut World) {
    let delta_time = world.resources.window.timing.delta_time;
    let emitter_entities: Vec<_> = world
        .core
        .query_entities(crate::ecs::PARTICLE_EMITTER)
        .collect();
    let _span = tracing::info_span!("particles", emitters = emitter_entities.len()).entered();

    for entity in emitter_entities {
        if let Some(emitter) = world.core.get_particle_emitter_mut(entity) {
            if !emitter.enabled {
                continue;
            }

            if emitter.one_shot && emitter.has_fired {
                continue;
            }

            if emitter.burst_count > 0 && !emitter.has_fired {
                emitter.accumulated_spawn = emitter.burst_count as f32;
                emitter.has_fired = true;
            } else if emitter.spawn_rate > 0.0 {
                emitter.accumulated_spawn += emitter.spawn_rate * delta_time;
            }
        }
    }
}
