nature-sim/Utilities/MapData/MapPopulation.gd
Dan Baker f98773237e Implements tree visibility occlusion system.
Adds tree fading based on camera line of sight.
Trees now fade out when they obstruct the player's view,
improving visibility.

Also includes several fixes:
- Fixes tree distribution.
- Adds a see-through shader.
- Adds camera and occlusion scripts.
2025-07-01 10:32:21 +01:00

74 lines
2.6 KiB
GDScript

class_name MapPopulationClass
extends Node
var map_data: Array = Global.map_data
## Generate the CellDataResource for a given cell
## Setup the X and Z
## If it's a path or water we do nothing else for now
## If it's anything else then we need to:
## Set grass density directly from the vegetation density
## Then do the following:
## Density < 0.5 - chance of spawning special stuff and nothing else
## Density > 0.6 - add trees, varying quantity from 0.6 to 1
## Density 0.1 to 0.6 - add bushes, varying quantity TBD
## Density 0.1 to 0.4 - add flowers, varying quantity TBD
# Pre-calculate tree distribution for the cell
static func generate_cell_with_distribution(x: int, z: int, density: float, path: bool = false, water: bool = false, camp: bool = false):
var cell_data = CellDataResource.new()
cell_data.x = x
cell_data.z = z
cell_data.vegetation_density = density
cell_data.camp = camp
if not (path or water or camp):
if density >= 0.6:
var tree_preferences = BiomeData.calculate_tree_probabilities(x, z)
var tree_distribution = calculate_tree_distribution(tree_preferences, density)
# Add trees based on calculated distribution
for tree_type in tree_distribution.keys():
var quantity = tree_distribution[tree_type]["quantity"]
var resource = tree_distribution[tree_type]["resource"].duplicate()
if quantity > 0:
cell_data.add_trees(resource, quantity)
Global.map_data[x][z] = cell_data
# Calculate how many of each tree type to spawn
static func calculate_tree_distribution(tree_preferences: Dictionary, density: float) -> Dictionary:
var distribution = {}
if tree_preferences.is_empty():
return distribution
# Calculate total number of trees for this cell
var total_trees = int((density / 5) * 10) + randi_range(1, 2)
#var total_trees = int((density - 0.6) * 25) + randi_range(3, 8)
# Normalize the chances to get proportions
var total_weight = 0.0
for tree_name in tree_preferences.keys():
total_weight += tree_preferences[tree_name]["chance"]
if total_weight <= 0.0:
return distribution
# Distribute trees based on weighted probabilities
for tree_name in tree_preferences.keys():
var proportion = tree_preferences[tree_name]["chance"] / total_weight
var base_quantity = int(total_trees * proportion)
# Add some randomness - chance to get +1 tree based on remainder
var remainder = (total_trees * proportion) - base_quantity
if randf() < remainder:
base_quantity += 1
if base_quantity > 0:
distribution[tree_name] = {
"quantity": base_quantity,
"resource": tree_preferences[tree_name]["resource"]
}
return distribution