extends Node3D
## Tower Defense — Lecture 27 (Final)
##
## Generates a visual grid overlay to show where towers
## can be placed. Grid lines are subtle and persistent.

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

const GRID_SIZE := 2.0  # Size of each grid cell
const GRID_WIDTH := 20  # Number of cells in X direction
const GRID_DEPTH := 15  # Number of cells in Z direction
const LINE_WIDTH := 0.05
const LINE_HEIGHT := 0.02

# ============================================================
# GRID APPEARANCE
# ============================================================

var grid_color := Color(0.3, 0.3, 0.3, 0.3)  # Subtle gray

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

func _ready() -> void:
	_generate_grid_lines()


## Generate grid line meshes
func _generate_grid_lines() -> void:
	var material := StandardMaterial3D.new()
	material.albedo_color = grid_color
	material.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
	material.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
	
	# Calculate grid bounds
	var half_width := (GRID_WIDTH * GRID_SIZE) / 2.0
	var half_depth := (GRID_DEPTH * GRID_SIZE) / 2.0
	
	# Generate lines parallel to Z axis (vertical in top-down view)
	for i in range(GRID_WIDTH + 1):
		var x := -half_width + i * GRID_SIZE
		var line := CSGBox3D.new()
		line.size = Vector3(LINE_WIDTH, LINE_HEIGHT, GRID_DEPTH * GRID_SIZE)
		line.material = material
		line.position = Vector3(x, 0.01, 0)
		add_child(line)
	
	# Generate lines parallel to X axis (horizontal in top-down view)
	for j in range(GRID_DEPTH + 1):
		var z := -half_depth + j * GRID_SIZE
		var line := CSGBox3D.new()
		line.size = Vector3(GRID_WIDTH * GRID_SIZE, LINE_HEIGHT, LINE_WIDTH)
		line.material = material
		line.position = Vector3(0, 0.01, z)
		add_child(line)


## Time Complexity: O(w + d) where w = GRID_WIDTH, d = GRID_DEPTH
## Space Complexity: O(w + d) for generated lines
##
## Correctness: Creates GRID_WIDTH + GRID_DEPTH + 2 lines total,
## forming a complete grid pattern across the playable area.
