Skip to content

Commit 7441600

Browse files
committed
simple_nodes: Add SimpleEnding node
This provides a label that displays either a win or lose message along with blocks to set or clear the label during the game. A LabelSettings instance is provided to easily customize the text settings for the label. Since the Label isn't created until simple_setup and that runs after _ready, a signal is used to defer the game_over and reset methods until the label exists. Otherwise, using the blocks from a "when starting" block would crash. https://phabricator.endlessm.com/T35661
1 parent 1a0cd11 commit 7441600

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
@tool
2+
class_name SimpleEnding
3+
extends Control
4+
5+
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
6+
const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_catalog.gd")
7+
const OptionData = preload("res://addons/block_code/code_generation/option_data.gd")
8+
const Types = preload("res://addons/block_code/types/types.gd")
9+
10+
## Emitted when SimpleEnding has completed setup.
11+
signal setup
12+
13+
## The message that will be shown when the player wins the game.
14+
@export var win_message: String = "YOU WIN!"
15+
16+
## The message that will be shown when the player loses the game.
17+
@export var lose_message: String = "GAME OVER"
18+
19+
## Text settings for the message label.
20+
@export var label_settings: LabelSettings
21+
22+
var _label: Label
23+
24+
25+
func game_over(result: String):
26+
# Wait until simple_setup completes so the label exists.
27+
await setup
28+
29+
match result:
30+
"WIN":
31+
_label.text = win_message
32+
"LOSE":
33+
_label.text = lose_message
34+
_:
35+
_label.text = ""
36+
push_warning('Unrecognized game result "%s"' % result)
37+
38+
39+
func reset():
40+
# Wait until simple_setup completes so the label exists.
41+
await setup
42+
43+
_label.text = ""
44+
45+
46+
func _create_label_settings():
47+
if label_settings:
48+
return
49+
50+
label_settings = LabelSettings.new()
51+
label_settings.font_size = 200
52+
53+
54+
func _create_label():
55+
if _label:
56+
return
57+
58+
_label = Label.new()
59+
_label.label_settings = label_settings
60+
_label.horizontal_alignment = HorizontalAlignment.HORIZONTAL_ALIGNMENT_CENTER
61+
_label.vertical_alignment = VerticalAlignment.VERTICAL_ALIGNMENT_CENTER
62+
63+
add_child(_label)
64+
65+
66+
func _ready():
67+
simple_setup()
68+
69+
70+
func simple_setup():
71+
_create_label_settings()
72+
_create_label()
73+
74+
if Engine.is_editor_hint():
75+
# Show the win message in the editor so adjusting the label settings
76+
# is visible.
77+
_label.text = win_message
78+
79+
setup.emit()
80+
81+
82+
func get_custom_class():
83+
return "SimpleEnding"
84+
85+
86+
static func setup_custom_blocks():
87+
var _class_name = "SimpleEnding"
88+
var block_list: Array[BlockDefinition] = []
89+
var block_definition: BlockDefinition
90+
91+
block_definition = BlockDefinition.new()
92+
block_definition.name = &"simpleending_game_over"
93+
block_definition.target_node_class = _class_name
94+
block_definition.category = "Lifecycle | Game"
95+
block_definition.type = Types.BlockType.STATEMENT
96+
block_definition.display_template = "game over {result: STRING}"
97+
block_definition.code_template = "game_over({result})"
98+
block_definition.defaults = {
99+
"result": OptionData.new(["WIN", "LOSE"]),
100+
}
101+
block_definition.description = "Show the game over label with the win or lose message."
102+
block_list.append(block_definition)
103+
104+
block_definition = BlockDefinition.new()
105+
block_definition.name = &"simpleending_reset"
106+
block_definition.target_node_class = _class_name
107+
block_definition.category = "Lifecycle | Game"
108+
block_definition.type = Types.BlockType.STATEMENT
109+
block_definition.display_template = "reset game over"
110+
block_definition.code_template = "reset()"
111+
block_definition.description = "Reset the game over label."
112+
block_list.append(block_definition)
113+
114+
BlocksCatalog.add_custom_blocks(_class_name, block_list)

addons/block_code/ui/constants.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const BUILTIN_CATEGORIES_PROPS: Dictionary = {
2222
"color": Color("ec3b59"),
2323
"order": 10,
2424
},
25+
"Lifecycle | Game":
26+
{
27+
"color": Color("ec3b59"),
28+
"order": 12,
29+
},
2530
"Lifecycle | Spawn":
2631
{
2732
"color": Color("ec3b59"),

0 commit comments

Comments
 (0)