extends Control


# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	for k in $VBoxContainer/list.get_children():
		k.queue_free()
	var dst = INF
	var player_pos = global.player.global_transform.origin
	for k in get_tree().get_nodes_in_group("npc"):
		var hb = HBoxContainer.new()
		var label1 = Label.new()
		label1.text = k.name
		var label2 = Label.new()
		if k.has_meta("action"):
			label2.text = k.get_meta("action")
		else:
			label2.text = "unknown action"
		hb.add_child(label1)
		hb.add_child(label2)
		$VBoxContainer/list.add_child(hb)
		var npc_pos = k.global_transform.origin
		var ndst = player_pos.distance_squared_to(npc_pos)
		if ndst < dst:
			var bb = k.get_node("blackboard")
			$VBoxContainer/closest/name.text = k.name
			dst = ndst
			if k.has_meta("action"):
				$VBoxContainer/closest/action.text = "action: " + k.get_meta("action")
			var enemy = bb.get("closest_enemy")
			if enemy:
				$VBoxContainer/closest/closest_enemy.text = "enemy: " + enemy.name
			else:
				$VBoxContainer/closest/closest_enemy.text = "no enemy"
			if k.has_meta("target_group"):
				$VBoxContainer/closest/target_group.text = "target: " + k.get_meta("target_group")
			else:
				$VBoxContainer/closest/target_group.text = ""
			if k.has_meta("target_loc"):
				$VBoxContainer/closest/target_loc.text = "target loc: " + str(k.get_meta("target_loc"))
			else:
				$VBoxContainer/closest/target_loc.text = ""
			if k.has_meta("path"):
				$VBoxContainer/closest/path.text = "path: " + str(k.get_meta("path"))
			else:
				$VBoxContainer/closest/path.text = ""
			if k.has_meta("path_point"):
				$VBoxContainer/closest/path_point.text = "path point: " + str(k.get_meta("path_point"))
			else:
				$VBoxContainer/closest/path_point.text = ""
