extends Path3D
## Tower Defense — Lecture 26 (Final)
##
## Generates visual road segments along a Path3D curve
## to show the enemy path clearly.

# ============================================================
# CONSTANTS
# ============================================================

const ROAD_WIDTH := 2.0
const ROAD_HEIGHT := 0.1
const SEGMENT_LENGTH := 1.0  # Distance between road tiles

# ============================================================
# ROAD APPEARANCE
# ============================================================

var road_color := Color(0.3, 0.25, 0.2)  # Brown/dirt color

# ============================================================
# LIFECYCLE
# ============================================================

func _ready() -> void:
	_generate_road_tiles()


## Generate CSG road tiles along the curve
func _generate_road_tiles() -> void:
	if curve == null or curve.get_point_count() < 2:
		return
	
	var total_length := curve.get_baked_length()
	var current_distance := 0.0
	
	# Create material once
	var road_material := StandardMaterial3D.new()
	road_material.albedo_color = road_color
	
	# Generate tiles along the path
	while current_distance < total_length:
		var pos := curve.sample_baked(current_distance)
		var next_pos := curve.sample_baked(current_distance + 0.1)
		var direction := (next_pos - pos).normalized()
		
		# Create road tile
		var tile := CSGBox3D.new()
		tile.size = Vector3(ROAD_WIDTH, ROAD_HEIGHT, SEGMENT_LENGTH)
		tile.material = road_material
		
		# Position and rotate tile
		tile.position = pos
		tile.position.y = 0.05  # Slightly above ground to avoid z-fighting
		
		# Rotate to face path direction
		if direction.length() > 0.01:
			tile.look_at(pos + direction, Vector3.UP)
			tile.rotate_y(PI / 2)  # Align length with path
		
		add_child(tile)
		
		current_distance += SEGMENT_LENGTH


## Time Complexity: O(n) where n = total_length / SEGMENT_LENGTH
## Space Complexity: O(n) for generated tiles
##
## Correctness: Samples curve at fixed intervals, creating tiles
## that visually approximate the path. Small gaps may appear on
## sharp curves due to discrete sampling.
