Refactors grass and tree spawning for seed consistency
Updates grass and tree spawning to use the parent's RNG, ensuring consistent random generation based on the seed. This removes redundant RNG instances and ensures that grass and trees are generated predictably for a given cell.
This commit is contained in:
parent
e7337bede6
commit
33c525a3c0
2 changed files with 40 additions and 42 deletions
|
|
@ -5,21 +5,36 @@ extends Node3D
|
|||
@export var min_distance: float = 0.5
|
||||
var spawned_positions: Array[Vector3] = []
|
||||
var parent_ground_tile: GroundTile
|
||||
var rng: RandomClass = RandomClass.new()
|
||||
# Remove this line: var rng: RandomClass = RandomClass.new()
|
||||
|
||||
func _ready():
|
||||
parent_ground_tile = get_parent() as GroundTile
|
||||
|
||||
|
||||
func spawn_trees_for_cell(cell_info: CellDataResource):
|
||||
if not cell_info:
|
||||
return
|
||||
|
||||
rng.set_seed(cell_info.cell_seed)
|
||||
|
||||
return
|
||||
|
||||
# Use parent's RNG instead of creating new one
|
||||
if not parent_ground_tile or not parent_ground_tile.rng:
|
||||
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
|
||||
|
|
@ -42,22 +57,8 @@ func spawn_trees(tree_count: int):
|
|||
|
||||
attempts += 1
|
||||
|
||||
# Rest of your functions stay the same...
|
||||
func get_random_position() -> Vector3:
|
||||
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 is_position_valid(pos: Vector3) -> bool:
|
||||
for existing_pos in spawned_positions:
|
||||
if pos.distance_to(existing_pos) < min_distance:
|
||||
return false
|
||||
return true
|
||||
|
||||
func spawn_tree_at_position(pos: Vector3):
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue