extends Movement_StateReceiver

func _ready():
	var state_ref :State
	var state_name :String
	state_name = 'jump'
	state_ref = get_node(callable_state_machine).get_state_reference(state_name)
	#TODO Should probably assert here or something.
	state_ref.connect("state_entered", self, "_on_state_entered_" + state_name)

	# Lets see if this nutty thing works
#	var state_ref :State = get_node(callable_state_machine).get_state_reference('idle')
#	state_ref.connect("state_entered", self, "_on_state_entered_idle")
	# It works but node order really mattered.
	# State machine needed to be processed first probably for ready function.
	# good to know!

var player_number: int = 1

func get_movement_direction() -> float:
	#return Input.get_axis('move_left', 'move_right')
	desired_movement_vector.x = Input.get_axis("move_left_" + str(player_number), "move_right_" + str(player_number))
	return Input.get_axis("move_left_" + str(player_number), "move_right_" + str(player_number))

var _wants_jump :bool
func wants_jump() -> bool:	
	if Input.is_action_just_pressed("jump_" + str(player_number)):
		desired_movement_vector.y = UP
		_wants_jump = true
		return true
	else:
		_wants_jump = false
		return false

var _wants_shoot :bool
func wants_shoot() -> bool:	
	if Input.is_action_just_pressed("attack_" + str(player_number)):
		_wants_shoot = true
		return true
	else:
		_wants_shoot = false
		return false

var _wants_attack_secondary :bool
func wants_attack_secondary() -> bool:	
	if Input.is_action_just_pressed("attack_secondary_" + str(player_number)):
		_wants_attack_secondary = true
		return true
	else:
		_wants_attack_secondary = false
		return false

var _wants_dash :bool
func wants_dash() -> bool:	
	if Input.is_action_just_pressed("dash_" + str(player_number)):
		_wants_dash = true
		return true
	else:
		_wants_dash = false
		return false

var _wants_roll :bool
func wants_roll() -> bool:
	if Input.is_action_just_pressed("dash_" + str(player_number)) and Input.is_action_pressed("ui_up"):
		_wants_roll = true
		return true
	else:
		_wants_roll = false
		return false


	
#TODO: This is probably not a good way to do this.
# we want to check input once and make decisions based on
# velocity and desired movement.
#func _state_process_physics_input_idle():
#	get_movement_direction()
#
#func _state_process_physics_input_move():
#	get_movement_direction()

# Probably better to do it this way then allocating individual
# state functions that all do the same thing.
func process_physics_input(delta):
	# call the parent, Which would call the individuals
	.process_physics_input(delta)
	# then process movement controls
	physics_delta = delta
	get_movement_direction()
	wants_jump()
	wants_shoot()
	wants_attack_secondary()
	wants_dash()
	wants_roll()

func flip_sprite_to_movement_direction():
	if desired_movement_vector.x != 0.0:
		parent.transform.x.x = desired_movement_vector.x

func _state_process_physics_idle():
	if desired_movement_vector.y == UP:
		request_state_change.call_func('jump')
		
	#TODO: May need that 'bump' logic to make velocity work.
	if desired_movement_vector.x != 0: # and velocity.x != 0:
		request_state_change.call_func('move')

	if !parent.is_on_floor():
		#return fall_state
		request_state_change.call_func('fall')

	if _wants_roll == true:
		request_state_change.call_func('roll')

	if _wants_shoot == true:
		request_state_change.call_func('attack_shoot')
	
	if _wants_attack_secondary == true:
		request_state_change.call_func('attack_sword')

func _state_process_physics_attack_shoot():
	if current_state.animation_finished == true:
		request_state_change.call_func('idle')

	if !parent.is_on_floor():
		#return fall_state
		request_state_change.call_func('fall')


func _state_process_physics_attack_sword():
	if current_state.animation_finished == true:
		request_state_change.call_func('idle')

	if !parent.is_on_floor():
		#return fall_state
		request_state_change.call_func('fall')

func _state_process_physics_roll():
	if current_state.animation_finished == true:
		request_state_change.call_func('idle')

	# Force role in facing direction 
	#	instead of movement direction
	move_actor_as_desired(parent.transform.x.x)

	if !parent.is_on_floor():
		#return fall_state
		request_state_change.call_func('fall')


func _state_process_physics_land():
	if current_state.animation_finished == true:
		request_state_change.call_func('idle')

	if !parent.is_on_floor():
		#return fall_state
		request_state_change.call_func('fall')
	move_actor_as_desired()
	
func _state_process_physics_fall():
	flip_sprite_to_movement_direction()
	
	if parent.is_on_floor():
		#modifier.reference()
		#idle_state.modifier = landing_modifier
		request_state_change.call_func('land')
	
	move_actor_as_desired()

func _state_process_physics_jump():
	if desired_movement_vector.x != 0.0:
		parent.transform.x.x = desired_movement_vector.x
	if velocity.y > 0:
		request_state_change.call_func('fall')
		
	if parent.is_on_floor():
		#modifier.reference()
		#idle_state.modifier = landing_modifier
		request_state_change.call_func('idle')
	
	move_actor_as_desired()

func _state_process_physics_move():
	# flip sprite in direction
	flip_sprite_to_movement_direction()
	
	if desired_movement_vector.y == UP:
		request_state_change.call_func('jump')
	
	if desired_movement_vector.x == 0:
		request_state_change.call_func('idle')

	if !parent.is_on_floor():
		request_state_change.call_func('fall')
	
	move_actor_as_desired()

#func _on_state_entered_idle():
#	print("Movement State Idle State Entered!")

func _on_state_entered_jump():
	#TODO: Make this a state variable
	# I used to call it 'jump_force' like an idiot.
	velocity.y = -200


