extends Node2D
var hp = 5
var immune = false
var immune_timer = 0


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


func _ready():
#	$Piranha.set_process(false)
	$Wire/AnimatedSprite.frame = 0
	_spawn_piranha() #delete me later


func _on_Hitbox_area_entered(area):
	if area.is_in_group("Fish") and $InvulnTimer.time_left == 0:
		$AnimationPlayer.play("recoil")
		hp -= 1
		if hp == 4:
			$Wire/AnimatedSprite.frame = 0
		elif hp == 3:
			_spawn_piranha()
#			var piranha_scene = load("res://Hazards/Piranha/Piranha.tscn")
#			var piranha = piranha_scene.instance()
#			get_parent().add_child(piranha)
#			piranha.position = Vector2(300, -300)
			
		elif hp > 0 and hp < 3:
			$Wire/AnimatedSprite.frame = 1
		elif hp < 1:
			$Wire/AnimatedSprite.frame = 2
			get_parent().blackout()


func _spawn_piranha():
	print("Spawning the piranha now...")
	#$AnimationPlayer.play("drop")
	#yield($AnimationPlayer, "animation_finished")
	var piranha_scene = load("res://Hazards/Piranha/Piranha.tscn")
	var piranha = piranha_scene.instance()
#	piranha.set_process(false)
	#call_deferred("add_child", piranha)
	add_child(piranha)
	piranha.position = $Position2D.position
	#piranha.global_position.x = position.x - 300
	#piranha.global_position.y = position.y + 300
	#$TweenDrop.interpolate_property(piranha, "position:y", piranha.get_global_position().y, piranha.get_global_position().y+500, 1, Tween.TRANS_CIRC, Tween.EASE_IN)
	#$TweenDrop.start()
	#yield($TweenDrop, "tween_completed")
	print("Piranha is now at " + str(piranha.get_global_position()))
#	piranha.set_process(true)
