extends State

export (NodePath) var jump_node
export (NodePath) var fall_node
export (NodePath) var idle_node

onready var jump_state: State = get_node(jump_node)
onready var fall_state: State = get_node(fall_node)
onready var idle_state: State = get_node(idle_node)

func process_frame(_delta: float) -> State:
	if move_component.wants_jump() and parent.is_on_floor():
		return jump_state

	move_component.get_movement_direction()

	return null


func process_physics(delta: float) -> State:
#	if move_component.wants_jump() and parent.is_on_floor():
#		return jump_state

	parent.velocity.y += gravity * delta

#	var movement = move_component.get_movement_direction() * move_speed
#
#	# only detecting input not actual movement
#	if movement == 0:
#		return idle_state		
	
#	parent.velocity.x = movement
#	parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1))

	parent.velocity.x = move_component.desired_movement_vector.x * move_speed
	parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1))

	if parent.velocity.x == 0.0:
		return idle_state
	else:
		if parent.velocity.x > 0:
			parent.direction.x = 1
		elif parent.velocity.x < 0:
			parent.direction.x = -1
	

	if move_component.flipped_sprite == true:
		animations.flip_h = parent.direction.x > 0
		if parent.direction.x < 0:
			animations.offset.x = -18
		else:
			animations.offset.x = 0
	else:
		animations.flip_h = parent.direction.x < 0
		animations.offset.x = 0


	# Another approach of changing state based on actual achieved velocity 	
#	if parent.velocity.x == 0:
#		return idle_state
	
	if !parent.is_on_floor():
		return fall_state
	return null
