|
| 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) |
0 commit comments