Fixing daynight cycle, and adding character home

This commit is contained in:
hz
2025-11-08 16:38:03 -06:00
parent d37ef6c0b9
commit 00be758651
6 changed files with 200 additions and 19 deletions
+49 -1
View File
@@ -2,6 +2,8 @@
extends DirectionalLight3D
class_name DayNightController
signal time_changed(current_hours: float)
@export var grid_manager_path: NodePath
@export_range(1.0, 48.0, 0.5) var day_length_hours: float = 12.0
@export_range(1.0, 600.0, 1.0) var real_seconds_per_game_hour: float = 60.0
@@ -11,12 +13,17 @@ class_name DayNightController
var _grid_manager: Node3D = null
var _time_of_day_hours: float = 0.0
var _initial_environment_settings: Dictionary = {}
var _sunlight_multiplier: float = 1.0
func _ready() -> void:
set_process(true)
_resolve_references()
_time_of_day_hours = clamp(start_time_of_day_hours, 0.0, day_length_hours)
_apply_sun_lighting()
_emit_time_changed()
_capture_initial_environment()
_update_environment()
func _process(delta: float) -> void:
if Engine.is_editor_hint():
@@ -25,6 +32,8 @@ func _process(delta: float) -> void:
return
_time_of_day_hours = fposmod(_time_of_day_hours + delta / real_seconds_per_game_hour, day_length_hours)
_apply_sun_lighting()
_emit_time_changed()
_update_environment()
func _resolve_references() -> void:
if grid_manager_path != NodePath():
@@ -51,7 +60,8 @@ func _apply_sun_lighting() -> void:
if direction.is_equal_approx(up_vector) or direction.is_equal_approx(-up_vector):
up_vector = Vector3.FORWARD
look_at_from_position(sun_position, target, up_vector)
var daylight: float = clamp(direction.y * 0.5 + 0.5, 0.0, 1.0)
var daylight_phase: float = sin((sun_angle - TAU * 0.25))
var daylight: float = clamp(daylight_phase * 0.5 + 0.5, 0.0, 1.0)
var dawn_color: Color = Color(0.95, 0.58, 0.32)
var night_color: Color = Color(0.12, 0.18, 0.35)
var day_color: Color = Color(1.0, 0.96, 0.88)
@@ -59,3 +69,41 @@ func _apply_sun_lighting() -> void:
light_color = night_color.lerp(warm_mix, daylight)
light_energy = lerp(0.05, 1.25, daylight)
shadow_enabled = daylight > 0.1
_sunlight_multiplier = daylight
func get_time_of_day_hours() -> float:
return _time_of_day_hours
func get_day_length_hours() -> float:
return day_length_hours
func _emit_time_changed() -> void:
emit_signal("time_changed", _time_of_day_hours)
func _capture_initial_environment() -> void:
var world: World3D = get_world_3d()
if not world:
return
var env: Environment = world.environment
if env:
_initial_environment_settings = {
"ambient_light_color": env.ambient_light_color,
"ambient_light_energy": env.ambient_light_energy,
"background_color": env.background_color
}
func _update_environment() -> void:
var world: World3D = get_world_3d()
if not world:
return
var env: Environment = world.environment
if not env:
return
var daylight: float = clamp(_sunlight_multiplier, 0.0, 1.0)
var ambient_day: Color = Color(0.6, 0.7, 0.85)
var ambient_night: Color = Color(0.02, 0.05, 0.1)
var background_day: Color = Color(0.5, 0.75, 1.0)
var background_night: Color = Color(0.01, 0.02, 0.08)
env.ambient_light_color = ambient_night.lerp(ambient_day, daylight)
env.ambient_light_energy = lerp(0.15, 1.5, daylight)
env.background_color = background_night.lerp(background_day, daylight)