extends Node2D

export var hp = 10
onready var fish = get_node("/root/Tank/Fish")


func _ready():
	$TweenSpawn.interpolate_property(self, "modulate", Color(1,0,0,1), Color(1,1,1,1), 3, Tween.TRANS_EXPO, Tween.EASE_IN)
	$TweenSpawn.start()
	yield($TweenSpawn, "tween_completed")
	$Timer.start()


func _on_Hitbox_area_entered(area: Area2D) -> void:
	if area.is_in_group("Projectile"):
		hp -= 1
		if hp < 1:
			_die()


func _die() -> void:
	$DeadEye.visible = true
	fish.blackout()


func _on_Timer_timeout():
	_reposition()
	yield($Tween, "tween_completed")
	$AnimationPlayer.play("Stab")
	yield($AnimationPlayer, "animation_finished")
	$AnimationPlayer.play_backwards("Stab")
	yield($AnimationPlayer, "animation_finished")
	$Timer.wait_time = rand_range(.3,1.5)
	$Timer.start()


func _reposition() -> void:
	var tween_duration = rand_range(.3,1.5)
	$Tween.interpolate_property($Tentacle, "position:y", $Tentacle.position.y, rand_range(0, 900), tween_duration, Tween.TRANS_BACK, Tween.EASE_IN_OUT)
	$Tween.start()
	yield($Tween, "tween_completed")


func _on_Hurtbox_area_entered(area: Area2D) -> void:
	if area.is_in_group("Fish"):
		fish.belly_up("a tentacle")
