extends Node2D

const Droplet := preload("res://Droplet.tscn")

var rain_density := 0.0 # drops/s

func _ready() -> void:
    _change_rain()

func _physics_process(dt: float) -> void:
    for _i in range(max(0, int(roundf(rain_density * dt + .5 - randf())))):
        _spawn_droplet()

func _change_rain() -> void:
    var n1 := randfn(0.0, 1.0)
    var n2 := randfn(0.0, 1.0)
    var target_density := clampf(10.0 * n1 * n1, 2.0, 150.0) - 2.0
    create_tween().set_process_mode(Tween.TWEEN_PROCESS_PHYSICS) \
        .tween_property(self, "rain_density", target_density, .5 + 5.0 * randf()) \
        .set_ease(Tween.EASE_IN_OUT) \
        .set_trans(Tween.TRANS_QUAD) \
        .set_delay(10.0 + minf(4.0 * n2 * n2, 30.0)) \
        .finished.connect(_change_rain)
    
func _spawn_droplet() -> void:
    var d := Droplet.instantiate()
    d.scale = Vector2(0.75 + 0.5 * randf(), 0.75 + 0.5 * randf())
    d.position = Vector2(100.0 + 1350.0 * randf(), -(50.0 + 400.0 * randf()))
    d.linear_velocity = Vector2(-300.0 * (0.75 + 0.5 * randf()), 1250.0 * (0.75 + 0.5 * randf()))
    d.z_index = 1
    if randf() < .5:
        d.collision_mask = 0
        if randf() < .5:
            d.z_index = 0
    add_child(d)

func _on_area_2d_body_entered(body: Node2D) -> void:
    body.apply_central_impulse(Vector2(0.5, -0.5) * 5000)
