summaryrefslogtreecommitdiffstats
path: root/World.gd
diff options
context:
space:
mode:
authordusoleil <howcansocksbereal@gmail.com>2022-09-18 07:45:13 -0400
committerdusoleil <howcansocksbereal@gmail.com>2022-09-18 07:45:13 -0400
commit3ac2fb62b67b6446ab31b2455a4cb74069f1c54f (patch)
tree4764fa39466d7fd0d4ac9133e5a237dd1576a487 /World.gd
parent5923cd97dfafd7fee2784a91e81d7d55ed28448e (diff)
downloadgodot_wildjam_49-3ac2fb62b67b6446ab31b2455a4cb74069f1c54f.tar.gz
godot_wildjam_49-3ac2fb62b67b6446ab31b2455a4cb74069f1c54f.zip
Add ChunkLoader to World Generation
Break up world gen into chunk and offload generating them to a Chunk Loader. The World figures out which chunks should be loaded and gives this to the Chunk Loader. The Chunk Loader frees any chunks out of render distance. It also picks an unloaded chunk and gives it to a worker thread to generate it.
Diffstat (limited to 'World.gd')
-rw-r--r--World.gd60
1 files changed, 26 insertions, 34 deletions
diff --git a/World.gd b/World.gd
index da811a8..97f0a09 100644
--- a/World.gd
+++ b/World.gd
@@ -4,45 +4,17 @@ export(NodePath) var traveler = null
onready var _traveler = get_node(traveler)
export var acceleration = 10.0
-onready var Chunk = preload("res://Chunk.tscn")
-onready var chunk_size = $"Chunks/Chunk/Size".shape.radius*2.0
-var chunks = {}
+onready var chunk_render_distance = $"ChunkRenderDistance/Radius".shape.radius
-func v2_coords(coords:Vector3):
- return Vector2(coords.x,coords.z)
-
-func v3_coords(coords:Vector2):
- return Vector3(coords.x,0.0,coords.y)
+onready var chunk_half_size = $"Chunks/Chunk/Size".shape.radius
+onready var chunk_size = chunk_half_size * 2.0
-func chunk_coords(coords:Vector2):
- return coords.snapped(Vector2(chunk_size,chunk_size))
-
-func gen_chunk(coords:Vector2):
- var chunk = Chunk.instance()
- chunk.transform.origin = v3_coords(coords)
- $"Chunks".add_child(chunk)
- chunks[coords] = chunk
-
-func gen_chunks():
- var gen_center = -v2_coords($"Chunks".transform.origin)
- var gen_radius = $"ChunkRenderDistance/Radius".shape.radius
- var gen_radius_rounded = stepify(gen_radius, chunk_size)
- for x in range(-gen_radius_rounded, gen_radius_rounded+chunk_size, chunk_size):
- for y in range(-gen_radius_rounded, gen_radius_rounded+chunk_size, chunk_size):
- var gen_coords = Vector2(x,y)
- if gen_coords.length() > gen_radius:
- continue
- var coords = chunk_coords(gen_coords + gen_center)
- if !chunks.has(coords):
- gen_chunk(coords)
+func _ready():
+ ChunkLoader.world = self
+ ChunkGen.chunk_half_size = self.chunk_half_size
func _on_ChunkRenderDistance_area_exited(area:Area):
- gen_chunks()
area.queue_free()
- chunks.erase(v2_coords(area.transform.origin))
-
-func _ready():
- gen_chunks()
func travel(direction:Vector3):
var heading = _traveler.global_transform.basis
@@ -53,6 +25,26 @@ func travel(direction:Vector3):
$"Chunks".add_central_force(direction*acceleration)
func _physics_process(_state):
+ chunk_process()
var stick = Input.get_axis("ship_down","ship_up")
if stick != 0.0:
self.travel(Vector3.FORWARD*stick)
+
+func v2_coords(coords:Vector3):
+ return Vector2(coords.x,coords.z)
+
+func chunk_coords(coords:Vector2):
+ return coords.snapped(Vector2(chunk_size,chunk_size))
+
+func chunk_process():
+ var gen_center = -v2_coords($"Chunks".transform.origin)
+ var gen_radius = chunk_render_distance
+ var gen_radius_rounded = stepify(gen_radius, chunk_size)
+ for x in range(-gen_radius_rounded, gen_radius_rounded+chunk_size, chunk_size):
+ for y in range(-gen_radius_rounded, gen_radius_rounded+chunk_size, chunk_size):
+ var gen_coords = Vector2(x,y)
+ if gen_coords.length() > gen_radius:
+ continue
+ var coords = chunk_coords(gen_coords + gen_center)
+ ChunkLoader.add_chunk(coords)
+ ChunkLoader.chunk_update()