var position = Vector3(0, 0, 0)
var chunk_offset = Vector3(0, 0, 0)
var elements = {}
var element_holder_class = preload("res://scenes/level/element_holder.gd")

var level
var chunk_size

var initialized = false

func _init(x, y, z):
	self.position.x = x
	self.position.y = y
	self.position.z = z
	self.chunk_size = LevelManager.LEVEL_CHUNK_SIZE

func initialize():
	if initialized:
		return
	if level.loader.is_saving:
		return
	_fill_chunk_with_elements()
	#print ("initialize")
	initialized = true

func _fill_chunk_with_elements():
	for x in range(self.chunk_size):
		for y in range(self.chunk_size):
			for z in range(self.chunk_size):
				self.elements[str(x) + "_" + str(y) + "_" + str(z)] = self.element_holder_class.new(x + self.chunk_offset.x, y + self.chunk_offset.y, z + self.chunk_offset.z)

func wipe():
	for elem in self.elements:
		self.elements[elem].wipe()

	

func get_element(elem_position, skip_init = false): 
	if skip_init == false:
		initialize()
	var key = str(elem_position.x) + "_" + str(elem_position.y) + "_" + str(elem_position.z)
	if self.elements.has(key):
		return self.elements[key]
	return null
