From d2e82658fefea2ae8a159b9af29801e4f4629a2e Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Fri, 17 Sep 2021 19:12:54 +0800 Subject: [PATCH] =?UTF-8?q?perf[gdscript]:=20=E5=A2=9E=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/demo01_hello/hello.md | 12 ++++++++++- scene/demo_test/DemoTest.tscn | 6 ++++++ script/demo_test/demoTest.gd | 23 +++++++++++++++++++++ util/NumberUtils.gd | 6 ++++++ util/RandomUtils.gd | 38 ++++++++++++++++++++++++++++++++--- 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 scene/demo_test/DemoTest.tscn create mode 100644 script/demo_test/demoTest.gd create mode 100644 util/NumberUtils.gd diff --git a/doc/demo01_hello/hello.md b/doc/demo01_hello/hello.md index fb28ae7..24f1ee3 100644 --- a/doc/demo01_hello/hello.md +++ b/doc/demo01_hello/hello.md @@ -31,4 +31,14 @@ ue虽然开源源代码,但是代码量太大,历史包袱太重,不适合 使用godot你既可以用GDScript去编写代码,也可以用C++去编写代码,还可以去改底层引擎代码 ``` -- 用godot你能感觉我能把控底层,我能把控每一行代码的底层细节,这个是程序员的浪漫,godot在国外比较火是有原因的 \ No newline at end of file +- 用godot你能感觉我能把控底层,我能把控每一行代码的底层细节,这个是程序员的浪漫,godot在国外比较火是有原因的 + +# 为使用godot我应该选择什么语言,C# or GDScript + +- gds更加简单,支持的更加完整,而且是脚本可以热更新 +- C#很多库用不了,有限制,如果是为了性能可以在godot中使用C++ +- C#对于godot来说比较重,推荐gds +- unity在很久之前也同时支持过js和C#,最后放弃了js,主要是因为unity是mono和C#非常友好 +- godot使用C++去解释执行gds,这个时候对于godot来说比较重,推荐gds +- 通过学习gds也可以学到一些编译原理的知识 +- godot不仅仅是一个游戏引擎,而且还是一个优质的学习资源 \ No newline at end of file diff --git a/scene/demo_test/DemoTest.tscn b/scene/demo_test/DemoTest.tscn new file mode 100644 index 0000000..f882b8a --- /dev/null +++ b/scene/demo_test/DemoTest.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://script/demo_test/demoTest.gd" type="Script" id=1] + +[node name="Node2D" type="Node2D"] +script = ExtResource( 1 ) diff --git a/script/demo_test/demoTest.gd b/script/demo_test/demoTest.gd new file mode 100644 index 0000000..110a5cf --- /dev/null +++ b/script/demo_test/demoTest.gd @@ -0,0 +1,23 @@ +extends Node2D + +const RandomUtils = preload("res://util/RandomUtils.gd") + +var count = 0 + +func _ready(): + print(RandomUtils.randomBoolean()) + randomize() + + +func _process(delta): + count = count + 1 + if count % 100 == 0: + randomTest() + pass + + +func randomTest(): + print(RandomUtils.randomBoolean()) + print(RandomUtils.randomInt()) + print(RandomUtils.randomIntLimit(2)) + print(RandomUtils.randomIntRange(-2147483648, -2147483646)) diff --git a/util/NumberUtils.gd b/util/NumberUtils.gd new file mode 100644 index 0000000..ef76bb4 --- /dev/null +++ b/util/NumberUtils.gd @@ -0,0 +1,6 @@ +extends Object + +const MIN_INT: int = -2147483648 + +const MAX_INT: int = 2147483647 + diff --git a/util/RandomUtils.gd b/util/RandomUtils.gd index d1d0c67..612a828 100644 --- a/util/RandomUtils.gd +++ b/util/RandomUtils.gd @@ -1,8 +1,40 @@ extends Object +const NumberUtils = preload("res://util/NumberUtils.gd") +const StringUtils = preload("res://util/StringUtils.gd") -const random = [null, false] +const random = [false, null] -static func init(): - random[0] = RandomNumberGenerator.new() +# 单例模式 +static func getRandom() -> RandomNumberGenerator: + if random[0]: + return random[1] + var randomGenerator = RandomNumberGenerator.new() + randomGenerator.randomize() + random[1] = randomGenerator + return randomGenerator + + +static func randomBoolean() -> bool: + return 1 == getRandom().randi_range(0, 2) + + +# 获得随机数[-2^32, 2^32) +static func randomInt() -> int: + return randomIntRange(NumberUtils.MIN_INT, NumberUtils.MAX_INT) + +# 获得指定范围内的随机数 [0,limit) +static func randomIntLimit(limit: int) -> int: + return randomIntRange(0, limit) + +# 获得随机数[-2^32, 2^32) +static func randomIntRange(from: int, to: int) -> int: + if from < NumberUtils.MIN_INT: + push_error(StringUtils.format("from [{}] shoud be >= [{}]", [from, NumberUtils.MIN_INT])) + return 0 + if to > NumberUtils.MAX_INT: + push_error(StringUtils.format("to [{}] should be <= [{}]", [from, NumberUtils.MAX_INT])) + return 0 + + return getRandom().randi_range(from, to - 1)