extends Node2D

@onready var _animated_sprite = $AnimatedSprite2D

const idle_pos = 0.0
const shoot_pos = 60.0
const speed_idle = 3
const speed_shoot = 10

var go_idle
var go_shoot

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	go_idle = false
	go_shoot = false

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	
	if (go_idle):
		position.x = floorf(lerp(position.x, idle_pos, delta * speed_idle))
		if (abs(position.x - idle_pos) <= 1) : go_idle = false
		
	elif (go_shoot):
		position.x = floorf(lerp(position.x, shoot_pos, delta * speed_shoot))
		if (abs(position.x - shoot_pos) <= 1) : go_shoot = false
	
	if(get_parent().get_isAlive()):
		return

	if(Input.is_action_pressed("suck")):
		print("sucking")
		
		get_parent().set_isAlive(true)
		_animated_sprite.play("inhale")
		#_animated_sprite.animation.animation_set_next("inhale", "idle")


func _on_animated_sprite_2d_animation_finished() -> void:
	if (_animated_sprite.animation == "inhale"):
		_animated_sprite.play("shoot")
		go_shoot = true
		go_idle = false
		return
	if (_animated_sprite.animation == "shoot"):
		_animated_sprite.play("idle")


func _on_animated_sprite_2d_frame_changed() -> void:
	if (_animated_sprite.animation == "shoot"):
		if (_animated_sprite.frame == 3):
			get_parent().shoot_bubble()
		elif(_animated_sprite.frame == 4):
			go_idle = true
			go_shoot = false
