Files
godot-playground/scenes/Grid/time_display.gd
T

57 lines
1.7 KiB
GDScript

@tool
extends Label
class_name TimeDisplay
@export var day_night_controller_path: NodePath
@export var display_enabled: bool = true
var _day_night_controller: DayNightController = null
func _ready() -> void:
set_process(true)
_resolve_references()
_connect_controller()
_update_visibility()
_update_text()
func _process(_delta: float) -> void:
if Engine.is_editor_hint():
_update_text()
func set_display_enabled(value: bool) -> void:
display_enabled = value
_update_visibility()
_update_text()
func _resolve_references() -> void:
if day_night_controller_path != NodePath():
_day_night_controller = get_node_or_null(day_night_controller_path) as DayNightController
else:
_day_night_controller = get_parent() as DayNightController
func _connect_controller() -> void:
if _day_night_controller and not _day_night_controller.is_connected("time_changed", Callable(self, "_on_time_changed")):
_day_night_controller.time_changed.connect(_on_time_changed)
func _on_time_changed(_hours: float) -> void:
_update_text()
func _update_visibility() -> void:
visible = display_enabled and _day_night_controller != null
func _update_text() -> void:
if not display_enabled or not _day_night_controller:
text = ""
return
var hours: float = _day_night_controller.get_time_of_day_hours()
var total_hours: float = max(_day_night_controller.get_day_length_hours(), 0.001)
var clamped_hours: float = clamp(hours, 0.0, total_hours)
var whole_hours: int = int(clamped_hours)
var minutes: int = int(round((clamped_hours - float(whole_hours)) * 60.0))
if minutes == 60:
whole_hours += 1
minutes = 0
if whole_hours >= int(total_hours):
whole_hours = 0
text = "%02d:%02d" % [whole_hours, minutes]