Skip to content

Commit fe2a64e

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. Label is inherited directly so that all the label properties are available in the inspector. https://phabricator.endlessm.com/T35661
1 parent 1a0cd11 commit fe2a64e

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
@tool
2+
class_name SimpleEnding
3+
extends Label
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+
## The message that will be shown when the player wins the game.
11+
@export var win_message: String = "YOU WIN!"
12+
13+
## The message that will be shown when the player loses the game.
14+
@export var lose_message: String = "GAME OVER"
15+
16+
17+
func game_over(result: String):
18+
match result:
19+
"WIN":
20+
text = win_message
21+
"LOSE":
22+
text = lose_message
23+
_:
24+
text = ""
25+
push_warning('Unrecognized game result "%s"' % result)
26+
27+
28+
func reset():
29+
text = ""
30+
31+
32+
func _ready():
33+
simple_setup()
34+
35+
36+
func simple_setup():
37+
if Engine.is_editor_hint():
38+
# Show the win message in the editor so adjusting the label properties
39+
# is visible.
40+
text = win_message
41+
else:
42+
text = ""
43+
add_theme_font_size_override("font_size", 200)
44+
horizontal_alignment = HorizontalAlignment.HORIZONTAL_ALIGNMENT_CENTER
45+
vertical_alignment = VerticalAlignment.VERTICAL_ALIGNMENT_CENTER
46+
47+
48+
func get_custom_class():
49+
return "SimpleEnding"
50+
51+
52+
static func setup_custom_blocks():
53+
var _class_name = "SimpleEnding"
54+
var block_list: Array[BlockDefinition] = []
55+
var block_definition: BlockDefinition
56+
57+
block_definition = BlockDefinition.new()
58+
block_definition.name = &"simpleending_game_over"
59+
block_definition.target_node_class = _class_name
60+
block_definition.category = "Lifecycle | Game"
61+
block_definition.type = Types.BlockType.STATEMENT
62+
block_definition.display_template = "game over {result: STRING}"
63+
block_definition.code_template = "game_over({result})"
64+
block_definition.defaults = {
65+
"result": OptionData.new(["WIN", "LOSE"]),
66+
}
67+
block_definition.description = "Show the game over label with the win or lose message."
68+
block_list.append(block_definition)
69+
70+
block_definition = BlockDefinition.new()
71+
block_definition.name = &"simpleending_reset"
72+
block_definition.target_node_class = _class_name
73+
block_definition.category = "Lifecycle | Game"
74+
block_definition.type = Types.BlockType.STATEMENT
75+
block_definition.display_template = "reset game over"
76+
block_definition.code_template = "reset()"
77+
block_definition.description = "Reset the game over label."
78+
block_list.append(block_definition)
79+
80+
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)