extends Node2D

var scroll_multiplier: float = 450
var scroll_multiplier_active = false

@onready var parallax_nodes = get_tree().get_nodes_in_group("parallax_clouds")


func _ready():
  EventBus.cloud_speed.connect(_on_cloud_speed)

func _on_cloud_speed(active: bool):
  if active == true:
    for node in parallax_nodes:
        if node is Parallax2D:
          var current_autoscroll = node.get_autoscroll()
          var new_autoscroll = current_autoscroll.y + scroll_multiplier
          var tween = create_tween().set_trans(Tween.TRANS_QUAD).parallel()
          tween.tween_property(node, "autoscroll:y", new_autoscroll, 5)
          tween.tween_property(node,  "modulate",Color(0.40,0.40,0.40), 2.5)
          
    var coffee_dip = create_tween().set_trans(Tween.TRANS_QUAD).parallel()
    coffee_dip.tween_property($CoffeeBuoy, "modulate", Color(0.40,0.40,0.40,0),5)
    await coffee_dip.finished
    $CoffeeBuoy.hide()
    scroll_multiplier_active = true
    
  if active == false:
    if scroll_multiplier_active == true: 
      for node in parallax_nodes:
        if node is Parallax2D:
          var current_autoscroll = node.get_autoscroll()
          var new_autoscroll = current_autoscroll.y - scroll_multiplier
          var tween = create_tween().set_trans(Tween.TRANS_QUAD).parallel()
          tween.tween_property(node, "autoscroll:y", new_autoscroll, 5)
          tween.tween_property(node,  "modulate",Color(1,1,1), 1.5)
          
      $CoffeeBuoy.show()
      var coffee_dip = create_tween().set_trans(Tween.TRANS_QUAD).parallel()
      coffee_dip.tween_property($CoffeeBuoy, "modulate", Color(1,1,1,1),5)
      $CoffeeBuoy.show()
      scroll_multiplier_active = false
      
    if scroll_multiplier_active == false:
      return
