Files
tgcc/core/daynight/day_night_controller.gd

45 lines
1.0 KiB
GDScript

@tool
class_name DayNightController
extends Node
#Time duration in seconds for a day
@export var day_duration: float = 120.0
#starting time
@export_range(0.0, 1.0) var start_time: float = 0.0
#Multiply for debug
@export var time_scale: float = 1.0
@export var paused: bool = false
signal time_changed(time: float)
var current_time: float = 0.0
func _ready() -> void:
current_time = start_time
update_time(current_time)
func _process(delta: float) -> void:
if paused or day_duration <= 0.0:
return
var increment: float = (delta * time_scale) / day_duration
current_time = wrapf(current_time + increment, 0.0, 1.0)
update_time(current_time)
func get_time_string() -> String:
var total_minutes: int = int(current_time * 1440.0) # 1440 = 24*60
var hours: int = total_minutes / 60
var minutes: int = total_minutes % 60
return "%02d:%02d" % [hours, minutes]
func set_time(t: float) -> void:
current_time = wrapf(t, 0.0, 1.0)
update_time(current_time)
func update_time(currtime: float) -> void:
emit_signal("time_changed", currtime)