stick-the-quick/autoload/Wait/Wait.gd

62 lines
1.4 KiB
GDScript

extends Node
signal _ticked(delta: float)
func _ready() -> void:
process_mode = PROCESS_MODE_ALWAYS
func _process(delta: float) -> void:
_ticked.emit(delta)
func get_unix_time() -> int:
return Time.get_unix_time_from_datetime_dict(
Time.get_datetime_dict_from_system()
)
func tick() -> float:
return await _ticked
func ticks(n: int) -> float:
var sum: float = 0.0
while n > 0:
sum += await tick()
n -= 1
return sum
func seconds(timeout: float) -> float:
var sum: float = 0.0
while sum < timeout:
sum += await tick()
return sum
func minutes(timeout: float) -> float:
return await until(get_unix_time() + int(60*timeout))
func hours(timeout: float) -> float:
return await until(get_unix_time() + int(3600*timeout))
func until(condition: Variant, timeout: float = -1.0) -> float:
var sum: float = 0.0
if condition is int:
while get_unix_time() < condition and (
timeout < 0.0 or sum < timeout
):
sum += await tick()
elif condition is Promise:
while (not condition.is_finished()) and (
timeout < 0.0 or sum < timeout
):
sum += await tick()
elif condition is Callable:
while (not condition.call()) and (
timeout < 0.0 or sum < timeout
):
sum += await tick()
elif condition is Signal:
var p := Promise.new(condition)
while (not p.is_finished()) and (
timeout < 0.0 or sum < timeout
):
sum += await tick()
return sum