extends Node
class_name PlaceMap

var temp_map_height :int = 1
var temp_map_x :int = 32
var temp_map_z :int = 32
var tile_map :Dictionary = {}
var wall_map :Dictionary = {}
var room_map :Dictionary = {}
var place_nav :AStar3D = AStar3D.new()

enum TILE_STATE {CLOSED, OPEN}
enum WALL_STATE {CLOSED, OPEN, DOOR}

const direction_vectors :Array = [
	Vector3i(0, 0, -12),
	Vector3i(12, 0, 0),
	Vector3i(0, 0, 12),
	Vector3i(-12, 0, 0)
]

func _process(_delta: float) -> void:
	var color_tile_built :Color = Color(0.939, 0.741, 0.73, 1.0)
	var color_wall_built :Color = Color(0.81, 0.299, 0.316, 1.0)
	var built_tiles :Array = []
	var built_walls :Array = []
	var tiles :Array = []
	var walls :Array = []
	for i :Vector3i in tile_map.keys():
		var pos :Vector3 = Vector3(i) / 12
		if tile_map[i]["state"] == TILE_STATE.CLOSED:
			tiles.append(pos)
		elif tile_map[i]["state"] == TILE_STATE.OPEN:
			built_tiles.append(pos)
	for i :Vector3i in wall_map.keys():
		var pos :Vector3 = Vector3(i) / 12
		if wall_map[i]["state"] == WALL_STATE.OPEN:
			walls.append(pos)
		elif wall_map[i]["state"] == WALL_STATE.CLOSED:
			built_walls.append(pos)
	DebugDraw3D.draw_points(built_tiles, 0, 0.2, color_tile_built)
	DebugDraw3D.draw_points(built_walls, 0, 0.2, color_wall_built)
	DebugDraw3D.draw_points(tiles, 0, 0.2, Color(0.459, 0.404, 0.866, 1.0))
	DebugDraw3D.draw_points(walls, 0, 0.2, Color(0.343, 0.409, 0.528, 1.0))

func temp_create_map() -> void:
	#TEMP: just makes a square. Should be replaced with premade maps eventually.
	for y :int in range(temp_map_height):
		for x :int in range(temp_map_x):
			for z :int in range(temp_map_z):
				var map_pos :Vector3i = Vector3i(x * 12, y, z * 12)
				tile_map[map_pos] = {
					"object": null,
					"state": TILE_STATE.CLOSED
				}
	
	for i :Vector3i in tile_map.keys():
		for v :Vector3i in direction_vectors:
			var map_pos :Vector3i = i - (v / 2)
			wall_map[map_pos] = {
				"object": null,
				"state": WALL_STATE.OPEN,
			}
			
func give_walls(tile :Tile) -> void:
	var temp_dict :Dictionary
	for i :Vector3i in direction_vectors:
		if tile_map.has(tile.map_position + i):
			var neighbor :Tile = tile_map[tile.map_position + i]["object"]
			var room :Room = tile.lookup_tile_to_room
			if not room_map[room].has(neighbor):
				if wall_map[tile.map_position + (i / 2)]["state"] == PlaceMap.WALL_STATE.CLOSED :
					temp_dict[Vector3(i / 2) / 12] = PlaceMap.WALL_STATE.CLOSED
	if temp_dict:
		tile.face_dict = temp_dict.duplicate()
