extends Node2D

@export var enabled = false
@onready var player = $"../../Character"

func _ready():
	visible = false

func _physics_process(delta):
	if enabled:
		#position.y -= delta * (35 + clamp(abs(player.global_position.y) - abs(global_position.y) - 500, 0, INF))
		global_position.y = lerpf(global_position.y, max(player.global_position.y - 100, -8425), delta / 5)
		
		visible = true
		$Particles.emitting = true
		if global_position.y <= -8425:
			enabled = false
		var alarmSync = roundi(fmod(Music.get_playback_position(), 1.86))
		if player.global_position.y < -8425 and Music.get_playback_position() < 59.1 and alarmSync == 2 and (Music.get_playback_position() < 33 or Music.get_playback_position() > 54):
			Music.seek(59)
		elif player.global_position.y > -8425 and Music.get_playback_position() > 59.1:
			Music.seek(18.5)
	#if enabled:
		#enabled = false
		#var tween = create_tween()
		#tween.tween_property(self, "global_position", global_position - Vector2(0, 000), 68)
		#visible = true
		#$Particles.emitting = true
		#print(tween)

func _process(delta):
	$LavaSprite.region_rect.position.x = fmod($LavaSprite.region_rect.position.x + delta * 100, 64)

func _on_area_2d_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
	if body.has_method("die"):
		Music.stop()
		body.die()
