summaryrefslogtreecommitdiffstats
path: root/world/chunk/ChunkLoader.gd
diff options
context:
space:
mode:
Diffstat (limited to 'world/chunk/ChunkLoader.gd')
-rw-r--r--world/chunk/ChunkLoader.gd32
1 files changed, 25 insertions, 7 deletions
diff --git a/world/chunk/ChunkLoader.gd b/world/chunk/ChunkLoader.gd
index 2e01bca..42e40bb 100644
--- a/world/chunk/ChunkLoader.gd
+++ b/world/chunk/ChunkLoader.gd
@@ -3,13 +3,13 @@ extends Node
var world = null
var Chunk = preload("res://world/chunk/Chunk.tscn")
-var chunks = {}
var loaded_chunks = {}
var chunk_thread = Thread.new()
var mtx = Mutex.new()
var sgnl = Semaphore.new()
var exit = false
var chunk_to_load = null
+var chunk_to_len
func _ready():
chunk_thread.start(self, "chunk_loader")
@@ -21,8 +21,11 @@ func _exit_tree():
mtx.unlock()
chunk_thread.wait_to_finish()
-func add_chunk(coords:Vector2):
- chunks[coords] = coords.length()
+func v2_coords(coords:Vector3):
+ return Vector2(coords.x,coords.z)
+
+func chunk_coords(coords:Vector2):
+ return coords.snapped(Vector2(world.chunk_size,world.chunk_size))
func free_chunk(coords:Vector2):
if !loaded_chunks.has(coords):
@@ -33,23 +36,35 @@ func free_chunk(coords:Vector2):
loaded_chunks.erase(coords)
func chunk_update():
+ var chunks = {}
+ var gen_center = -v2_coords(world.get_node("Chunks").transform.origin)
+ var gen_radius = world.lod_distance
+ var gen_radius_rounded = stepify(gen_radius, world.chunk_size)
+ for x in range(-gen_radius_rounded, gen_radius_rounded+world.chunk_size, world.chunk_size):
+ for y in range(-gen_radius_rounded, gen_radius_rounded+world.chunk_size, world.chunk_size):
+ var gen_coords = Vector2(x,y)
+ if gen_coords.length() > gen_radius:
+ continue
+ var coords = chunk_coords(gen_coords + gen_center)
+ chunks[coords] = coords.length()
var closest_unloaded_chunk = null
var closest_len = 0.0
for chunk in chunks.keys():
if !loaded_chunks.has(chunk):
- if closest_unloaded_chunk == null || closest_len > chunks[chunk]:
+ var chunk_len = chunks[chunk]
+ if closest_unloaded_chunk == null || closest_len > chunk_len:
closest_unloaded_chunk = chunk
- closest_len = chunks[chunk]
+ closest_len = chunk_len
if closest_unloaded_chunk != null:
mtx.lock()
if chunk_to_load == null:
chunk_to_load = closest_unloaded_chunk
+ chunk_to_len = closest_len
sgnl.post()
mtx.unlock()
for chunk in loaded_chunks.keys():
if !chunks.has(chunk):
free_chunk(chunk)
- chunks = {}
func chunk_loader():
while true:
@@ -57,15 +72,18 @@ func chunk_loader():
mtx.lock()
var x = exit
var coords = chunk_to_load
+ var owo = chunk_to_len
chunk_to_load = null
mtx.unlock()
if x:
break
var chunk = Chunk.instance()
chunk.transform.origin = Vector3(coords.x,0.0,coords.y)
+ owo -= world.chunk_size
+ chunk.lod = 2 if owo <= world.lod_close else 1 if owo <= world.lod_mid else 0
ChunkGen.gen_chunk(chunk)
self.call_deferred("finish_chunk", chunk)
func finish_chunk(chunk):
world.get_node("Chunks").add_child(chunk)
- loaded_chunks[Vector2(chunk.transform.origin.x,chunk.transform.origin.z)] = weakref(chunk)
+ loaded_chunks[v2_coords(chunk.transform.origin)] = weakref(chunk)