extends Area2D
onready var face_sprite = $FaceSprite
onready var air_sprite = $AirSprite
onready var air_audio_streamer = $AirAudioStreamer
onready var eating_timer = $EatingTimer
onready var eat_area = $EatCollisionShape2D
onready var eat_audio = $EatAudioStreamer

var face_closed = preload("res://blowface_closed.png")
var face_blowing = preload("res://blowface.png")
var face_eating = preload("res://eating_face.png")

signal stop_eating
signal ate_flake

var blower_position = self.position
# player_mode 0 is balance, 1 is eating
var player_mode = 0

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Input.is_action_just_pressed("ui_accept") and player_mode == 0:
		self.gravity = 1000
		face_sprite.texture = face_blowing
		air_audio_streamer.play()
		air_sprite.visible = true
		
	elif Input.is_action_just_released("ui_accept") and player_mode == 0:
		self.gravity = 0
		face_sprite.texture = face_closed
		air_audio_streamer.stop()
		air_sprite.visible = false
		
	if Input.is_action_pressed("ui_right"):
		blower_position.x += 12
		self.position = blower_position
		
	if Input.is_action_pressed("ui_left"):
		blower_position.x -= 12
		self.position = blower_position
	pass

func _on_SeeSaw_start_eating():
	print("start eating event handler Blower.gd")
	self.gravity_vec = Vector2(0, 1)
	self.gravity = -30000
	air_audio_streamer.stop()
	air_sprite.visible = false
	self.gravity = 0
	face_sprite.texture = face_eating
	player_mode = 1
	eating_timer.start()
	pass

func _on_EatingTimer_timeout():
	print('eating timer timeout in Blower.gd')
	self.gravity_vec = Vector2(0, -1)
	self.gravity = 0
	face_sprite.texture = face_closed
	player_mode = 0
	emit_signal("stop_eating")
	pass

func _on_EatArea2D_body_entered(body):
	if body.has_method("remove") and player_mode == 1:
		eat_audio.play()
		body.remove()
		emit_signal("ate_flake")
	pass # Replace with function body.
