Basics of chunk generation
All checks were successful
linting & formatting / build (push) Successful in 13s

This commit is contained in:
Rob Kelly 2025-09-29 19:04:41 -06:00
parent ed77a1afea
commit b709118be2
8 changed files with 85 additions and 6 deletions

View File

@ -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]

View File

@ -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

View File

@ -0,0 +1 @@
uid://chqpqe4anvamd

View File

@ -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")

View File

@ -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))

View File

@ -0,0 +1,4 @@
class_name WorldGenManagerType extends Node
## Global autoloaded singleton controller for worldgen parameters
@export var noise: FastNoiseLite

View File

@ -0,0 +1 @@
uid://7frynyj4vspc

View File

@ -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"