extends Node
class_name Room

var room_to_tile :Array = []

var nav :AStar3D = AStar3D.new()

var lookup_room_to_door :Array = []

var place_id: int = 0

signal nav_request

func _process(delta: float) -> void:
	for i in nav.get_point_ids():
		for j in nav.get_point_connections(i):
			var i_pos :Vector3 = nav.get_point_position(i)
			var j_pos :Vector3 = nav.get_point_position(j)
			if i > 0:
				DebugDraw3D.draw_line(i_pos, j_pos, Color(0.613, 0.613, 0.613, 1.0))

func save():
	var save_data = {
#Basics
		"id": self.get_instance_id(),
		"type": "Room",
		"scene_file_path": scene_file_path,
		"parent": get_parent().get_instance_id(),
#Connections
		"place_id": place_id,
#Data
	}
	return save_data

func init_room(new_dict):
#When a room is created, run through these steps

	#setup_path_grid(new_dict)
	update_nav()
	for i in new_dict.values():
		i.lookup_tile_to_room = self

func update_nav() -> void:
	emit_signal("nav_request", self)

func setup_path_grid(new_array):
#Sets up the A* grid of tiles that are in this room.
#TODO: Better grid changing
	pass
	#nav.clear()
	#room_to_tile = new_array
	#var room_to_tile_positions: Array
	#
	#for i in room_to_tile:
		#var id = nav.get_available_point_id()
		#var point_pos = Vector3(i.place_position)
		#nav.add_point(id, point_pos)
		#room_to_tile_positions.append(point_pos)
#
		#for n in Tile.direction_vector_dict.values():
			#var neighbor_pos = point_pos + Vector3(n)
			#if room_to_tile_positions.has(neighbor_pos):
				#var closest = nav.get_closest_point(neighbor_pos)
				#if (neighbor_pos) == Vector3(nav.get_point_position(closest)):
					#nav.connect_points(id, closest)

func get_room_path(start :Vector3i, end :Vector3i) -> Array:
	
	var start_point = nav.get_closest_point(Vector3(start))
	var end_point = nav.get_closest_point(Vector3(end))
	var path = nav.get_point_path(start_point, end_point)
	return path
	

		
		
		
		
	
	
	#for i in room_to_tile:
		#var place_end :Vector3 = Vector3(end)
		#var pos = Vector3(i.place_position)
		#if pos == place_end:

		#if fmod(place_end.x, 1) != 0:
			#var tst1 = place_end + Vector3(-0.5, 0, 0)
			#var tst2 = place_end + Vector3(0.5, 0, 0)
			#if pos == tst1:
				#var start_point = nav.get_closest_point(Vector3(start))
				#var end_point = nav.get_closest_point(pos)
				#var path = nav.get_point_path(start_point, end_point)
				#return path
			#if pos == tst2:
				#var start_point = nav.get_closest_point(Vector3(start))
				#var end_point = nav.get_closest_point(pos)
				#var path = nav.get_point_path(start_point, end_point)
				#return path
		#elif fmod(place_end.z, 1) != 0:
			#var tst1 = place_end + Vector3(0, 0, -0.5)
			#var tst2 = place_end + Vector3(0, 0, 0.5)
			#if pos == tst1:
				#var start_point = nav.get_closest_point(Vector3(start))
				#var end_point = nav.get_closest_point(pos)
				#var path = nav.get_point_path(start_point, end_point)
				#return path
			#if pos == tst2:
				#var start_point = nav.get_closest_point(Vector3(start))
				#var end_point = nav.get_closest_point(pos)
				#var path = nav.get_point_path(start_point, end_point)
				#return path
	return []
