extends Spatial

static func build_house(main_xform: Transform):
	var rnd = streaming.get_place_rnd(main_xform)
	print(main_xform.origin, " seed = ", rnd.state)
	var l = 6 + 2 * rnd.randi() % 7 - 1
	var h = l - 1
	var d = 3 + rnd.randi() % (l - 5 + 1)
	var range_used = false
	var objects = []
	for k in range(l + 1):
		var pos = Vector3(0, 0, k * 2)
		var xform = Transform(Basis(), pos)
		if k > 0:
			var what
			if k != d && rnd.randf() > 0.5 && !range_used:
				what = "roof_floor_range"
				range_used = true
			else:
				what = "roof_floor"
			objects.push_back({"xform": xform, "data": [what]})
			var xt = [Transform(Basis(), pos), Transform(Basis().rotated(Vector3.UP, PI), pos - Vector3(0, 0, 2))]
			for x in range(xt.size()):
				if x == 0 && k == d:
					continue
				if rnd.randf() > 0.5:
					what = "wall_solid"
				elif rnd.randf() > 0.5:
					what = "window_narrow"
				else:
					what = "window_wide"
				objects.push_back({"xform": xt[x], "data": [what]})
		var obj_data = []
		if k > 1 && k < l && rnd.randf() > 0.6:
			objects.push_back({"xform": xform, "data": ["wall_internal"]})

		match k:
			0:
				obj_data = ["side_wall", "bottom_side"]
			1:
				obj_data = ["bottom"]
			2:
				obj_data = ["bottom_wheels"]
			d:
				obj_data = ["entry", "bottom"]
			h:
				obj_data = ["bottom_wheels"]
			l:
				obj_data = ["side_wall_rotated", "bottom_side_rotated", "bottom"]
			_:
				obj_data = ["bottom"]
		objects.push_back({"xform": xform, "data": obj_data})
	return objects

# Called every frame. 'delta' is the elapsed time since the previous frame.
var state = 0
func _process(delta):
	match state:
		0:
			state = 1
		1:
			var objects = build_house(global_transform)
			streaming.spawn_house_objects(global_transform, objects)
			state = 2
		2:
			set_process(false)

#var prev = 3000000
#func _physics_process(delta):
#	var cam = get_viewport().get_camera()
#	var dst = cam.global_transform.origin.distance_squared_to(global_transform.origin)
#	match state:
#		2:
#			if abs(prev - dst) > 400:
#				streaming.update_node_position(n)
