extends MovementComponent

onready var idle_timeout = $IdleTimeout
onready var edge_detector:RayCast2D = $"../EdgeDetection"
onready var player_detector:RayCast2D = $"../PlayerDetection"

var flip_flop = false
var wants_to_attack = false

func process_physics(delta):
	if current_movement_state == "move" and edge_detector.is_colliding() == false:
		#print(owner.name, "uh oh gonna fall!")
		stop()
		return
	if player_detector.is_colliding():
		#print(owner.name, " wants to attack")
		stop()
		wants_to_attack = true
		if attack_function.is_valid():
			attack_function.call_func()
		return
	else:
		wants_to_attack = false

func process(delta):
	if current_movement_state == "idle" and idle_timeout.time_left == 0:
		if debug_component:
			print ("Time to move!")
		if flip_flop:
			flip_flop = false
			go_right()
			idle_timeout.start()
		else:
			flip_flop = true
			go_left()
			idle_timeout.start()
	if current_movement_state == "move" and idle_timeout.time_left == 0:
		if debug_component:
			print("Time to stop.")
		idle_timeout.start()
		stop()
	return

func wants_shoot() -> bool:	
	return wants_to_attack
