You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Gridmap/scripts/Ground.gd

93 lines
2.4 KiB

extends Node3D
const WATER_HEIGHT = 0.5
var thread
var displayed_chunks = {}
var unready_chunks = {}
@export var camera: Camera3D
@export var OceanTiles: MultiMeshInstance3D
var chunks: Array
func _ready():
thread = Thread.new()
init_chunks()
func init_chunks():
for x in Global.world.width / Global.world.chunk_size:
chunks.append([])
for y in Global.world.height / Global.world.chunk_size:
chunks[x].append([])
chunks[x][y] = Chunk.new(x, y, Global.world.chunk_size, chunks, WATER_HEIGHT)
chunks[x][y].set_name("Chunk")
chunks[x][y]
func add_chunk(x, z):
var key = str(x) + "," + str(z)
if displayed_chunks.has(key) or unready_chunks.has(key):
return
if not thread.is_started():
thread.start(Callable(self, "load_chunk").bind([thread, x, z]))
unready_chunks[key] = 1
func load_chunk(array):
var thread = array[0]
var posX = array[1]
var posZ = array[2]
var x = fmod(posX, Global.world.width / Global.world.chunk_size)
var z = fmod(posZ, Global.world.height / Global.world.chunk_size)
#
# # Chargement du chunk
var chunk = chunks[x][z]
var chunk_position = Vector2(posX, posZ)
chunk.position = Vector3(posX * Global.world.chunk_size, 0, posZ * Global.world.chunk_size)
# OceanTiles.multimesh.set_instance_transform()
call_deferred("load_done", chunk, thread, chunk_position)
func load_done(chunk, thread, position):
add_child(chunk)
var key = str(position.x) + "," + str(position.y)
displayed_chunks[key] = chunk
unready_chunks.erase(key)
thread.wait_to_finish()
func get_chunk(x, z):
var key = str(x) + "," + str(z)
if displayed_chunks.has(key):
return displayed_chunks.get(key)
return null
func _process(delta):
update_chunks()
clean_up_chunks()
reset_chunks()
func update_chunks():
var camera_translation = camera.position
var c_x = int(camera_translation.x) / Global.world.chunk_size
var c_z = int(camera_translation.z) / Global.world.chunk_size
for x in range(c_x - Global.world.chunk_number * 0.5, c_x + Global.world.chunk_number * 0.53):
for z in range(c_z - Global.world.chunk_number * 0.6, c_z + Global.world.chunk_number * 0.33):
add_chunk(x, z)
var chunk = get_chunk(x, z)
if chunk != null:
chunk.should_remove = false
func clean_up_chunks():
for key in displayed_chunks:
var chunk = displayed_chunks[key]
if chunk.should_remove:
remove_child(chunk)
displayed_chunks.erase(key)
func reset_chunks():
for key in displayed_chunks:
var chunk = displayed_chunks[key]
chunk.should_remove = true