extends Node2D

enum PlayerAnimation {
	IDLE, WALK, RUN, JUMP, FALL, BRAK, MEDITATE,
	SHOOT, WALK_SHOOT, JUMP_SHOOT, DOUBLE_JUMP, WALL_JUMP_HOLD, WALL_JUMP,
	JETPACK_ACTIVATE, JETPACK_IDLE, JETPACK_WALK, JETPACK_SHOOT, JETPACK_HIT, JETPACK_DIE, JETPACK_OFF,	
	LADDER_REACH, LADDER_WALK, LADDER_END,
	HIT, HIT_IN_AIR, DEAD, DEAD_FALL, DEAD_LAND,
}

onready var player = self.get_parent()
onready var sprite = self.get_parent().get_node("AnimatedSprite")
var facingRight = true

var previous_animation = PlayerAnimation.IDLE

export (PlayerAnimation) var playerAnimation = PlayerAnimation.IDLE

func get_name():
	return PlayerAnimation.keys()[playerAnimation]

func set_animation(animation_type):
	if playerAnimation != animation_type:
		previous_animation = playerAnimation
		playerAnimation = animation_type
		animate()
	

# Return a Vector2 based on the player's direction for velocity multiplication
func get_direction_vector2():
	var left = false
	var right = true
	var up = false
	var down = false
	var vectX = 0.0
	var vectY = 0.0
	
	if not facingRight:
		right = false
		left = true
	
	if Controller.is_up():
		up = true
	if Controller.is_down():
		up = false
		down = true
	
	if right:
		vectX = 1.0
	elif left:
		vectX = -1.0
	
	if up:
		vectY = -1.0
	elif down:
		vectY = 1.0
	
	#special
	if up and not Controller.is_right() and not Controller.is_left():
		vectX = 0.0
	
	return Vector2(vectX, vectY)

func animate():
	if previous_animation == playerAnimation:
		return
	
	# TODO: change animation in sprite here
	sprite.play(PlayerAnimation.keys()[playerAnimation])
	
	
	previous_animation = playerAnimation

# warning-ignore:unused_argument
func _physics_process(delta):
	#animate()
	get_direction_vector2()
	
	if !player:
		player = self.get_parent()
	if !sprite:
		sprite = self.get_parent().get_node("Sprite")
		
	#if (player.velocity.x < 0 and sprite.scale.x != -1):
	#	sprite.scale.x = -1
	#elif (player.velocity.x > 0 and sprite.scale.x != 1):
	#	sprite.scale.x = 1
	if facingRight:
		sprite.scale.x = 1
	else:
		sprite.scale.x = -1

func _ready():
	animate()
