Skip to content

Commit 161c2a6

Browse files
authored
Merge pull request #71 from endlessm/character-movement-rework
Character movement rework
2 parents ad90e57 + bb0f9c0 commit 161c2a6

File tree

8 files changed

+150
-132
lines changed

8 files changed

+150
-132
lines changed

addons/block_code/block_code_node/block_code.gd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,21 @@ func _update_parent_script():
4545
var script := GDScript.new()
4646
script.set_source_code(block_script.generated_script)
4747
script.reload()
48+
49+
# Persist export script variables (like SimpleCharacter exported texture)
50+
var persist_properties = {}
51+
var old_property_list = parent.get_property_list()
52+
for property in old_property_list:
53+
if property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE:
54+
persist_properties[property.name] = parent.get(property.name)
55+
4856
parent.set_script(script)
4957
parent.set_process(true)
5058

59+
# Set persisted script variables in new script
60+
for property_name in persist_properties:
61+
parent.set(property_name, persist_properties.get(property_name))
62+
5163

5264
func _get_configuration_warnings():
5365
var warnings = []

addons/block_code/examples/pong_game/paddle.gd

Lines changed: 0 additions & 33 deletions
This file was deleted.

addons/block_code/examples/pong_game/paddle.tscn

Lines changed: 0 additions & 19 deletions
This file was deleted.

addons/block_code/examples/pong_game/pong_game.tscn

Lines changed: 71 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
[gd_scene load_steps=165 format=3 uid="uid://tf7b8c64ecc0"]
1+
[gd_scene load_steps=166 format=3 uid="uid://tf7b8c64ecc0"]
22

3-
[ext_resource type="PackedScene" uid="uid://s7enbp56f256" path="res://addons/block_code/examples/pong_game/paddle.tscn" id="1_1k5k2"]
43
[ext_resource type="Script" path="res://addons/block_code/examples/pong_game/pong_game.gd" id="1_bjkc8"]
54
[ext_resource type="PackedScene" uid="uid://cg8ibi18um3vg" path="res://addons/block_code/examples/pong_game/space.tscn" id="1_y56ac"]
65
[ext_resource type="Script" path="res://addons/block_code/block_code_node/block_code.gd" id="3_6jaq8"]
6+
[ext_resource type="PackedScene" uid="uid://ddx1cd5q6t61o" path="res://addons/block_code/simple_nodes/simple_character/simple_character.tscn" id="3_hjpbs"]
77
[ext_resource type="Script" path="res://addons/block_code/ui/block_canvas/serialized_block_tree_node.gd" id="4_qtggh"]
8+
[ext_resource type="Texture2D" uid="uid://tplpgtnfeda0" path="res://addons/block_code/examples/pong_game/assets/paddle.png" id="4_ra7bh"]
89
[ext_resource type="Script" path="res://addons/block_code/ui/block_canvas/serialized_block.gd" id="5_wr38c"]
910
[ext_resource type="Script" path="res://addons/block_code/ui/block_canvas/serialized_block_tree_node_array.gd" id="6_ppdc3"]
1011
[ext_resource type="Script" path="res://addons/block_code/block_script_data/block_script_data.gd" id="7_uuuue"]
@@ -13,84 +14,108 @@
1314
[ext_resource type="PackedScene" uid="uid://1ownc3rikn2k" path="res://addons/block_code/examples/pong_game/ball_spawn_area.tscn" id="10_5vs1t"]
1415
[ext_resource type="PackedScene" uid="uid://fhoapg3anjsu" path="res://addons/block_code/examples/pong_game/goal_area.tscn" id="12_nqmxu"]
1516

16-
[sub_resource type="Resource" id="Resource_2ownr"]
17+
[sub_resource type="Resource" id="Resource_02fc8"]
1718
script = ExtResource("5_wr38c")
1819
block_class = &"StatementBlock"
19-
serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.290196, 0.52549, 0.835294, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Move with player 2 buttons, speed {speed: VECTOR2}"], ["statement", "velocity = Input.get_vector(\"player_2_left\", \"player_2_right\", \"player_2_up\", \"player_2_down\")*{speed}
20+
serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.439216, 0.501961, 0.564706, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Move with player 1 buttons, speed {speed: VECTOR2}"], ["statement", "var dir = Vector2()
21+
dir.x += float(Input.is_key_pressed(KEY_D))
22+
dir.x -= float(Input.is_key_pressed(KEY_A))
23+
dir.y += float(Input.is_key_pressed(KEY_S))
24+
dir.y -= float(Input.is_key_pressed(KEY_W))
25+
dir = dir.normalized()
26+
velocity = dir*{speed}
2027
move_and_slide()"], ["defaults", {}], ["param_input_strings", {
21-
"speed": "0, 1000"
28+
"speed": "0,1000"
2229
}]]
2330

24-
[sub_resource type="Resource" id="Resource_lt7l7"]
31+
[sub_resource type="Resource" id="Resource_hofsq"]
2532
script = ExtResource("4_qtggh")
26-
serialized_block = SubResource("Resource_2ownr")
33+
serialized_block = SubResource("Resource_02fc8")
2734
path_child_pairs = []
2835

29-
[sub_resource type="Resource" id="Resource_lp80u"]
36+
[sub_resource type="Resource" id="Resource_ijy7o"]
3037
script = ExtResource("5_wr38c")
3138
block_class = &"EntryBlock"
32-
serialized_props = [["block_name", "process_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 1], ["position", Vector2(36, 43)], ["block_format", "On Process"], ["statement", "func _process(delta):"], ["defaults", {}], ["param_input_strings", {}], ["signal_name", ""]]
39+
serialized_props = [["block_name", "process_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 1], ["position", Vector2(82, 24)], ["block_format", "On Process"], ["statement", "func _process(delta):"], ["defaults", {}], ["param_input_strings", {}], ["signal_name", ""]]
3340

34-
[sub_resource type="Resource" id="Resource_0j1nn"]
41+
[sub_resource type="Resource" id="Resource_wlov0"]
3542
script = ExtResource("4_qtggh")
36-
serialized_block = SubResource("Resource_lp80u")
37-
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_lt7l7")]]
43+
serialized_block = SubResource("Resource_ijy7o")
44+
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_hofsq")]]
3845

39-
[sub_resource type="Resource" id="Resource_t1ec3"]
46+
[sub_resource type="Resource" id="Resource_frqvp"]
4047
script = ExtResource("6_ppdc3")
41-
array = Array[ExtResource("4_qtggh")]([SubResource("Resource_0j1nn")])
48+
array = Array[ExtResource("4_qtggh")]([SubResource("Resource_wlov0")])
4249

43-
[sub_resource type="Resource" id="Resource_t7nl4"]
50+
[sub_resource type="Resource" id="Resource_qmak3"]
4451
script = ExtResource("7_uuuue")
45-
script_inherits = "Paddle"
46-
block_trees = SubResource("Resource_t1ec3")
47-
generated_script = "extends Paddle
52+
script_inherits = "SimpleCharacter"
53+
block_trees = SubResource("Resource_frqvp")
54+
generated_script = "extends SimpleCharacter
4855
4956
var VAR_DICT := {}
5057
5158
func _process(delta):
52-
velocity = Input.get_vector(\"player_2_left\", \"player_2_right\", \"player_2_up\", \"player_2_down\")*Vector2(0, 1000)
59+
var dir = Vector2()
60+
dir.x += float(Input.is_key_pressed(KEY_D))
61+
dir.x -= float(Input.is_key_pressed(KEY_A))
62+
dir.y += float(Input.is_key_pressed(KEY_S))
63+
dir.y -= float(Input.is_key_pressed(KEY_W))
64+
dir = dir.normalized()
65+
velocity = dir*Vector2(0,1000)
5366
move_and_slide()
5467
5568
"
5669

57-
[sub_resource type="Resource" id="Resource_lf6va"]
70+
[sub_resource type="Resource" id="Resource_wvsmi"]
5871
script = ExtResource("5_wr38c")
5972
block_class = &"StatementBlock"
60-
serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.290196, 0.52549, 0.835294, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Move with player 1 buttons, speed {speed: VECTOR2}"], ["statement", "velocity = Input.get_vector(\"ui_left\", \"ui_right\", \"ui_up\", \"ui_down\")*{speed}
73+
serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.439216, 0.501961, 0.564706, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Move with player 2 buttons, speed {speed: VECTOR2}"], ["statement", "var dir = Vector2()
74+
dir.x += float(Input.is_key_pressed(KEY_RIGHT))
75+
dir.x -= float(Input.is_key_pressed(KEY_LEFT))
76+
dir.y += float(Input.is_key_pressed(KEY_DOWN))
77+
dir.y -= float(Input.is_key_pressed(KEY_UP))
78+
dir = dir.normalized()
79+
velocity = dir*{speed}
6180
move_and_slide()"], ["defaults", {}], ["param_input_strings", {
62-
"speed": "0, 1000"
81+
"speed": "0,1000"
6382
}]]
6483

65-
[sub_resource type="Resource" id="Resource_5obqu"]
84+
[sub_resource type="Resource" id="Resource_v80v0"]
6685
script = ExtResource("4_qtggh")
67-
serialized_block = SubResource("Resource_lf6va")
86+
serialized_block = SubResource("Resource_wvsmi")
6887
path_child_pairs = []
6988

70-
[sub_resource type="Resource" id="Resource_hhevk"]
89+
[sub_resource type="Resource" id="Resource_3qglq"]
7190
script = ExtResource("5_wr38c")
7291
block_class = &"EntryBlock"
73-
serialized_props = [["block_name", "process_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 1], ["position", Vector2(34, 50)], ["block_format", "On Process"], ["statement", "func _process(delta):"], ["defaults", {}], ["param_input_strings", {}], ["signal_name", ""]]
92+
serialized_props = [["block_name", "process_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 1], ["position", Vector2(57, 27)], ["block_format", "On Process"], ["statement", "func _process(delta):"], ["defaults", {}], ["param_input_strings", {}], ["signal_name", ""]]
7493

75-
[sub_resource type="Resource" id="Resource_ipx1e"]
94+
[sub_resource type="Resource" id="Resource_gnrty"]
7695
script = ExtResource("4_qtggh")
77-
serialized_block = SubResource("Resource_hhevk")
78-
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_5obqu")]]
96+
serialized_block = SubResource("Resource_3qglq")
97+
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_v80v0")]]
7998

80-
[sub_resource type="Resource" id="Resource_iqrk3"]
99+
[sub_resource type="Resource" id="Resource_2qfet"]
81100
script = ExtResource("6_ppdc3")
82-
array = Array[ExtResource("4_qtggh")]([SubResource("Resource_ipx1e")])
101+
array = Array[ExtResource("4_qtggh")]([SubResource("Resource_gnrty")])
83102

84-
[sub_resource type="Resource" id="Resource_52r02"]
103+
[sub_resource type="Resource" id="Resource_lxj2y"]
85104
script = ExtResource("7_uuuue")
86-
script_inherits = "Paddle"
87-
block_trees = SubResource("Resource_iqrk3")
88-
generated_script = "extends Paddle
105+
script_inherits = "SimpleCharacter"
106+
block_trees = SubResource("Resource_2qfet")
107+
generated_script = "extends SimpleCharacter
89108
90109
var VAR_DICT := {}
91110
92111
func _process(delta):
93-
velocity = Input.get_vector(\"ui_left\", \"ui_right\", \"ui_up\", \"ui_down\")*Vector2(0, 1000)
112+
var dir = Vector2()
113+
dir.x += float(Input.is_key_pressed(KEY_RIGHT))
114+
dir.x -= float(Input.is_key_pressed(KEY_LEFT))
115+
dir.y += float(Input.is_key_pressed(KEY_DOWN))
116+
dir.y -= float(Input.is_key_pressed(KEY_UP))
117+
dir = dir.normalized()
118+
velocity = dir*Vector2(0,1000)
94119
move_and_slide()
95120
96121
"
@@ -1020,21 +1045,25 @@ script = ExtResource("1_bjkc8")
10201045

10211046
[node name="Space" parent="." instance=ExtResource("1_y56ac")]
10221047

1023-
[node name="PaddleLeft" parent="." instance=ExtResource("1_1k5k2")]
1024-
modulate = Color(0.511, 0.362, 0.972, 1)
1048+
[node name="PaddleLeft" parent="." instance=ExtResource("3_hjpbs")]
1049+
modulate = Color(0.509804, 0.360784, 0.972549, 1)
10251050
position = Vector2(64, 544)
1051+
collision_mask = 5
1052+
texture = ExtResource("4_ra7bh")
10261053

10271054
[node name="BlockCode" type="Node" parent="PaddleLeft"]
10281055
script = ExtResource("3_6jaq8")
1029-
block_script = SubResource("Resource_t7nl4")
1056+
block_script = SubResource("Resource_qmak3")
10301057

1031-
[node name="PaddleRight" parent="." instance=ExtResource("1_1k5k2")]
1032-
modulate = Color(0.511, 0.362, 0.972, 1)
1058+
[node name="PaddleRight" parent="." instance=ExtResource("3_hjpbs")]
1059+
modulate = Color(0.509804, 0.360784, 0.972549, 1)
10331060
position = Vector2(1856, 544)
1061+
collision_mask = 5
1062+
texture = ExtResource("4_ra7bh")
10341063

10351064
[node name="BlockCode" type="Node" parent="PaddleRight"]
10361065
script = ExtResource("3_6jaq8")
1037-
block_script = SubResource("Resource_52r02")
1066+
block_script = SubResource("Resource_lxj2y")
10381067

10391068
[node name="Ball" parent="." instance=ExtResource("9_xrqll")]
10401069
modulate = Color(0.511, 0.362, 0.972, 1)

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,64 @@
22
class_name SimpleCharacter
33
extends CharacterBody2D
44

5-
var sprite_texture: Texture2D = preload("res://icon.svg")
5+
@export var texture: Texture2D:
6+
set = _set_texture
7+
8+
9+
func _set_texture(new_texture):
10+
texture = new_texture
11+
12+
if not is_node_ready():
13+
return
14+
15+
$Sprite2D.texture = texture
16+
var shape = RectangleShape2D.new()
17+
shape.size = Vector2(100, 100) if texture == null else texture.get_size()
18+
$CollisionShape2D.shape = shape
619

720

821
func _ready():
9-
$Sprite2D.texture = sprite_texture
22+
_set_texture(texture)
1023

1124

1225
func get_custom_class():
1326
return "SimpleCharacter"
1427

1528

16-
static func get_exposed_properties() -> Array[String]:
17-
return ["position"]
18-
19-
2029
static func get_custom_blocks() -> Array[Block]:
2130
var b: Block
2231
var block_list: Array[Block] = []
2332

2433
# Movement
2534
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
2635
b.block_type = Types.BlockType.EXECUTE
27-
b.block_format = "Move with player 1 buttons, speed {speed: INT}"
28-
b.statement = 'velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")*{speed}\n' + "move_and_slide()"
36+
b.block_format = "Move with player 1 buttons, speed {speed: VECTOR2}"
37+
b.statement = (
38+
"var dir = Vector2()\n"
39+
+ "dir.x += float(Input.is_key_pressed(KEY_D))\n"
40+
+ "dir.x -= float(Input.is_key_pressed(KEY_A))\n"
41+
+ "dir.y += float(Input.is_key_pressed(KEY_S))\n"
42+
+ "dir.y -= float(Input.is_key_pressed(KEY_W))\n"
43+
+ "dir = dir.normalized()\n"
44+
+ "velocity = dir*{speed}\n"
45+
+ "move_and_slide()"
46+
)
2947
b.category = "Input"
3048
block_list.append(b)
3149

3250
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
3351
b.block_type = Types.BlockType.EXECUTE
34-
b.block_format = "Move with player 2 buttons, speed {speed: INT}"
35-
b.statement = 'velocity = Input.get_vector("player_2_left", "player_2_right", "player_2_up", "player_2_down")*{speed}\n' + "move_and_slide()"
52+
b.block_format = "Move with player 2 buttons, speed {speed: VECTOR2}"
53+
b.statement = (
54+
"var dir = Vector2()\n"
55+
+ "dir.x += float(Input.is_key_pressed(KEY_RIGHT))\n"
56+
+ "dir.x -= float(Input.is_key_pressed(KEY_LEFT))\n"
57+
+ "dir.y += float(Input.is_key_pressed(KEY_DOWN))\n"
58+
+ "dir.y -= float(Input.is_key_pressed(KEY_UP))\n"
59+
+ "dir = dir.normalized()\n"
60+
+ "velocity = dir*{speed}\n"
61+
+ "move_and_slide()"
62+
)
3663
b.category = "Input"
3764
block_list.append(b)
3865

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
[gd_scene load_steps=4 format=3 uid="uid://ddx1cd5q6t61o"]
1+
[gd_scene load_steps=3 format=3 uid="uid://ddx1cd5q6t61o"]
22

33
[ext_resource type="Script" path="res://addons/block_code/simple_nodes/simple_character/simple_character.gd" id="1_oqwef"]
4-
[ext_resource type="Texture2D" uid="uid://dr8e0tvfxjy1f" path="res://icon.svg" id="2_vn8k5"]
54

6-
[sub_resource type="CircleShape2D" id="CircleShape2D_dfyyg"]
5+
[sub_resource type="RectangleShape2D" id="RectangleShape2D_104qs"]
6+
size = Vector2(16, 16)
77

88
[node name="SimpleCharacter" type="CharacterBody2D"]
99
script = ExtResource("1_oqwef")
1010

1111
[node name="Sprite2D" type="Sprite2D" parent="."]
12-
scale = Vector2(0.16, 0.16)
13-
texture = ExtResource("2_vn8k5")
1412

1513
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
16-
shape = SubResource("CircleShape2D_dfyyg")
14+
shape = SubResource("RectangleShape2D_104qs")

addons/block_code/ui/picker/categories/category_factory.gd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,29 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:
478478
b.category = "Signal"
479479
block_list.append(b)
480480

481+
"CharacterBody2D":
482+
var b = BLOCKS["statement_block"].instantiate()
483+
b.block_type = Types.BlockType.EXECUTE
484+
b.block_format = "Move with keys {up: STRING} {down: STRING} {left: STRING} {right: STRING} with speed {speed: VECTOR2}"
485+
b.statement = (
486+
"var dir = Vector2()\n"
487+
+ "dir.x += float(Input.is_key_pressed(OS.find_keycode_from_string({right})))\n"
488+
+ "dir.x -= float(Input.is_key_pressed(OS.find_keycode_from_string({left})))\n"
489+
+ "dir.y += float(Input.is_key_pressed(OS.find_keycode_from_string({down})))\n"
490+
+ "dir.y -= float(Input.is_key_pressed(OS.find_keycode_from_string({up})))\n"
491+
+ "dir = dir.normalized()\n"
492+
+ "velocity = dir*{speed}\n"
493+
+ "move_and_slide()"
494+
)
495+
b.defaults = {
496+
"up": "W",
497+
"down": "S",
498+
"left": "A",
499+
"right": "D",
500+
}
501+
b.category = "Input"
502+
block_list.append(b)
503+
481504
var prop_list = ClassDB.class_get_property_list(_class_name, true)
482505
block_list.append_array(blocks_from_property_list(prop_list, props))
483506

0 commit comments

Comments
 (0)