diff --git a/doc/demo02_base/base.md b/doc/demo02_base/base.md index e7f701e..6b7cbf8 100644 --- a/doc/demo02_base/base.md +++ b/doc/demo02_base/base.md @@ -20,6 +20,8 @@ ![Image text](image/gc2.png) ![Image text](image/gc3.png) +- gdscript是垃圾回收和手动回收结合起来了,使用什么样的回收完全交给你自己 +![Image text](image/太极.png) diff --git a/doc/demo02_base/image/太极.png b/doc/demo02_base/image/太极.png new file mode 100644 index 0000000..2556de3 Binary files /dev/null and b/doc/demo02_base/image/太极.png differ diff --git a/doc/demo02_base/image/太极.png.import b/doc/demo02_base/image/太极.png.import new file mode 100644 index 0000000..f8692c9 --- /dev/null +++ b/doc/demo02_base/image/太极.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/太极.png-59d7111e2abf9f43bec4eb921f3e6540.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo02_base/image/太极.png" +dest_files=[ "res://.import/太极.png-59d7111e2abf9f43bec4eb921f3e6540.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo05_scene_node/scene_node.md b/doc/demo05_scene_node/scene_node.md index 7692b05..05469f0 100644 --- a/doc/demo05_scene_node/scene_node.md +++ b/doc/demo05_scene_node/scene_node.md @@ -17,6 +17,16 @@ unity的2d坐标系虽然在左下角,但是unity的屏幕坐标系依然在 因为单论平面内容制作,左上原点更符合视觉习惯,也更符合设计常识,比如ps。 ``` +# 全局坐标和局部坐标(相对坐标) + +- 全局坐标和局部坐标可以相互转化 + +``` +Node2D及其子节点的位置可以使用position和gloabl_position来控制,其中,前者是该节点相对于父节点的相对位置,后者是全局位置。 +简而言之,position的坐标系是以父节点的位置坐标为原点的坐标系,方向,还是水平向右为x轴正方向,竖直向下为y轴正方向。 +需要注意的是,全局位置是以场景的左上角为原点的,并不是以根结点的位置作为原点的 +``` + # 场景和节点 ![Image text](image/场景.png) diff --git a/doc/demo06_move/image/move1.png b/doc/demo06_move/image/move1.png new file mode 100644 index 0000000..95315c4 Binary files /dev/null and b/doc/demo06_move/image/move1.png differ diff --git a/doc/demo06_move/image/move1.png.import b/doc/demo06_move/image/move1.png.import new file mode 100644 index 0000000..a3f161d --- /dev/null +++ b/doc/demo06_move/image/move1.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/move1.png-95aa6fa257a51da26ff41252e07cc282.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo06_move/image/move1.png" +dest_files=[ "res://.import/move1.png-95aa6fa257a51da26ff41252e07cc282.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo06_move/image/move2.png b/doc/demo06_move/image/move2.png new file mode 100644 index 0000000..3c2f93f Binary files /dev/null and b/doc/demo06_move/image/move2.png differ diff --git a/doc/demo06_move/image/move2.png.import b/doc/demo06_move/image/move2.png.import new file mode 100644 index 0000000..4bd0bad --- /dev/null +++ b/doc/demo06_move/image/move2.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/move2.png-b5b7532785e6fca5f754677b3a706a98.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo06_move/image/move2.png" +dest_files=[ "res://.import/move2.png-b5b7532785e6fca5f754677b3a706a98.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo06_move/image/move3.png b/doc/demo06_move/image/move3.png new file mode 100644 index 0000000..69de82b Binary files /dev/null and b/doc/demo06_move/image/move3.png differ diff --git a/doc/demo06_move/image/move3.png.import b/doc/demo06_move/image/move3.png.import new file mode 100644 index 0000000..f49bf54 --- /dev/null +++ b/doc/demo06_move/image/move3.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/move3.png-7f98e06376aee3a2f31cf0a02a960d9b.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo06_move/image/move3.png" +dest_files=[ "res://.import/move3.png-7f98e06376aee3a2f31cf0a02a960d9b.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo06_move/move.md b/doc/demo06_move/move.md new file mode 100644 index 0000000..2968368 --- /dev/null +++ b/doc/demo06_move/move.md @@ -0,0 +1,3 @@ +![Image text](image/move1.png) +![Image text](image/move2.png) +![Image text](image/move3.png) diff --git a/project.godot b/project.godot index 9515db7..77df0d8 100644 --- a/project.godot +++ b/project.godot @@ -24,6 +24,41 @@ config/name="godot-util" run/main_scene="res://scene/demo01_hello/HelloWorld.tscn" config/icon="res://icon.png" +[input] + +move_left={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null) + ] +} +move_right={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null) + ] +} +move_up={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null) + ] +} +move_down={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null) + ] +} + [physics] common/enable_pause_aware_picking=true diff --git a/scene/demo05_scene_node/SceneNode.tscn b/scene/demo05_scene_node/SceneNode.tscn index 0e7b6eb..3a887d7 100644 --- a/scene/demo05_scene_node/SceneNode.tscn +++ b/scene/demo05_scene_node/SceneNode.tscn @@ -4,6 +4,7 @@ [ext_resource path="res://icon.png" type="Texture" id=2] [node name="Node2D" type="Node2D"] +position = Vector2( 100, 0 ) [node name="Hero" type="Sprite" parent="."] position = Vector2( 477.791, 270.893 ) diff --git a/scene/demo06_move/Move.tscn b/scene/demo06_move/Move.tscn new file mode 100644 index 0000000..3197407 --- /dev/null +++ b/scene/demo06_move/Move.tscn @@ -0,0 +1,11 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://script/demo06_move/move_hero.gd" type="Script" id=1] +[ext_resource path="res://icon.png" type="Texture" id=2] + +[node name="Node2D" type="Node2D"] + +[node name="MoveHero" type="Sprite" parent="."] +position = Vector2( 492.717, 412.746 ) +texture = ExtResource( 2 ) +script = ExtResource( 1 ) diff --git a/scene/demo_test/DemoTest.tscn b/scene/demo_test/DemoTest.tscn index c5fedd6..aefbd71 100644 --- a/scene/demo_test/DemoTest.tscn +++ b/scene/demo_test/DemoTest.tscn @@ -9,3 +9,5 @@ script = ExtResource( 1 ) [node name="icon" type="Sprite" parent="."] position = Vector2( 410.547, 444.333 ) texture = ExtResource( 2 ) + +[node name="RigidBody2D" type="RigidBody2D" parent="."] diff --git a/script/demo05_scene_node/hero.gd b/script/demo05_scene_node/hero.gd index 9ae46c2..26324dd 100644 --- a/script/demo05_scene_node/hero.gd +++ b/script/demo05_scene_node/hero.gd @@ -9,6 +9,21 @@ var spriteHeight: int func _enter_tree(): + # textureTest() + # positionTest() + pass + + +# 坐标点测试用例 +func positionTest(): + print(position) + print(global_position) + print(to_global(position)) + print(to_local(global_position)) + pass + +# 纹理测试用例 +func textureTest(): var texture = get_texture() var image = texture.get_data() print(offset) @@ -16,7 +31,7 @@ func _enter_tree(): print("height: ", texture.get_height()) print("width: ", texture.get_width()) print("Image data property: ", image.data) - + # 无法直接编辑图片 image.shrink_x2() print("new image data property",image.data) diff --git a/script/demo06_move/move_hero.gd b/script/demo06_move/move_hero.gd new file mode 100644 index 0000000..08b8735 --- /dev/null +++ b/script/demo06_move/move_hero.gd @@ -0,0 +1,31 @@ +extends Sprite + + +# How fast the player will move (pixels/sec). +export var speed = 400 +# Size of the game window. +var screen_size + + +func _ready(): + screen_size = get_viewport_rect().size + + +func _process(delta): + var velocity = Vector2.ZERO # The player's movement vector. + if Input.is_action_pressed("move_right"): + velocity.x += 1 + if Input.is_action_pressed("move_left"): + velocity.x -= 1 + if Input.is_action_pressed("move_down"): + velocity.y += 1 + if Input.is_action_pressed("move_up"): + velocity.y -= 1 + + if velocity.length() <= 0: + return + + velocity = velocity.normalized() * speed + position += velocity * delta + position.x = clamp(position.x, 0, screen_size.x) + position.y = clamp(position.y, 0, screen_size.y) diff --git a/zfoo/util/IdUtils.gd b/zfoo/util/IdUtils.gd new file mode 100644 index 0000000..a191707 --- /dev/null +++ b/zfoo/util/IdUtils.gd @@ -0,0 +1,26 @@ +extends Object + + +const atomicInt: Array = [0] + +const mutex = [false, null] + +static func getMutex() -> Mutex: + if mutex[0]: + return mutex[1] + var mutexInstance = Mutex.new() + mutex[1] = mutexInstance + return mutexInstance + + +# 获取本地int的唯一id,如果达到最大值则重新从最小值重新计算,线程安全 +static func getLocalIntId() -> int: + var mutexInstance = getMutex() + + mutexInstance.lock() + + var id = atomicInt[0] + atomicInt[0] = id + 1 + + mutexInstance.unlock() + return id