pull/2/head
jaysunxiao 2021-10-03 17:23:08 +08:00
parent e838f6fe5b
commit c206823b30
17 changed files with 138 additions and 21 deletions

View File

@ -1,16 +0,0 @@
extends Node
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

View File

@ -1,6 +1,6 @@
# extends Node2D
const StringUtils = preload("res://util/StringUtils.gd")
const StringUtils = preload("res://zfoo/util/StringUtils.gd")
# (optional) class definition with a custom icon
class_name MyClass, "res://icon.png"

View File

@ -1,6 +1,6 @@
extends Node2D
const RandomUtils = preload("res://util/RandomUtils.gd")
const RandomUtils = preload("res://zfoo/util/RandomUtils.gd")
var count = 0

5
zfoo/common/Runnable.gd Normal file
View File

@ -0,0 +1,5 @@
extends Object
func run():
pass

View File

@ -0,0 +1,40 @@
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()

View File

@ -0,0 +1,22 @@
extends Object
const Runnable = preload("res://zfoo/common/Runnable.gd")
# the time from now to delay execution
var delay: int
# 触发时间
var triggerTimestamp: int
# 是否重复
var repteated: bool
# 回调方法
var runnable: Runnable
func _init(runnable: Runnable, delay: int, triggerTimestamp: int, repteated: bool = false):
self.runnable = runnable
self.delay = delay
self.triggerTimestamp = triggerTimestamp
self.repteated = repteated

View File

@ -0,0 +1,15 @@
extends Object
const timestamp = [0]
# 获取精确的时间戳
static func currentTimeMillis() -> int:
var currentTimeMillis = OS.get_system_time_msecs()
timestamp[0] = currentTimeMillis
return currentTimeMillis
# 获取最多只有一秒延迟的粗略时间戳,适用于对时间精度要求不高的场景,最多只有一秒误差
static func now() -> int:
return timestamp[0]

10
zfoo/test/ZfooTest.tscn Normal file
View File

@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://zfoo/test/zfoo_test.gd" type="Script" id=1]
[ext_resource path="res://zfoo/zfoo.gd" type="Script" id=2]
[node name="Node2D" type="Node2D"]
script = ExtResource( 1 )
[node name="zfoo" type="Node2D" parent="."]
script = ExtResource( 2 )

View File

@ -0,0 +1,5 @@
extends "res://zfoo/common/Runnable.gd"
func run():
print("MySchedulerRunnnable!!!")

View File

@ -0,0 +1,7 @@
const SchedulerBus = preload("res://zfoo/scheduler/SchedulerBus.gd")
const MySchedulerRunnable = preload("res://zfoo/test/scheduler/MySchedulerRunnable.gd")
const TimeUtils = preload("res://zfoo/scheduler/TimeUtils.gd")
static func test():
SchedulerBus.schedule(MySchedulerRunnable.new(), 5000)

9
zfoo/test/zfoo_test.gd Normal file
View File

@ -0,0 +1,9 @@
extends Node2D
const SchedulerTest = preload("res://zfoo/test/scheduler/SchedulerTest.gd")
func _ready():
SchedulerTest.test()

View File

@ -0,0 +1,5 @@
extends Object
static func isEmpty(dictionary: Dictionary) -> bool:
return dictionary == null or dictionary.size() == 0

View File

@ -1,7 +1,7 @@
extends Object
const NumberUtils = preload("res://util/NumberUtils.gd")
const StringUtils = preload("res://util/StringUtils.gd")
const NumberUtils = preload("res://zfoo/util/NumberUtils.gd")
const StringUtils = preload("res://zfoo/util/StringUtils.gd")
const random = [false, null]

View File

@ -1,6 +1,6 @@
extends Object
const ArrayUtils = preload("res://util/ArrayUtils.gd")
const ArrayUtils = preload("res://zfoo/util/ArrayUtils.gd")
const EMPTY: String = ""
const EMPTY_JSON: String = "{}"

15
zfoo/zfoo.gd Normal file
View File

@ -0,0 +1,15 @@
extends Node2D
const SchedulerBus = preload("res://zfoo/scheduler/SchedulerBus.gd")
var timeCounter: float = 0
func _physics_process(delta):
schedulerUpdate(delta)
func schedulerUpdate(delta: float) -> void:
timeCounter += delta
if (int(timeCounter) == 1) :
SchedulerBus.triggerPerSecond()
timeCounter = 0