extends Weapon
class_name Gatling

const BULLET_SCENE : PackedScene = preload("res://entities/mecha/weapons/gatling/bullet/bullet.tscn")

var max_bullet_rate : float = 100.
var min_bullet_rate : float = 1.
var bullet_dispersion : float = 0.03
var bullet_rate := min_bullet_rate
var bullet_acc = 0.1
var bullet_decc = 1.
var time_shooting = 0.

func process_fire(delta : float, mecha : Mecha, shoot : bool) -> bool:
    if shoot:
        bullet_rate = lerp(
            bullet_rate,
            max_bullet_rate,
            min(bullet_acc * delta, 1)
        )
    else : 
        bullet_rate = lerp(
            bullet_rate,
            min_bullet_rate,
            min(bullet_decc * delta, 1)
        )

    time_shooting += delta

    if shoot:
        for i in range(roundi(bullet_rate * time_shooting)):
            time_shooting -= 1/bullet_rate
            shoot_bullet(mecha)
    else :
        time_shooting = 0.
    return shoot

func shoot_bullet(mecha : Mecha,):
    var new_bullet : Bullet = BULLET_SCENE.instantiate()
    new_bullet.rotation.y = mecha.get_target_angle()
    new_bullet.position = mecha.get_projectile_spwan_point()
    mecha.add_recoil()
    add_child(new_bullet)
    new_bullet.rotation += Vector3(
        randf_range(-bullet_dispersion, bullet_dispersion),
        randf_range(-bullet_dispersion, bullet_dispersion),
        randf_range(-bullet_dispersion, bullet_dispersion)
    )
    
