41 lines
1.2 KiB
GDScript
41 lines
1.2 KiB
GDScript
|
extends Object
|
||
|
|
||
|
const Runnable = preload("res://zfoo/common/Runnable.gd")
|
||
|
|
||
|
const SchedulerDefinition = preload("res://zfoo/scheduler/SchedulerDefinition.gd")
|
||
|
const TimeUtils = preload("res://zfoo/scheduler/TimeUtils.gd")
|
||
|
const ArrayUtils = preload("res://zfoo/util/ArrayUtils.gd")
|
||
|
const CollectionUtils = preload("res://zfoo/util/CollectionUtils.gd")
|
||
|
|
||
|
const schedulerMap: Dictionary = {}
|
||
|
|
||
|
static func schedule(runnable: Runnable, delay: int) -> void:
|
||
|
var triggerTimestamp = TimeUtils.currentTimeMillis() + delay
|
||
|
var definition = SchedulerDefinition.new(runnable, delay, triggerTimestamp, false)
|
||
|
schedulerMap[definition] = null
|
||
|
|
||
|
|
||
|
static func triggerPerSecond() -> void:
|
||
|
var timestamp = TimeUtils.currentTimeMillis()
|
||
|
|
||
|
if CollectionUtils.isEmpty(schedulerMap):
|
||
|
return
|
||
|
|
||
|
var deleteSchedulers = []
|
||
|
for scheduler in schedulerMap.keys():
|
||
|
if timestamp < scheduler.triggerTimestamp:
|
||
|
continue
|
||
|
if scheduler.repteated:
|
||
|
scheduler.triggerTimestamp += timestamp
|
||
|
else:
|
||
|
deleteSchedulers.append(scheduler)
|
||
|
scheduler.runnable.run()
|
||
|
|
||
|
if ArrayUtils.isEmpty(deleteSchedulers):
|
||
|
return
|
||
|
|
||
|
for scheduler in deleteSchedulers:
|
||
|
schedulerMap.erase(scheduler)
|
||
|
scheduler.runnable.free()
|
||
|
scheduler.free()
|