Tree collisions and highlights

This commit is contained in:
Dan Baker 2025-06-26 18:28:33 +01:00
parent 57602adddb
commit 7255cbdf64
17 changed files with 231 additions and 2251 deletions

View file

@ -1,14 +1,12 @@
[gd_scene load_steps=16 format=3 uid="uid://bwcevwwphdvq"]
[gd_scene load_steps=14 format=3 uid="uid://bwcevwwphdvq"]
[ext_resource type="Script" uid="uid://bq7hia2dit80y" path="res://Entities/GroundTile/ground_tile.gd" id="1_uwxqs"]
[ext_resource type="ArrayMesh" uid="uid://duj6747nq4qsk" path="res://Stages/Test3D/assets/stylizedGrassMeshes/grass.res" id="3_8mhad"]
[ext_resource type="Script" uid="uid://cacp8ncwuofuj" path="res://Entities/GroundTile/scripts/grass.gd" id="3_224hx"]
[ext_resource type="Material" uid="uid://b1miqvl8lus75" path="res://Stages/Test3D/GrassMaterialOverride.tres" id="3_f37ob"]
[ext_resource type="Script" uid="uid://btju6b83mvgvk" path="res://Entities/GroundTile/scripts/grass_multimesh.gd" id="4_3wpcb"]
[ext_resource type="PackedScene" uid="uid://dgvycnw8hpebx" path="res://Entities/Tree/assets/tree-tall.glb" id="6_7lc7k"]
[ext_resource type="Script" uid="uid://cqko4m7cbxsfb" path="res://Entities/GroundTile/scripts/trees.gd" id="7_7lc7k"]
[ext_resource type="PackedScene" uid="uid://bwhpbjdyl577e" path="res://Entities/Tree/assets/tree.glb" id="8_ot4p5"]
[ext_resource type="PackedScene" uid="uid://bwdibgbi3ycqn" path="res://Entities/Tree/assets/tree-autumn-tall.glb" id="12_4hjaq"]
[ext_resource type="PackedScene" uid="uid://c27fogucecn0r" path="res://Entities/Tree/Tree.tscn" id="7_224hx"]
[sub_resource type="ViewportTexture" id="ViewportTexture_h4g11"]
viewport_path = NodePath("DebugText/DebugTextViewport")
@ -33,6 +31,7 @@ flip_faces = true
script = ExtResource("1_uwxqs")
[node name="DebugText" type="Node3D" parent="."]
visible = false
[node name="DebugTextViewport" type="SubViewport" parent="DebugText"]
size = Vector2i(50, 50)
@ -72,4 +71,4 @@ mesh = SubResource("PlaneMesh_f37ob")
[node name="Trees" type="Node3D" parent="."]
script = ExtResource("7_7lc7k")
tree_scenes = Array[PackedScene]([ExtResource("8_ot4p5"), ExtResource("6_7lc7k"), ExtResource("12_4hjaq"), ExtResource("8_ot4p5"), ExtResource("8_ot4p5"), ExtResource("8_ot4p5"), ExtResource("8_ot4p5")])
tree_scenes = Array[PackedScene]([ExtResource("7_224hx")])

View file

@ -1,7 +1,5 @@
# GroundTile.gd
class_name GroundTile
extends Node3D
@onready var debug_text: Label = $DebugText/DebugTextViewport/DebugTextLabel
@onready var tree_spawner = $Trees
@onready var grass_spawner = $Grass
@ -9,17 +7,22 @@ var grid_x: int
var grid_z: int
var cell_info: CellDataResource = null
var spawners_ready: bool = false
var rng: RandomClass = RandomClass.new()
var cached_rng: RandomClass = null
func _ready() -> void:
spawners_ready = true
# Now that spawners are ready, trigger spawning if we have cell_info
if cell_info != null:
spawn_content()
update_text_label()
rng.set_seed(cell_info.cell_seed)
spawn_content()
update_text_label()
func get_rng() -> RandomClass:
if cached_rng == null and cell_info:
cached_rng = RandomClass.get_seeded_instance(cell_info.cell_seed)
elif cached_rng == null:
cached_rng = RandomClass.get_shared_instance()
return cached_rng
func set_grid_location(x, z) -> void:
grid_x = x

View file

@ -21,14 +21,16 @@ func spawn_grass_for_cell(value):
grass_multimesh.setup_multimesh()
func update_grass_density() -> void:
if parent_node == null or parent_node.rng == null:
if parent_node == null:
return
var rng = parent_node.get_rng()
if grass_density > 0.8:
grass_instance_range = parent_node.rng.randi_range(100, 500)
grass_instance_range = rng.randi_range(100, 500)
elif grass_density > 0.6:
grass_instance_range = parent_node.rng.randi_range(30, 50)
grass_instance_range = rng.randi_range(30, 50)
elif grass_density > 0.3:
grass_instance_range = parent_node.rng.randi_range(5, 20)
grass_instance_range = rng.randi_range(5, 20)
else:
grass_instance_range = parent_node.rng.randi_range(0, 1)
grass_instance_range = rng.randi_range(0, 1)

View file

@ -1,7 +1,6 @@
extends MultiMeshInstance3D
var mm: MultiMesh
var parent_node: GrassController
static var grass_mesh: Mesh = null
func _ready() -> void:
@ -16,7 +15,7 @@ func _ready() -> void:
func setup_multimesh() -> void:
if parent_node == null:
return
if grass_mesh == null:
Log.pr("Error: Could not load grass mesh")
return
@ -30,18 +29,21 @@ func setup_multimesh() -> void:
# Configure instance count
mm.instance_count = parent_node.grass_instance_range
# Get shared RNG from GroundTile
var rng = parent_node.parent_node.get_rng()
# Generate positions using shared RNG
for i in range(mm.instance_count):
var random_pos = Vector3(
parent_node.parent_node.rng.randf_range(-1.0, 1.0),
rng.randf_range(-1.0, 1.0),
0.0,
parent_node.parent_node.rng.randf_range(-1.0, 1.0)
rng.randf_range(-1.0, 1.0)
)
var random_rotation = parent_node.parent_node.rng.randf_range(0.0, TAU)
var random_rotation = rng.randf_range(0.0, TAU)
var basis = Basis(Vector3.UP, random_rotation)
var random_scale = parent_node.parent_node.rng.randf_range(0.05, 0.3)
var random_scale = rng.randf_range(0.05, 0.3)
basis = basis.scaled(Vector3(random_scale, random_scale, random_scale))
var tx = Transform3D(basis, random_pos)

View file

@ -11,29 +11,14 @@ func _ready():
func spawn_trees_for_cell(cell_info: CellDataResource):
if not cell_info:
return
# Use parent's RNG instead of creating new one
if not parent_ground_tile or not parent_ground_tile.rng:
return
return
if not parent_ground_tile:
return
var tree_count = max(0, cell_info.trees.size())
spawn_trees(tree_count)
# Update all rng calls to use parent_ground_tile.rng:
func get_random_position() -> Vector3:
var x = parent_ground_tile.rng.randf_range(-spawn_area_size.x / 2, spawn_area_size.x / 2)
var z = parent_ground_tile.rng.randf_range(-spawn_area_size.y / 2, spawn_area_size.y / 2)
return Vector3(x, 0, z)
func spawn_tree_at_position(pos: Vector3):
var random_index = parent_ground_tile.rng.randi() % tree_scenes.size()
var random_tree_scene = tree_scenes[random_index]
var tree_instance = random_tree_scene.instantiate()
add_child(tree_instance)
tree_instance.position = pos
tree_instance.rotation.y = parent_ground_tile.rng.randf() * TAU
func spawn_trees(tree_count: int):
if tree_scenes.is_empty() or tree_count == 0:
return
@ -56,6 +41,21 @@ func spawn_trees(tree_count: int):
attempts += 1
func get_random_position() -> Vector3:
var rng = parent_ground_tile.get_rng()
var x = rng.randf_range(-spawn_area_size.x / 2, spawn_area_size.x / 2)
var z = rng.randf_range(-spawn_area_size.y / 2, spawn_area_size.y / 2)
return Vector3(x, 0, z)
func spawn_tree_at_position(pos: Vector3):
var rng = parent_ground_tile.get_rng()
var random_index = rng.randi() % tree_scenes.size()
var random_tree_scene = tree_scenes[random_index]
var tree_instance = random_tree_scene.instantiate()
add_child(tree_instance)
tree_instance.position = pos
tree_instance.rotation.y = rng.randf() * TAU
func is_position_valid(pos: Vector3) -> bool:
for existing_pos in spawned_positions:
if pos.distance_to(existing_pos) < min_distance: