From b709118be222f2d3680e65304407e9ecec06812e Mon Sep 17 00:00:00 2001 From: Rob Kelly Date: Mon, 29 Sep 2025 19:04:41 -0600 Subject: [PATCH] Basics of chunk generation --- project.godot | 1 + src/world/generation/chunk/chunk.gd | 29 +++++++++++++++++++ src/world/generation/chunk/chunk.gd.uid | 1 + src/world/generation/chunk/chunk.tscn | 23 +++++++++++++++ src/world/generation/generated_world.gd | 22 ++++++++++---- .../worldgen_manager/worldgen_manager.gd | 4 +++ .../worldgen_manager/worldgen_manager.gd.uid | 1 + .../worldgen_manager/worldgen_manager.tscn | 10 +++++++ 8 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 src/world/generation/chunk/chunk.gd create mode 100644 src/world/generation/chunk/chunk.gd.uid create mode 100644 src/world/generation/chunk/chunk.tscn create mode 100644 src/world/generation/worldgen_manager/worldgen_manager.gd create mode 100644 src/world/generation/worldgen_manager/worldgen_manager.gd.uid create mode 100644 src/world/generation/worldgen_manager/worldgen_manager.tscn diff --git a/project.godot b/project.godot index 81c2f3b..e3320c1 100644 --- a/project.godot +++ b/project.godot @@ -17,6 +17,7 @@ config/features=PackedStringArray("4.5", "Forward Plus") [autoload] PhantomCameraManager="*res://addons/phantom_camera/scripts/managers/phantom_camera_manager.gd" +WorldGenManager="*res://src/world/generation/worldgen_manager/worldgen_manager.tscn" [debug] diff --git a/src/world/generation/chunk/chunk.gd b/src/world/generation/chunk/chunk.gd new file mode 100644 index 0000000..e49674a --- /dev/null +++ b/src/world/generation/chunk/chunk.gd @@ -0,0 +1,29 @@ +class_name Chunk extends Node3D +## A discrete generated chunk of the world + +const SIZE := Vector2(64, 64) +const SCENE := preload("res://src/world/generation/chunk/chunk.tscn") + + +func chunk_position() -> Vector2: + return Chunk.world_to_chunk(position) + + +func generate() -> void: + # TODO: this + print_debug("Generating chunk at ", chunk_position()) + + +static func chunk_to_world(chunk_pos: Vector2) -> Vector3: + return Vector3(chunk_pos.x * SIZE.x, 0, chunk_pos.y * SIZE.y) + + +static func world_to_chunk(world_pos: Vector3) -> Vector2: + return Vector2(world_pos.x / SIZE.x, world_pos.z / SIZE.y) + + +static func generate_chunk(chunk_pos: Vector2) -> Chunk: + var instance: Chunk = SCENE.instantiate() + instance.position = Chunk.chunk_to_world(chunk_pos) + instance.generate() + return instance diff --git a/src/world/generation/chunk/chunk.gd.uid b/src/world/generation/chunk/chunk.gd.uid new file mode 100644 index 0000000..59f2606 --- /dev/null +++ b/src/world/generation/chunk/chunk.gd.uid @@ -0,0 +1 @@ +uid://chqpqe4anvamd diff --git a/src/world/generation/chunk/chunk.tscn b/src/world/generation/chunk/chunk.tscn new file mode 100644 index 0000000..319936a --- /dev/null +++ b/src/world/generation/chunk/chunk.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=4 format=3 uid="uid://crs68yhijqkca"] + +[ext_resource type="Script" uid="uid://chqpqe4anvamd" path="res://src/world/generation/chunk/chunk.gd" id="1_87ter"] + +[sub_resource type="PlaneMesh" id="PlaneMesh_0cma0"] +size = Vector2(64, 64) +subdivide_width = 64 +subdivide_depth = 64 +center_offset = Vector3(32, 0, 32) + +[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_87ter"] +points = PackedVector3Array(1.9073486e-06, 0, 1.9073486e-06, 1.9073486e-06, 0, 64, 64, 0, 1.9073486e-06, 64, 0, 64) + +[node name="Chunk" type="Node3D"] +script = ExtResource("1_87ter") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] +mesh = SubResource("PlaneMesh_0cma0") + +[node name="StaticBody3D" type="StaticBody3D" parent="MeshInstance3D"] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="MeshInstance3D/StaticBody3D"] +shape = SubResource("ConvexPolygonShape3D_87ter") diff --git a/src/world/generation/generated_world.gd b/src/world/generation/generated_world.gd index 81bd08c..fe45bc2 100644 --- a/src/world/generation/generated_world.gd +++ b/src/world/generation/generated_world.gd @@ -1,11 +1,21 @@ class_name GeneratedWorld extends Node3D +@export var generation_radius := 6.0 -# Called when the node enters the scene tree for the first time. -func _ready() -> void: - pass # Replace with function body. +var chunks: Dictionary[Vector2, Chunk] = {} -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta: float) -> void: - pass +func get_chunk(chunk_pos: Vector2) -> Chunk: + if chunk_pos not in chunks: + var chunk := Chunk.generate_chunk(chunk_pos) + add_child(chunk) + chunks[chunk_pos] = chunk + return chunks[chunk_pos] + + +func _process(_delta: float) -> void: + var camera := get_viewport().get_camera_3d() + var center := Chunk.world_to_chunk(camera.global_position) + for x in range(center.x - generation_radius, center.x + generation_radius): + for y in range(center.y - generation_radius, center.y + generation_radius): + get_chunk(Vector2(x, y)) diff --git a/src/world/generation/worldgen_manager/worldgen_manager.gd b/src/world/generation/worldgen_manager/worldgen_manager.gd new file mode 100644 index 0000000..f808c36 --- /dev/null +++ b/src/world/generation/worldgen_manager/worldgen_manager.gd @@ -0,0 +1,4 @@ +class_name WorldGenManagerType extends Node +## Global autoloaded singleton controller for worldgen parameters + +@export var noise: FastNoiseLite diff --git a/src/world/generation/worldgen_manager/worldgen_manager.gd.uid b/src/world/generation/worldgen_manager/worldgen_manager.gd.uid new file mode 100644 index 0000000..f771211 --- /dev/null +++ b/src/world/generation/worldgen_manager/worldgen_manager.gd.uid @@ -0,0 +1 @@ +uid://7frynyj4vspc diff --git a/src/world/generation/worldgen_manager/worldgen_manager.tscn b/src/world/generation/worldgen_manager/worldgen_manager.tscn new file mode 100644 index 0000000..1ff916e --- /dev/null +++ b/src/world/generation/worldgen_manager/worldgen_manager.tscn @@ -0,0 +1,10 @@ +[gd_scene load_steps=3 format=3 uid="uid://dxelp6rnok01y"] + +[ext_resource type="Script" uid="uid://7frynyj4vspc" path="res://src/world/generation/worldgen_manager/worldgen_manager.gd" id="1_7xvag"] + +[sub_resource type="FastNoiseLite" id="FastNoiseLite_7xvag"] + +[node name="WorldGenManager" type="Node"] +script = ExtResource("1_7xvag") +noise = SubResource("FastNoiseLite_7xvag") +metadata/_custom_type_script = "uid://7frynyj4vspc"