extends Spatial

const tile_size = 8
const palace_size = 12
const layers = 4

func _ready():
	var voffset = 0.0
	for layer in range(layers):
		match layer:
			0:
				voffset += 0.0
			1:
				voffset += 1
			2:
				voffset += 4
			_:
				voffset += 4
		for i in range(palace_size):
			for j in range(palace_size):
				var x = i * tile_size - palace_size * tile_size / 2
				var z = j * tile_size - palace_size * tile_size / 2
				if layer == 0:
					var ct = "courtyard_tile"
					var xform = Transform(Basis(), Vector3(x, voffset, z))
					if i > 0 && i < palace_size - 1:
						if j > 0 && j < palace_size - 1:
								call("place", ct, xform)
					if i in [0, palace_size - 1] || j in [0, palace_size - 1]:
						if (j == palace_size / 2.0 || i != palace_size / 2.0):
							call("place", "foundation_tile", xform)
				else:
					var ct = "room_tile"
					var tower_angles = {
						0:
							{
								0: -PI / 2.0,
								palace_size - 1: 0
							},
						palace_size - 1:
							{
								0: PI,
								palace_size - 1: PI / 2.0
							}
					}
					var xform = Transform(Basis(), Vector3(x, voffset, z))
					if i  == 0 && !j in [0, palace_size - 1]:
						xform.basis = Basis().rotated(Vector3.UP, -PI / 2.0)
						spawn_wall(layer, i, j, xform)
					elif i == palace_size - 1 && !j in [0, palace_size - 1]:
						xform.basis = Basis().rotated(Vector3.UP, PI / 2.0)
						spawn_wall(layer, i, j, xform)
					elif !i in [0, palace_size - 1] && j == 0:
						xform.basis = Basis().rotated(Vector3.UP, PI)
						spawn_wall(layer, i, j, xform)
					elif !i in [0, palace_size - 1] && j == palace_size - 1:
						xform.basis = Basis()
						spawn_wall(layer, i, j, xform)
					elif tower_angles.has(i) && tower_angles[i].has(j):
						xform.basis = Basis().rotated(Vector3.UP, tower_angles[i][j])
						if layer == layers - 1:
							var twr = "tower_roof_tile"
							call("place", twr, xform)
						else:
							var tw = "tower_walls_tile"
							call("place", tw, xform)
							var st = "stairs_tile"
							call("place", st, xform)
							if layer == 1:
								var tfl = "tower_floor_tile"
								call("place", tfl, xform)
	call("place", "courtroom", Transform(Basis(), Vector3(-2, 0, 0)))
	var car = Spatial.new()
	car.add_to_group("spawn")
	car.add_to_group("keep")
	car.add_to_group("car")
	add_child(car)
	car.global_transform.origin = Vector3(-5, 0, -5)
#	for e in range(5):
#		var major_f = Spatial.new()
#		major_f.add_to_group("spawn")
#		if e == 0:
#			major_f.add_to_group("male")
#		else:
#			major_f.add_to_group("female")
#		major_f.add_to_group("keep")
#		add_child(major_f)
#		major_f.global_transform = Transform(Basis(), Vector3(cos(PI / 3 * e) * 2.0, 0, sin(PI / 3 * e) * 2.0) + Vector3(10.0, 0, 0))
	print("PALACE done")

func place(obj: String, where: Transform):
	streaming.spawn_child(obj, global_transform * where)

func spawn_wall(layer:int, i: int, j: int, xform: Transform):
	if layer == layers - 1:
		var rt = "roof_tile"
		call("place", rt, xform)
	elif (j != palace_size / 2.0 && i != palace_size / 2.0)|| layer > 2:
		var ct = "room_tile"
		call("place", ct, xform)
	elif (j == palace_size / 2.0 && i != palace_size / 2.0) && layer == 1:
		var ent = "entry_tile"
		call("place", ent, xform)
	elif (j != palace_size / 2.0 && i == palace_size / 2.0) && layer == 1:
		var gw = "gate_bottom_tile"
		call("place", gw, xform)
	elif (j != palace_size / 2.0 && i == palace_size / 2.0) && layer == 2:
		var gw = "gate_top_tile"
		call("place", gw, xform)
	elif (j == palace_size / 2.0 && i != palace_size / 2.0) && layer == 2:
		var ct = "room_tile"
		call("place", ct, xform)
