extends Node

func _process(_delta: float) -> void:
  if not PlayerData.alive or not PlayerData.in_combat:
    return
  if not is_instance_valid(PlayerData.target):
    return
  if PlayerData.aa_timer > 0:
    return
  var ability_set: AbilitySet = PlayerData.ability_set
  if not ability_set:
    return
  var player: Node = get_tree().get_first_node_in_group("player")
  if not player:
    return
  var aa_damage: float = ability_set.aa_damage
  var aa_range: float = ability_set.aa_range
  var aa_is_heal: bool = ability_set.aa_is_heal
  var dmg: float = aa_damage * (PlayerData.buff_heal if aa_is_heal else PlayerData.buff_damage) * PlayerData.level_scale
  if aa_is_heal:
    EventBus.heal_requested.emit(player, player, dmg)
  else:
    var dist: float = player.global_position.distance_to(PlayerData.target.global_position)
    if dist > aa_range:
      return
    EventBus.damage_requested.emit(player, PlayerData.target, dmg)
  PlayerData.aa_timer = PlayerData.aa_cooldown
