extends Spatial


# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var latMin:float = 0
var latMax:float = 1
var lonMin:float = 0.0
var lonMax:float = 1.0
var resolution:int = 512
var radius:float = 10
var scaleFact:int = 1

var origin:Vector3
var center:Vector3

var initialized = false

func setParams(latMinIN, latMaxIN, lonMinIN, lonMaxIN, resolutionIN, radiusIN:float, scaleFactIN):
	latMin = latMinIN
	latMax = latMaxIN
	lonMin = lonMinIN
	lonMax = lonMaxIN
	resolution = resolutionIN
	radius = radiusIN
	scaleFact = scaleFactIN

# Called when the node enters the scene tree for the first time.
func _ready():
	initialize()
	#.translate(.get_node("Mesh").origin)

func initialize():
	print("chunk %s,%s initializing" %[latMin, lonMin])
	var initialized = .get_node("Mesh").initialize(latMin, latMax, lonMin, lonMax, resolution, radius, scaleFact)
	if not initialized:
		var download = Downloader.Download.new()
		while not download._file_name == .get_node("Mesh").tl.getFilepath():
			print("awaiting file download for %s" %.get_node("Mesh").tl.getFilepath())
			download = yield(Messenger, "fileDownloaded")
	var texInitialized = .get_node("Mesh").initTexture()
	if not texInitialized:
		var dl = Downloader.Download.new()
		while not dl._file_name == .get_node("Mesh").texl.getTexName():
			print("awaiting file download for %s" %.get_node("Mesh").texl.getTexName())
			dl = yield(Messenger, "fileDownloaded")
	.get_node("Mesh").genMesh()
	.get_node("Origin").transform.origin = .get_node("Mesh").origin
	.get_node("Center").transform.origin = .get_node("Mesh").center
	print("chunk %s,%s ready" %[latMin, lonMin])

# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass
