extends Sprite2D
class_name Gun

var Bullet = preload("res://scene/entity/bullet.tscn")
var should_rotate = true

func _ready():
    $"..".connect("shoot", _on_shoot)

func _process(delta):
    var victim = $"..".victim
    if $"..".dead or victim == null or victim.dead:
        set_rotation(0)
    elif victim != null and should_rotate:
        look_at(victim.get_global_position())
        rotate(PI/2)

func _on_shoot(victim: UFO):
    if victim != null and not $"..".dead:
        var bullet: Bullet = Bullet.instantiate()
        bullet.target_node = victim
        bullet.set_position(get_global_position())
        bullet.set_modulate($"..".bullet_color)
        bullet.set_scale(Vector2(0.2, 0.2))
        bullet.damage = $"..".attack_damage
        bullet.base_speed = $"..".bullet_base_speed_u
        bullet.speed_bound = $"..".bullet_base_speed_l
        $"../../../Bullets".add_child(bullet)
