summaryrefslogtreecommitdiffstats
path: root/world/chunk
diff options
context:
space:
mode:
Diffstat (limited to 'world/chunk')
-rw-r--r--world/chunk/Chunk.gd16
-rw-r--r--world/chunk/Chunk.tscn36
-rw-r--r--world/chunk/ChunkGen.gd49
-rw-r--r--world/chunk/ChunkLoader.gd66
-rw-r--r--world/chunk/helper/ChunkedCSGMesh.gd6
-rw-r--r--world/chunk/helper/ChunkedMeshInstance.gd6
-rw-r--r--world/chunk/helper/ChunkedSprite3D.gd6
7 files changed, 185 insertions, 0 deletions
diff --git a/world/chunk/Chunk.gd b/world/chunk/Chunk.gd
new file mode 100644
index 0000000..7c5c1c3
--- /dev/null
+++ b/world/chunk/Chunk.gd
@@ -0,0 +1,16 @@
+extends Area
+
+enum LOD {DISTANCE,MID,CLOSE}
+var lod = -1
+
+func lod_update():
+ for obj in $"gen_tree".get_children():
+ match lod:
+ LOD.CLOSE:
+ obj.lod_close()
+ LOD.MID:
+ obj.lod_mid()
+ LOD.DISTANCE:
+ obj.lod_distance()
+ _:
+ obj.lod_distance()
diff --git a/world/chunk/Chunk.tscn b/world/chunk/Chunk.tscn
new file mode 100644
index 0000000..4ce985b
--- /dev/null
+++ b/world/chunk/Chunk.tscn
@@ -0,0 +1,36 @@
+[gd_scene load_steps=7 format=2]
+
+[ext_resource path="res://world/chunk/Chunk.gd" type="Script" id=1]
+[ext_resource path="res://world/chunk/helper/ChunkedCSGMesh.gd" type="Script" id=2]
+
+[sub_resource type="SphereShape" id=1]
+radius = 50.0
+
+[sub_resource type="SpatialMaterial" id=3]
+flags_unshaded = true
+params_cull_mode = 2
+albedo_color = Color( 0.937255, 0, 1, 1 )
+
+[sub_resource type="PlaneMesh" id=2]
+size = Vector2( 100, 100 )
+
+[sub_resource type="PlaneMesh" id=4]
+size = Vector2( 98, 98 )
+
+[node name="Chunk" type="Area"]
+collision_layer = 32
+collision_mask = 0
+script = ExtResource( 1 )
+
+[node name="Size" type="CollisionShape" parent="."]
+shape = SubResource( 1 )
+
+[node name="Border" type="CSGMesh" parent="."]
+material_override = SubResource( 3 )
+script = ExtResource( 2 )
+_mesh = SubResource( 2 )
+
+[node name="CSGMesh" type="CSGMesh" parent="Border"]
+operation = 2
+script = ExtResource( 2 )
+_mesh = SubResource( 4 )
diff --git a/world/chunk/ChunkGen.gd b/world/chunk/ChunkGen.gd
new file mode 100644
index 0000000..5e99be8
--- /dev/null
+++ b/world/chunk/ChunkGen.gd
@@ -0,0 +1,49 @@
+extends Node
+
+var chunk_half_size setget _set_chunk_size
+var chunk_size
+
+func _set_chunk_size(val):
+ chunk_half_size = val
+ chunk_size = val * 2.0
+
+onready var noise = OpenSimplexNoise.new()
+
+func _ready():
+ randomize()
+ noise.seed = randi()
+ noise.octaves = 4
+ noise.period = 64
+ noise.persistence = 0.001
+ noise.lacunarity = 2.0
+
+func gen_chunk(chunk):
+ var gen_tree = Spatial.new()
+ gen_tree.name = "gen_tree"
+ chunk.add_child(gen_tree)
+ iterate_chunk(chunk, Vector2(10.0,10.0), funcref(self,"gen_rocks"))
+
+func v2_coords(coords:Vector3):
+ return Vector2(coords.x,coords.z)
+
+func v3_coords(coords:Vector2):
+ return Vector3(coords.x,0.0,coords.y)
+
+func iterate_chunk(chunk,step:Vector2,cb:FuncRef):
+ var chunk_size_rounded = Vector2(chunk_half_size,chunk_half_size).snapped(step)
+ for x in range(-chunk_size_rounded.x,chunk_size_rounded.x,step.x):
+ for y in range(-chunk_size_rounded.y,chunk_size_rounded.y,step.y):
+ cb.call_func(chunk,Vector2(x,y))
+
+onready var Rock = preload("res://world/obstacles/Rock.tscn")
+const rock_size = 5.0
+func make_rock(chunk,xform:Transform):
+ var rock = Rock.instance()
+ rock.transform = xform
+ chunk.get_node("gen_tree").add_child(rock)
+
+func gen_rocks(chunk,chunk_coords:Vector2):
+ var world_coords = chunk_coords + v2_coords(chunk.transform.origin)
+ var noise_val = self.noise.get_noise_2dv(world_coords)
+ if noise_val > 0.4:
+ make_rock(chunk,Transform(Basis().scaled(Vector3(2.0,2.0,2.0)),v3_coords(chunk_coords)))
diff --git a/world/chunk/ChunkLoader.gd b/world/chunk/ChunkLoader.gd
new file mode 100644
index 0000000..9f1d9b3
--- /dev/null
+++ b/world/chunk/ChunkLoader.gd
@@ -0,0 +1,66 @@
+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
+
+func _ready():
+ chunk_thread.start(self, "chunk_loader")
+
+func _exit_tree():
+ mtx.lock()
+ exit = true
+ sgnl.post()
+ mtx.unlock()
+ chunk_thread.wait_to_finish()
+
+func add_chunk(coords:Vector2):
+ chunks[coords] = coords.length()
+
+func chunk_update():
+ 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]:
+ closest_unloaded_chunk = chunk
+ closest_len = chunks[chunk]
+ if closest_unloaded_chunk != null:
+ mtx.lock()
+ if chunk_to_load == null:
+ chunk_to_load = closest_unloaded_chunk
+ sgnl.post()
+ mtx.unlock()
+ for chunk in loaded_chunks.keys():
+ if !chunks.has(chunk):
+ var c = loaded_chunks[chunk].get_ref()
+ if c != null:
+ c.queue_free()
+ loaded_chunks.erase(chunk)
+ chunks = {}
+
+func chunk_loader():
+ while true:
+ sgnl.wait()
+ mtx.lock()
+ var x = exit
+ var coords = chunk_to_load
+ chunk_to_load = null
+ mtx.unlock()
+ if x:
+ break
+ var chunk = Chunk.instance()
+ chunk.transform.origin = Vector3(coords.x,0.0,coords.y)
+ 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)
diff --git a/world/chunk/helper/ChunkedCSGMesh.gd b/world/chunk/helper/ChunkedCSGMesh.gd
new file mode 100644
index 0000000..2b8c6db
--- /dev/null
+++ b/world/chunk/helper/ChunkedCSGMesh.gd
@@ -0,0 +1,6 @@
+extends CSGMesh
+
+export(Mesh) var _mesh
+
+func _ready():
+ self.mesh = _mesh
diff --git a/world/chunk/helper/ChunkedMeshInstance.gd b/world/chunk/helper/ChunkedMeshInstance.gd
new file mode 100644
index 0000000..dde7eda
--- /dev/null
+++ b/world/chunk/helper/ChunkedMeshInstance.gd
@@ -0,0 +1,6 @@
+extends MeshInstance
+
+export(Mesh) var _mesh
+
+func _ready():
+ self.mesh = _mesh
diff --git a/world/chunk/helper/ChunkedSprite3D.gd b/world/chunk/helper/ChunkedSprite3D.gd
new file mode 100644
index 0000000..c9e0be9
--- /dev/null
+++ b/world/chunk/helper/ChunkedSprite3D.gd
@@ -0,0 +1,6 @@
+extends Sprite3D
+
+export(Texture) var _tex
+
+func _ready():
+ self.texture = _tex