extends KinematicBody2D

const FLOOR_NORMAL = Vector2(0, -1)
const SLOPE_SLIDE_STOP = 1.0
const GRAVITY = 900.0

export var move_speed = 30.0

var name = "boston"
var velocity = Vector2()
var floor_velocity = Vector2()
var anim = ""
var siding_left = false
var dir = 1
var hiden = false

func _fixed_process(delta):
	var new_anim = "idle"
	var new_siding_left = siding_left

	velocity.y += GRAVITY * delta
	velocity.x = move_speed * dir

	var motion = velocity * delta
	if motion != Vector2(0,0):
		motion = move( motion )

	if is_colliding():
		var normal = get_collision_normal()
		if normal.y < 0:
			if normal.y != -1:
				motion.y = 0
			motion = normal.slide( motion )

		if normal.x != 0:
			dir *= -1
			get_node("sprite").set_scale(Vector2(dir, 1))

		if motion != Vector2(0,0):
			move(motion)
		velocity = normal.slide( velocity )
#
#func death():
#	velocity.x = 0
#	set_fixed_process(false)
#	get_node("area").queue_free()
#	get_node("coll").queue_free()
#	get_node("damage_area").queue_free()
#	get_node("anim").play("death")
#	audio.play_sound("kick")

func hide():
	hiden = true
	set_fixed_process(false)
	audio.play_sound("sound12")
	get_node("anim").play("death")
	var timer = get_node("timer")
	timer.set_wait_time(4)
	timer.start()
	yield(timer, "timeout")
	wake_up()
	timer.set_wait_time(1)
	timer.start()
	yield(timer, "timeout")
	get_node("anim").play("walk")
	set_fixed_process(true)
	hiden = false
	
func wake_up():
	get_node("anim").play("wake_up")
	
func _ready():
	set_fixed_process(true)
	pass

func _on_area_body_enter( body ):
	if body extends KinematicBody2D and body != self and !hiden:
		body.rebound()
		hide()
