extends Node3D

@export var day_length := 120.0 # seconds for full rotation
@export var start_light_angle := -90.0
var end_light_angle = start_light_angle + 360.0
var start_radians = start_light_angle * PI / 180
var time := 0.0

@onready var sun := $DirectionalLight3D

func _process(delta):
	time = fmod((time + delta), day_length)
	var t = time / day_length

	# Rotate sun around X axis
	var angle = lerp(start_light_angle, end_light_angle, t) # sunrise → sunset → night → sunrise
	sun.rotation_degrees.x = angle

	# Adjust intensity
	var curSin = -sin((t * TAU) + start_radians)
	var energy = clamp((curSin * 1.0) + 0.2, 0.0, 1.2)
	sun.light_energy = energy
