Skip to content

Commit 7258186

Browse files
committed
Refactor simple nodes to generate BlockDefinitions
1 parent 8784f61 commit 7258186

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extends CharacterBody2D
44

55
const CategoryFactory = preload("res://addons/block_code/ui/picker/categories/category_factory.gd")
66
const Types = preload("res://addons/block_code/types/types.gd")
7+
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
78

89
@export var texture: Texture2D:
910
set = _set_texture
@@ -111,35 +112,35 @@ func move_with_player_buttons(player: String, kind: String, delta: float):
111112
move_and_slide()
112113

113114

114-
static func get_custom_blocks() -> Array[Block]:
115-
var b: Block
116-
var block_list: Array[Block] = []
115+
static func get_custom_blocks() -> Array[BlockDefinition]:
116+
var bd: BlockDefinition
117+
var block_definition_list: Array[BlockDefinition] = []
117118

118119
# Movement
119-
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
120-
b.block_name = "simplecharacter_move"
121-
b.block_type = Types.BlockType.STATEMENT
122-
b.block_format = "Move with {player: OPTION} buttons as {kind: OPTION}"
120+
bd = BlockDefinition.new()
121+
bd.name = "simplecharacter_move"
122+
bd.type = Types.BlockType.STATEMENT
123+
bd.category = "Input"
124+
bd.display_template = "Move with {player: OPTION} buttons as {kind: OPTION}"
125+
123126
# TODO: delta here is assumed to be the parameter name of
124127
# the _process or _physics_process method:
125-
b.statement = 'move_with_player_buttons("{player}", "{kind}", delta)'
126-
b.defaults = {
128+
bd.code_template = 'move_with_player_buttons("{player}", "{kind}", delta)'
129+
bd.defaults = {
127130
"player": OptionData.new(["player_1", "player_2"]),
128131
"kind": OptionData.new(["top-down", "platformer", "spaceship"]),
129132
}
130-
b.category = "Input"
131-
block_list.append(b)
132-
133-
var property_blocks = (
134-
CategoryFactory
135-
. property_to_blocklist(
136-
{
137-
"name": "speed",
138-
"type": TYPE_VECTOR2,
139-
"category": "Physics | Velocity",
140-
}
141-
)
142-
)
143-
block_list.append_array(property_blocks)
144-
145-
return block_list
133+
block_definition_list.append(bd)
134+
135+
var props = [
136+
{
137+
"name": "speed",
138+
"type": TYPE_VECTOR2,
139+
"category": "Physics | Velocity",
140+
}
141+
]
142+
for prop in props:
143+
var property_blocks = CategoryFactory.property_to_blocklist(prop)
144+
block_definition_list.append_array(property_blocks)
145+
146+
return block_definition_list

addons/block_code/simple_nodes/simple_scoring/simple_scoring.gd

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extends CanvasLayer
44

55
const CategoryFactory = preload("res://addons/block_code/ui/picker/categories/category_factory.gd")
66
const Types = preload("res://addons/block_code/types/types.gd")
7+
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
78

89
@export var score_left: int:
910
set = _set_score_left
@@ -63,25 +64,25 @@ func set_player_score(player: String, score: int):
6364
_score_labels[player].text = str(score)
6465

6566

66-
static func get_custom_blocks() -> Array[Block]:
67-
var b: Block
68-
var block_list: Array[Block] = []
67+
static func get_custom_blocks() -> Array[BlockDefinition]:
68+
var bd: BlockDefinition
69+
var block_definition_list: Array[BlockDefinition] = []
6970

7071
for player in _POSITIONS_FOR_PLAYER:
71-
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
72-
b.block_name = "simplescoring_set_score"
73-
b.block_type = Types.BlockType.STATEMENT
74-
b.block_format = "Set player %s score to {score: INT}" % player
75-
b.statement = "score_%s = {score}" % _POSITIONS_FOR_PLAYER[player]
76-
b.category = "Info | Score"
77-
block_list.append(b)
78-
79-
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
80-
b.block_name = "simplescoring_change_score"
81-
b.block_type = Types.BlockType.STATEMENT
82-
b.block_format = "Change player %s score by {score: INT}" % player
83-
b.statement = "score_%s += {score}" % _POSITIONS_FOR_PLAYER[player]
84-
b.category = "Info | Score"
85-
block_list.append(b)
86-
87-
return block_list
72+
bd = BlockDefinition.new()
73+
bd.name = "simplescoring_set_score_%s" % player
74+
bd.category = "Info | Score"
75+
bd.type = Types.BlockType.STATEMENT
76+
bd.display_template = "Set player %s score to {score: INT}" % player
77+
bd.code_template = "score_%s = {score}" % _POSITIONS_FOR_PLAYER[player]
78+
block_definition_list.append(bd)
79+
80+
bd = BlockDefinition.new()
81+
bd.name = "simplescoring_change_score_%s" % player
82+
bd.category = "Info | Score"
83+
bd.type = Types.BlockType.STATEMENT
84+
bd.display_template = "Change player %s score by {score: INT}" % player
85+
bd.code_template = "score_%s += {score}" % _POSITIONS_FOR_PLAYER[player]
86+
block_definition_list.append(bd)
87+
88+
return block_definition_list

0 commit comments

Comments
 (0)