extends KinematicBody2D

var health = 3
var moveSpeed = 0.8
var distance = Vector2(0,0)
var nextPosition = Vector2(512,300)
var velocity = Vector2(0,0)
const chased = false
var fall = false
var balloons = []

func _ready():
	balloons = $balloons.get_children()
	
func _process(delta):
	if health <= 0:
		fall = true
	if fall:
		velocity = Vector2(0, 400)
		collision_layer = 0
	else:
		distance = nextPosition - position
		var length = distance.length()
		velocity = movement(distance)
	if velocity:
		move_and_slide(velocity)

func movement(distance):
	pass

func shouldFlip():
	if nextPosition.x > get_viewport_rect().size.x/2:
		flip(false)
	else:
		flip(true)

func destroyBalloon():
	if(balloons.size() > 0):
		var toDestroy = balloons.front()
		toDestroy.destroy()
		balloons.remove(0)

func _on_VisibilityNotifier2D_screen_exited():
	queue_free()

func flip(facing):
	$icon.set_flip_h(facing)
	for balloon in balloons:
		balloon.set_flip_h(facing)
