From c206823b30d2b427b7faa1cd87ef618a76d49e87 Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Sun, 3 Oct 2021 17:23:08 +0800 Subject: [PATCH] doc --- scheduler/SchedulerBus.gd | 16 --------- script/demo02_base/MyClass.gd | 2 +- script/demo_test/demoTest.gd | 2 +- zfoo/common/Runnable.gd | 5 +++ zfoo/scheduler/SchedulerBus.gd | 40 ++++++++++++++++++++++ zfoo/scheduler/SchedulerDefinition.gd | 22 ++++++++++++ zfoo/scheduler/TimeUtils.gd | 15 ++++++++ zfoo/test/ZfooTest.tscn | 10 ++++++ zfoo/test/scheduler/MySchedulerRunnable.gd | 5 +++ zfoo/test/scheduler/SchedulerTest.gd | 7 ++++ zfoo/test/zfoo_test.gd | 9 +++++ {util => zfoo/util}/ArrayUtils.gd | 0 zfoo/util/CollectionUtils.gd | 5 +++ {util => zfoo/util}/NumberUtils.gd | 0 {util => zfoo/util}/RandomUtils.gd | 4 +-- {util => zfoo/util}/StringUtils.gd | 2 +- zfoo/zfoo.gd | 15 ++++++++ 17 files changed, 138 insertions(+), 21 deletions(-) delete mode 100644 scheduler/SchedulerBus.gd create mode 100644 zfoo/common/Runnable.gd create mode 100644 zfoo/scheduler/SchedulerBus.gd create mode 100644 zfoo/scheduler/SchedulerDefinition.gd create mode 100644 zfoo/scheduler/TimeUtils.gd create mode 100644 zfoo/test/ZfooTest.tscn create mode 100644 zfoo/test/scheduler/MySchedulerRunnable.gd create mode 100644 zfoo/test/scheduler/SchedulerTest.gd create mode 100644 zfoo/test/zfoo_test.gd rename {util => zfoo/util}/ArrayUtils.gd (100%) create mode 100644 zfoo/util/CollectionUtils.gd rename {util => zfoo/util}/NumberUtils.gd (100%) rename {util => zfoo/util}/RandomUtils.gd (89%) rename {util => zfoo/util}/StringUtils.gd (90%) create mode 100644 zfoo/zfoo.gd diff --git a/scheduler/SchedulerBus.gd b/scheduler/SchedulerBus.gd deleted file mode 100644 index 1eccaec..0000000 --- a/scheduler/SchedulerBus.gd +++ /dev/null @@ -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 diff --git a/script/demo02_base/MyClass.gd b/script/demo02_base/MyClass.gd index d6352bb..f1d8d87 100644 --- a/script/demo02_base/MyClass.gd +++ b/script/demo02_base/MyClass.gd @@ -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" diff --git a/script/demo_test/demoTest.gd b/script/demo_test/demoTest.gd index 110a5cf..8d62c3f 100644 --- a/script/demo_test/demoTest.gd +++ b/script/demo_test/demoTest.gd @@ -1,6 +1,6 @@ extends Node2D -const RandomUtils = preload("res://util/RandomUtils.gd") +const RandomUtils = preload("res://zfoo/util/RandomUtils.gd") var count = 0 diff --git a/zfoo/common/Runnable.gd b/zfoo/common/Runnable.gd new file mode 100644 index 0000000..6794b39 --- /dev/null +++ b/zfoo/common/Runnable.gd @@ -0,0 +1,5 @@ +extends Object + + +func run(): + pass diff --git a/zfoo/scheduler/SchedulerBus.gd b/zfoo/scheduler/SchedulerBus.gd new file mode 100644 index 0000000..f49cbdf --- /dev/null +++ b/zfoo/scheduler/SchedulerBus.gd @@ -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() diff --git a/zfoo/scheduler/SchedulerDefinition.gd b/zfoo/scheduler/SchedulerDefinition.gd new file mode 100644 index 0000000..ce856ce --- /dev/null +++ b/zfoo/scheduler/SchedulerDefinition.gd @@ -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 diff --git a/zfoo/scheduler/TimeUtils.gd b/zfoo/scheduler/TimeUtils.gd new file mode 100644 index 0000000..f729154 --- /dev/null +++ b/zfoo/scheduler/TimeUtils.gd @@ -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] diff --git a/zfoo/test/ZfooTest.tscn b/zfoo/test/ZfooTest.tscn new file mode 100644 index 0000000..ca3e2ce --- /dev/null +++ b/zfoo/test/ZfooTest.tscn @@ -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 ) diff --git a/zfoo/test/scheduler/MySchedulerRunnable.gd b/zfoo/test/scheduler/MySchedulerRunnable.gd new file mode 100644 index 0000000..e74a53f --- /dev/null +++ b/zfoo/test/scheduler/MySchedulerRunnable.gd @@ -0,0 +1,5 @@ +extends "res://zfoo/common/Runnable.gd" + + +func run(): + print("MySchedulerRunnnable!!!") diff --git a/zfoo/test/scheduler/SchedulerTest.gd b/zfoo/test/scheduler/SchedulerTest.gd new file mode 100644 index 0000000..5e41735 --- /dev/null +++ b/zfoo/test/scheduler/SchedulerTest.gd @@ -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) diff --git a/zfoo/test/zfoo_test.gd b/zfoo/test/zfoo_test.gd new file mode 100644 index 0000000..47d9a7a --- /dev/null +++ b/zfoo/test/zfoo_test.gd @@ -0,0 +1,9 @@ +extends Node2D + +const SchedulerTest = preload("res://zfoo/test/scheduler/SchedulerTest.gd") + +func _ready(): + SchedulerTest.test() + + + diff --git a/util/ArrayUtils.gd b/zfoo/util/ArrayUtils.gd similarity index 100% rename from util/ArrayUtils.gd rename to zfoo/util/ArrayUtils.gd diff --git a/zfoo/util/CollectionUtils.gd b/zfoo/util/CollectionUtils.gd new file mode 100644 index 0000000..3486ee8 --- /dev/null +++ b/zfoo/util/CollectionUtils.gd @@ -0,0 +1,5 @@ +extends Object + + +static func isEmpty(dictionary: Dictionary) -> bool: + return dictionary == null or dictionary.size() == 0 diff --git a/util/NumberUtils.gd b/zfoo/util/NumberUtils.gd similarity index 100% rename from util/NumberUtils.gd rename to zfoo/util/NumberUtils.gd diff --git a/util/RandomUtils.gd b/zfoo/util/RandomUtils.gd similarity index 89% rename from util/RandomUtils.gd rename to zfoo/util/RandomUtils.gd index 05ba0ee..442c81c 100644 --- a/util/RandomUtils.gd +++ b/zfoo/util/RandomUtils.gd @@ -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] diff --git a/util/StringUtils.gd b/zfoo/util/StringUtils.gd similarity index 90% rename from util/StringUtils.gd rename to zfoo/util/StringUtils.gd index a00e57c..af2b580 100644 --- a/util/StringUtils.gd +++ b/zfoo/util/StringUtils.gd @@ -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 = "{}" diff --git a/zfoo/zfoo.gd b/zfoo/zfoo.gd new file mode 100644 index 0000000..627a677 --- /dev/null +++ b/zfoo/zfoo.gd @@ -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