extends Node

const NPC_SCENE: PackedScene = preload("res://scenes/entities/npc/npc.tscn")

func _npc_root() -> Node3D:
  var n: Node = get_node_or_null("/root/World/EntityRoot/Npcs")
  return n

func spawn_default_npcs() -> void:
  var root := _npc_root()
  if root == null:
    return
  var profiles: Array = [
    _make_profile(&"barkeep", "Brena", "Barkeep at the Crooked Wheel tavern.", "Warm, gossipy, has lived here her whole life."),
    _make_profile(&"smith", "Halvor", "Village blacksmith. Lost an eye to a portal monster.", "Gruff, terse, cares deeply for the village."),
    _make_profile(&"sage", "Eyrie", "Village sage. Reads the portals, claims to remember the time before.", "Cryptic, slow-spoken, weary."),
    _make_profile(&"farmer", "Rolf", "Tends the small fields east of the village.", "Anxious, practical, wants the wars to end."),
  ]
  var radius: float = 6.0
  for i in range(profiles.size()):
    var angle: float = float(i) * TAU / float(profiles.size()) + 0.3
    var pos := Vector3(cos(angle) * radius, 0.0, sin(angle) * radius)
    var npc: StaticBody3D = NPC_SCENE.instantiate()
    npc.profile = profiles[i]
    npc.profile_id = profiles[i].npc_id
    npc.name = "Npc_%s" % str(profiles[i].npc_id)
    root.add_child(npc, true)
    npc.global_position = pos

func _make_profile(id: StringName, name: String, lore: String, pers: String) -> NpcProfile:
  var p := NpcProfile.new()
  p.npc_id = id
  p.display_name = name
  p.lore = lore
  p.personality = pers
  p.fallback_text = "..."
  p.greeting = "Hallo, Reisender."
  p.color = Color(0.55 + randf() * 0.35, 0.5 + randf() * 0.3, 0.4 + randf() * 0.3)
  return p
