extends AnimatedSprite_StateReceiver

var attack_sword: StateModifierAnimatedActor

func _ready():
	# Lets see if this nutty thing works
	var state_ref :State
	#state_ref = get_node(callable_state_machine).get_state_reference('fall')
	#state_ref.connect("state_entered", self, "_on_state_entered_fall")
	state_ref = get_node(callable_state_machine).get_state_reference('attack_sword')
	state_ref.connect("state_exited", self, "_on_state_exited_attack_sword")
	
	attack_sword = StateModifierAnimatedActor.new()
	attack_sword.setup('sword_drawn',StateModifier.TYPE.ANIMATION_SUFFIX,2.0)
	add_child(attack_sword.timeout)
	#attack_sword.animation_name = 'PoopenStein'
	attack_sword.animation_suffix = 'attack-sword'
	add_state_modifier.call_func(attack_sword)
	
	# Testing
	#speed_scale = .1


# I don't need this right now but I proved it can be done!
#func _state_process_idle():
#	print("I got called!")

#func _state_process_fall():
#	print("I got called!")

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

## Processing order is interesting here.
#Got call to enter state fall
#2? got the state change signal jump
#1? got the state change signal fall
#It's an animated Actor state!
## State signal comes before state machine. I'm not sure that's
## Good. Maybe I shouldn't subscribe to signals emitted by the state.


#func _on_state_entered_fall():
#	print ("State Enter Signal ", current_state.name)
#	frame = 2
#	stop()

#func _on_state_change_idle():
#	change_animation()
#	if previous_state_name == 'attack_sword':
#		animation = 'idle-attack-sword'

func _on_state_change_fall():
	change_animation(2)
	stop()

var climb_latch:bool = false
func _state_process_climb():
	UiManager.debug_text =  str(frame) 
	if current_state.movement_stopped == true and climb_latch == false:
		stop()
		if frame != 0 and (frame + 1) >=  frames.get_frame_count(animation):
			frame = 0
		else:
			frame = frame + 1
		climb_latch = true
	if current_state.movement_stopped == false and climb_latch == true:
		play()
		climb_latch = false

#func _on_state_change_ledge_grab():
#	change_animation(1)
#	stop()

func _on_state_exited_attack_sword():
	print("you just swung your sword, you should get a modifier.")
	#attack_sword.timeout.start()
	attack_sword.activate()
	print ("you has merged as? :" ,modifier.animation_name)
	
