
extends KinematicBody2D


# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var animacion
export var speed = 100
#const gravity = 9.81
const gravity = 1
#var near_stair
var is_using_stairs
var going_down
var going_up
var in_stairs = false
var on_switch = false
var switch
# Called when the node enters the scene tree for the first time.
func _ready():
	animacion=$sprite
	animacion.stop()
	
	is_using_stairs = false
#	is_using_stairs	= false
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	var movement = Vector2.ZERO
	if Input.is_action_pressed("ui_left"):
		movement.x += -1
		animacion.flip_h=true
	if Input.is_action_pressed("ui_right"):
		movement.x += 1
		animacion.flip_h=false
	pass
	if movement.x!=0 and (animacion.animation!="run" or not animacion.is_playing()) and not dead:
		animacion.play("run")
		if not $AudioStreamPlayer.playing:
			$AudioStreamPlayer.play()
	
	if dead:
		animacion.play("death")
	if  dead and (animacion.animation!="death" or not animacion.is_playing()):
		animacion.play("death")
		
#	if movement.x==0 and animacion.stop():
#		animacion.play("idle")
	if  movement.x==0 and (animacion.animation!="idle" or not animacion.is_playing()) and not dead:
		animacion.play("idle")
		if $AudioStreamPlayer.playing:
			$AudioStreamPlayer.stop()
			
#	if Input.is_action_pressed("ui_up") and is_using_stairs:
	var up_button = Input.is_action_pressed("ui_up")
	var down_button = Input.is_action_pressed("ui_down")
	if (up_button and going_up) or (down_button and going_down):
#		set_collision_mask_bit(17,true)
		z_index = -1
		set_collision_mask_bit(18,true)
		set_collision_mask_bit(19,false)
#		is_using_stairs = true
		
#	if (not up_button and not in_stairs):
#	if (not up_button and not down_button and not in_stairs):
	if (not in_stairs):
		z_index = 1
#		set_collision_mask_bit(17,false)
		set_collision_mask_bit(18,false)
		set_collision_mask_bit(19,true)
#		set_collision_mask_bit(2,false)
		pass
	movement.y = gravity
	move_and_slide_with_snap(movement*speed, Vector2(0,1000), Vector2.UP, false)
	
	if (Input.is_action_just_pressed("ui_select") and on_switch):
		switch.activate()




func near_stair(b):
	is_using_stairs = b

func _on_Area2D_body_entered(body):
	pass # Replace with function body.


func _on_Area2D_body_exited(body):
	pass # Replace with function body.


func _on_Area2D_area_entered(area):
	if area.name == "stair":
		in_stairs = true
	if area.name == "up_stair":
		near_stair(true)
		going_down = true
		pass
	if area.name == "down_stair":
		near_stair(true)
		going_up = true
		pass
	if area.name == "switch":
		on_switch = true
		switch = area
	pass # Replace with function body.


func _on_Area2D_area_exited(area):
	if area.name == "stair":
		in_stairs = false
	if area.name == "up_stair":
		near_stair(false)
		going_down = false
	if area.name == "down_stair":
		near_stair(false)
		going_up = false
		pass
	if area.name == "switch":
		on_switch = false
	pass # Replace with function body.
	
var diyng_shooted = false
var dead = false
func shooted():
	if not diyng_shooted:
		diyng_shooted = true
		yield(get_tree().create_timer(1.0), "timeout") #controlado por distancias????? o velocidad propia???????????????
		die()

func poisoned():
	yield(get_tree().create_timer(2.0), "timeout")
	die()
func die():
	if not dead:
		speed = 0
		collision_layer = 0
		dead = true
		
		yield(get_tree().create_timer(4.0), "timeout")
		get_parent().remove_child(self)


func _on_AudioStreamPlayer_finished():
	$AudioStreamPlayer.playing = false
	pass # Replace with function body.
