Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Character movement rework #71

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions addons/block_code/block_code_node/block_code.gd
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,21 @@ func _update_parent_script():
var script := GDScript.new()
script.set_source_code(block_script.generated_script)
script.reload()

# Persist export script variables (like SimpleCharacter exported texture)
var persist_properties = {}
var old_property_list = parent.get_property_list()
for property in old_property_list:
if property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE:
persist_properties[property.name] = parent.get(property.name)

parent.set_script(script)
parent.set_process(true)

# Set persisted script variables in new script
for property_name in persist_properties:
parent.set(property_name, persist_properties.get(property_name))


func _get_configuration_warnings():
var warnings = []
Expand Down
33 changes: 0 additions & 33 deletions addons/block_code/examples/pong_game/paddle.gd

This file was deleted.

19 changes: 0 additions & 19 deletions addons/block_code/examples/pong_game/paddle.tscn

This file was deleted.

113 changes: 71 additions & 42 deletions addons/block_code/examples/pong_game/pong_game.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[gd_scene load_steps=165 format=3 uid="uid://tf7b8c64ecc0"]
[gd_scene load_steps=166 format=3 uid="uid://tf7b8c64ecc0"]

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

[sub_resource type="Resource" id="Resource_2ownr"]
[sub_resource type="Resource" id="Resource_02fc8"]
script = ExtResource("5_wr38c")
block_class = &"StatementBlock"
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}
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()
dir.x += float(Input.is_key_pressed(KEY_D))
dir.x -= float(Input.is_key_pressed(KEY_A))
dir.y += float(Input.is_key_pressed(KEY_S))
dir.y -= float(Input.is_key_pressed(KEY_W))
dir = dir.normalized()
velocity = dir*{speed}
move_and_slide()"], ["defaults", {}], ["param_input_strings", {
"speed": "0, 1000"
"speed": "0,1000"
}]]

[sub_resource type="Resource" id="Resource_lt7l7"]
[sub_resource type="Resource" id="Resource_hofsq"]
script = ExtResource("4_qtggh")
serialized_block = SubResource("Resource_2ownr")
serialized_block = SubResource("Resource_02fc8")
path_child_pairs = []

[sub_resource type="Resource" id="Resource_lp80u"]
[sub_resource type="Resource" id="Resource_ijy7o"]
script = ExtResource("5_wr38c")
block_class = &"EntryBlock"
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", ""]]
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", ""]]

[sub_resource type="Resource" id="Resource_0j1nn"]
[sub_resource type="Resource" id="Resource_wlov0"]
script = ExtResource("4_qtggh")
serialized_block = SubResource("Resource_lp80u")
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_lt7l7")]]
serialized_block = SubResource("Resource_ijy7o")
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_hofsq")]]

[sub_resource type="Resource" id="Resource_t1ec3"]
[sub_resource type="Resource" id="Resource_frqvp"]
script = ExtResource("6_ppdc3")
array = Array[ExtResource("4_qtggh")]([SubResource("Resource_0j1nn")])
array = Array[ExtResource("4_qtggh")]([SubResource("Resource_wlov0")])

[sub_resource type="Resource" id="Resource_t7nl4"]
[sub_resource type="Resource" id="Resource_qmak3"]
script = ExtResource("7_uuuue")
script_inherits = "Paddle"
block_trees = SubResource("Resource_t1ec3")
generated_script = "extends Paddle
script_inherits = "SimpleCharacter"
block_trees = SubResource("Resource_frqvp")
generated_script = "extends SimpleCharacter

var VAR_DICT := {}

func _process(delta):
velocity = Input.get_vector(\"player_2_left\", \"player_2_right\", \"player_2_up\", \"player_2_down\")*Vector2(0, 1000)
var dir = Vector2()
dir.x += float(Input.is_key_pressed(KEY_D))
dir.x -= float(Input.is_key_pressed(KEY_A))
dir.y += float(Input.is_key_pressed(KEY_S))
dir.y -= float(Input.is_key_pressed(KEY_W))
dir = dir.normalized()
velocity = dir*Vector2(0,1000)
move_and_slide()

"

[sub_resource type="Resource" id="Resource_lf6va"]
[sub_resource type="Resource" id="Resource_wvsmi"]
script = ExtResource("5_wr38c")
block_class = &"StatementBlock"
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}
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()
dir.x += float(Input.is_key_pressed(KEY_RIGHT))
dir.x -= float(Input.is_key_pressed(KEY_LEFT))
dir.y += float(Input.is_key_pressed(KEY_DOWN))
dir.y -= float(Input.is_key_pressed(KEY_UP))
dir = dir.normalized()
velocity = dir*{speed}
move_and_slide()"], ["defaults", {}], ["param_input_strings", {
"speed": "0, 1000"
"speed": "0,1000"
}]]

[sub_resource type="Resource" id="Resource_5obqu"]
[sub_resource type="Resource" id="Resource_v80v0"]
script = ExtResource("4_qtggh")
serialized_block = SubResource("Resource_lf6va")
serialized_block = SubResource("Resource_wvsmi")
path_child_pairs = []

[sub_resource type="Resource" id="Resource_hhevk"]
[sub_resource type="Resource" id="Resource_3qglq"]
script = ExtResource("5_wr38c")
block_class = &"EntryBlock"
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", ""]]
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", ""]]

[sub_resource type="Resource" id="Resource_ipx1e"]
[sub_resource type="Resource" id="Resource_gnrty"]
script = ExtResource("4_qtggh")
serialized_block = SubResource("Resource_hhevk")
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_5obqu")]]
serialized_block = SubResource("Resource_3qglq")
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_v80v0")]]

[sub_resource type="Resource" id="Resource_iqrk3"]
[sub_resource type="Resource" id="Resource_2qfet"]
script = ExtResource("6_ppdc3")
array = Array[ExtResource("4_qtggh")]([SubResource("Resource_ipx1e")])
array = Array[ExtResource("4_qtggh")]([SubResource("Resource_gnrty")])

[sub_resource type="Resource" id="Resource_52r02"]
[sub_resource type="Resource" id="Resource_lxj2y"]
script = ExtResource("7_uuuue")
script_inherits = "Paddle"
block_trees = SubResource("Resource_iqrk3")
generated_script = "extends Paddle
script_inherits = "SimpleCharacter"
block_trees = SubResource("Resource_2qfet")
generated_script = "extends SimpleCharacter

var VAR_DICT := {}

func _process(delta):
velocity = Input.get_vector(\"ui_left\", \"ui_right\", \"ui_up\", \"ui_down\")*Vector2(0, 1000)
var dir = Vector2()
dir.x += float(Input.is_key_pressed(KEY_RIGHT))
dir.x -= float(Input.is_key_pressed(KEY_LEFT))
dir.y += float(Input.is_key_pressed(KEY_DOWN))
dir.y -= float(Input.is_key_pressed(KEY_UP))
dir = dir.normalized()
velocity = dir*Vector2(0,1000)
move_and_slide()

"
Expand Down Expand Up @@ -1020,21 +1045,25 @@ script = ExtResource("1_bjkc8")

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

[node name="PaddleLeft" parent="." instance=ExtResource("1_1k5k2")]
modulate = Color(0.511, 0.362, 0.972, 1)
[node name="PaddleLeft" parent="." instance=ExtResource("3_hjpbs")]
modulate = Color(0.509804, 0.360784, 0.972549, 1)
position = Vector2(64, 544)
collision_mask = 5
texture = ExtResource("4_ra7bh")

[node name="BlockCode" type="Node" parent="PaddleLeft"]
script = ExtResource("3_6jaq8")
block_script = SubResource("Resource_t7nl4")
block_script = SubResource("Resource_qmak3")

[node name="PaddleRight" parent="." instance=ExtResource("1_1k5k2")]
modulate = Color(0.511, 0.362, 0.972, 1)
[node name="PaddleRight" parent="." instance=ExtResource("3_hjpbs")]
modulate = Color(0.509804, 0.360784, 0.972549, 1)
position = Vector2(1856, 544)
collision_mask = 5
texture = ExtResource("4_ra7bh")

[node name="BlockCode" type="Node" parent="PaddleRight"]
script = ExtResource("3_6jaq8")
block_script = SubResource("Resource_52r02")
block_script = SubResource("Resource_lxj2y")

[node name="Ball" parent="." instance=ExtResource("9_xrqll")]
modulate = Color(0.511, 0.362, 0.972, 1)
Expand Down
47 changes: 37 additions & 10 deletions addons/block_code/simple_nodes/simple_character/simple_character.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,64 @@
class_name SimpleCharacter
extends CharacterBody2D

var sprite_texture: Texture2D = preload("res://icon.svg")
@export var texture: Texture2D:
set = _set_texture


func _set_texture(new_texture):
texture = new_texture

if not is_node_ready():
return

$Sprite2D.texture = texture
var shape = RectangleShape2D.new()
shape.size = Vector2(100, 100) if texture == null else texture.get_size()
$CollisionShape2D.shape = shape


func _ready():
$Sprite2D.texture = sprite_texture
_set_texture(texture)


func get_custom_class():
return "SimpleCharacter"


static func get_exposed_properties() -> Array[String]:
return ["position"]


static func get_custom_blocks() -> Array[Block]:
var b: Block
var block_list: Array[Block] = []

# Movement
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Move with player 1 buttons, speed {speed: INT}"
b.statement = 'velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")*{speed}\n' + "move_and_slide()"
b.block_format = "Move with player 1 buttons, speed {speed: VECTOR2}"
b.statement = (
"var dir = Vector2()\n"
+ "dir.x += float(Input.is_key_pressed(KEY_D))\n"
+ "dir.x -= float(Input.is_key_pressed(KEY_A))\n"
+ "dir.y += float(Input.is_key_pressed(KEY_S))\n"
+ "dir.y -= float(Input.is_key_pressed(KEY_W))\n"
+ "dir = dir.normalized()\n"
+ "velocity = dir*{speed}\n"
+ "move_and_slide()"
)
b.category = "Input"
block_list.append(b)

b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Move with player 2 buttons, speed {speed: INT}"
b.statement = 'velocity = Input.get_vector("player_2_left", "player_2_right", "player_2_up", "player_2_down")*{speed}\n' + "move_and_slide()"
b.block_format = "Move with player 2 buttons, speed {speed: VECTOR2}"
b.statement = (
"var dir = Vector2()\n"
+ "dir.x += float(Input.is_key_pressed(KEY_RIGHT))\n"
+ "dir.x -= float(Input.is_key_pressed(KEY_LEFT))\n"
+ "dir.y += float(Input.is_key_pressed(KEY_DOWN))\n"
+ "dir.y -= float(Input.is_key_pressed(KEY_UP))\n"
+ "dir = dir.normalized()\n"
+ "velocity = dir*{speed}\n"
+ "move_and_slide()"
)
b.category = "Input"
block_list.append(b)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
[gd_scene load_steps=4 format=3 uid="uid://ddx1cd5q6t61o"]
[gd_scene load_steps=3 format=3 uid="uid://ddx1cd5q6t61o"]

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

[sub_resource type="CircleShape2D" id="CircleShape2D_dfyyg"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_104qs"]
size = Vector2(16, 16)

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

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

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_dfyyg")
shape = SubResource("RectangleShape2D_104qs")
23 changes: 23 additions & 0 deletions addons/block_code/ui/picker/categories/category_factory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,29 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:
b.category = "Signal"
block_list.append(b)

"CharacterBody2D":
var b = BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Move with keys {up: STRING} {down: STRING} {left: STRING} {right: STRING} with speed {speed: VECTOR2}"
b.statement = (
"var dir = Vector2()\n"
+ "dir.x += float(Input.is_key_pressed(OS.find_keycode_from_string({right})))\n"
+ "dir.x -= float(Input.is_key_pressed(OS.find_keycode_from_string({left})))\n"
+ "dir.y += float(Input.is_key_pressed(OS.find_keycode_from_string({down})))\n"
+ "dir.y -= float(Input.is_key_pressed(OS.find_keycode_from_string({up})))\n"
+ "dir = dir.normalized()\n"
+ "velocity = dir*{speed}\n"
+ "move_and_slide()"
)
b.defaults = {
"up": "W",
"down": "S",
"left": "A",
"right": "D",
}
b.category = "Input"
block_list.append(b)

var prop_list = ClassDB.class_get_property_list(_class_name, true)
block_list.append_array(blocks_from_property_list(prop_list, props))

Expand Down
Loading
Loading