Much stuff wow
This commit is contained in:
parent
7255cbdf64
commit
734730beee
45 changed files with 697 additions and 119 deletions
|
|
@ -1,4 +1,4 @@
|
|||
[gd_scene load_steps=14 format=3 uid="uid://bwcevwwphdvq"]
|
||||
[gd_scene load_steps=13 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"]
|
||||
|
|
@ -6,7 +6,6 @@
|
|||
[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="Script" uid="uid://cqko4m7cbxsfb" path="res://Entities/GroundTile/scripts/trees.gd" id="7_7lc7k"]
|
||||
[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")
|
||||
|
|
@ -31,7 +30,6 @@ 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)
|
||||
|
|
@ -71,4 +69,3 @@ mesh = SubResource("PlaneMesh_f37ob")
|
|||
|
||||
[node name="Trees" type="Node3D" parent="."]
|
||||
script = ExtResource("7_7lc7k")
|
||||
tree_scenes = Array[PackedScene]([ExtResource("7_224hx")])
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
extends Node3D
|
||||
@export var tree_scenes: Array[PackedScene] = []
|
||||
|
||||
@export var spawn_area_size: Vector2 = Vector2(2.0, 2.0)
|
||||
@export var max_trees: int = 3
|
||||
@export var min_distance: float = 0.5
|
||||
|
||||
var spawned_positions: Array[Vector3] = []
|
||||
var parent_ground_tile: GroundTile
|
||||
|
||||
|
|
@ -16,30 +16,31 @@ func spawn_trees_for_cell(cell_info: CellDataResource):
|
|||
if not parent_ground_tile:
|
||||
return
|
||||
|
||||
var tree_count = max(0, cell_info.trees.size())
|
||||
spawn_trees(tree_count)
|
||||
|
||||
func spawn_trees(tree_count: int):
|
||||
if tree_scenes.is_empty() or tree_count == 0:
|
||||
return
|
||||
|
||||
# Clear existing trees WITHOUT queue_free()
|
||||
for child in get_children():
|
||||
child.free() # Immediate cleanup instead of queue_free()
|
||||
spawned_positions.clear()
|
||||
|
||||
# Spawn new trees
|
||||
# Spawn each tree in the array
|
||||
var spawned_count = 0
|
||||
var attempts = 0
|
||||
var max_attempts = tree_count * 10
|
||||
var max_attempts = cell_info.trees.size() * 10
|
||||
|
||||
while spawned_positions.size() < tree_count and attempts < max_attempts:
|
||||
for tree_resource in cell_info.trees:
|
||||
if attempts >= max_attempts:
|
||||
Log.pr("Reached max attempts, could only spawn %d of %d trees" % [spawned_count, cell_info.trees.size()])
|
||||
break
|
||||
|
||||
var pos = get_random_position()
|
||||
|
||||
if is_position_valid(pos):
|
||||
spawn_tree_at_position(pos)
|
||||
spawn_tree_at_position(pos, tree_resource as TreeDataResource)
|
||||
spawned_positions.append(pos)
|
||||
spawned_count += 1
|
||||
|
||||
attempts += 1
|
||||
|
||||
Log.pr("Spawned %d of %d trees in cell" % [spawned_count, cell_info.trees.size()])
|
||||
|
||||
func get_random_position() -> Vector3:
|
||||
var rng = parent_ground_tile.get_rng()
|
||||
|
|
@ -47,17 +48,67 @@ func get_random_position() -> Vector3:
|
|||
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()
|
||||
func spawn_tree_at_position(pos: Vector3, tree_resource: TreeDataResource):
|
||||
if not tree_resource:
|
||||
Log.pr("No tree resource provided")
|
||||
return
|
||||
|
||||
|
||||
var tree_scene = preload("res://Entities/Tree/Tree.tscn")
|
||||
var tree_instance = tree_scene.instantiate()
|
||||
add_child(tree_instance)
|
||||
tree_instance.position = pos
|
||||
|
||||
var rng = parent_ground_tile.get_rng()
|
||||
tree_instance.rotation.y = rng.randf() * TAU
|
||||
|
||||
# Pass the TreeDataResource to the Tree instance
|
||||
if tree_instance.has_method("set_tree_data"):
|
||||
tree_instance.set_tree_data(tree_resource)
|
||||
elif tree_instance.has_method("setup_tree"):
|
||||
tree_instance.setup_tree(tree_resource)
|
||||
else:
|
||||
Log.pr("Tree instance doesn't have set_tree_data() or setup_tree() method")
|
||||
|
||||
func is_position_valid(pos: Vector3) -> bool:
|
||||
for existing_pos in spawned_positions:
|
||||
if pos.distance_to(existing_pos) < min_distance:
|
||||
return false
|
||||
return true
|
||||
|
||||
# Alternative method that shuffles trees for more random placement
|
||||
func spawn_trees_shuffled(cell_info: CellDataResource):
|
||||
if not cell_info:
|
||||
return
|
||||
|
||||
if not parent_ground_tile:
|
||||
return
|
||||
|
||||
# Clear existing trees
|
||||
for child in get_children():
|
||||
child.free()
|
||||
spawned_positions.clear()
|
||||
|
||||
# Create a copy and shuffle for random placement order
|
||||
var trees_to_spawn = cell_info.trees.duplicate()
|
||||
trees_to_spawn.shuffle()
|
||||
|
||||
var spawned_count = 0
|
||||
var attempts = 0
|
||||
var max_attempts = trees_to_spawn.size() * 10
|
||||
|
||||
for tree_resource in trees_to_spawn:
|
||||
if attempts >= max_attempts:
|
||||
Log.pr("Reached max attempts, could only spawn %d of %d trees" % [spawned_count, trees_to_spawn.size()])
|
||||
break
|
||||
|
||||
var pos = get_random_position()
|
||||
|
||||
if is_position_valid(pos):
|
||||
spawn_tree_at_position(pos, tree_resource as TreeDataResource)
|
||||
spawned_positions.append(pos)
|
||||
spawned_count += 1
|
||||
|
||||
attempts += 1
|
||||
|
||||
Log.pr("Spawned %d of %d trees in cell (shuffled)" % [spawned_count, trees_to_spawn.size()])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue