extends State

var target: Node2D

func enter(previous_state_path: String, data := {}) -> void:
	if data.has("target"):
		target = data["target"]
		owner.sprite.play("Hunting")
		owner.wrapper.play_sprite("Hunting")
	else:
		# default behaviour; do nothing
		finished.emit(owner.fsm.States.IDLE, {})

func physics_update(_delta: float) -> void:
	if target == null or GameManager.calc_distance(owner.collision.position, target.position) > owner.sight_range:
		finished.emit(owner.fsm.States.IDLE, {}) 
		return
	
	# alternatively, we could use a collision shape and inbuilt signals, but im not sure if that works better (mixing signals and custom fsm stuff i mean
	if GameManager.calc_distance(owner.collision.position, target.position) > owner.attack_range:
		owner.move(move_towards(target.position))
	else:
		owner.attack(target)

func move_towards(pos: Vector2) -> Vector3:
	var diff = target.position - owner.collision.position
	return Vector3(diff.x, diff.y ,0)	
