|
| 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 not label_settings: |
| 38 | + label_settings = LabelSettings.new() |
| 39 | + label_settings.font_size = 200 |
| 40 | + horizontal_alignment = HorizontalAlignment.HORIZONTAL_ALIGNMENT_CENTER |
| 41 | + vertical_alignment = VerticalAlignment.VERTICAL_ALIGNMENT_CENTER |
| 42 | + |
| 43 | + |
| 44 | +func _enter_tree(): |
| 45 | + # In the editor, show the win message so that adjusting the label |
| 46 | + # properties is visible. Otherwise, clear the text when entering the tree |
| 47 | + # so that the label text persisted in the scene file isn't shown. |
| 48 | + # |
| 49 | + # Normally this would be done in _ready, but a block script might override |
| 50 | + # that and simple_setup is too late. |
| 51 | + if Engine.is_editor_hint(): |
| 52 | + text = win_message |
| 53 | + else: |
| 54 | + text = "" |
| 55 | + |
| 56 | + |
| 57 | +func get_custom_class(): |
| 58 | + return "SimpleEnding" |
| 59 | + |
| 60 | + |
| 61 | +static func setup_custom_blocks(): |
| 62 | + var _class_name = "SimpleEnding" |
| 63 | + var block_list: Array[BlockDefinition] = [] |
| 64 | + var block_definition: BlockDefinition |
| 65 | + |
| 66 | + block_definition = BlockDefinition.new() |
| 67 | + block_definition.name = &"simpleending_game_over" |
| 68 | + block_definition.target_node_class = _class_name |
| 69 | + block_definition.category = "Lifecycle | Game" |
| 70 | + block_definition.type = Types.BlockType.STATEMENT |
| 71 | + block_definition.display_template = "game over {result: STRING}" |
| 72 | + block_definition.code_template = "game_over({result})" |
| 73 | + block_definition.defaults = { |
| 74 | + "result": OptionData.new(["WIN", "LOSE"]), |
| 75 | + } |
| 76 | + block_definition.description = "Show the game over label with the win or lose message." |
| 77 | + block_list.append(block_definition) |
| 78 | + |
| 79 | + block_definition = BlockDefinition.new() |
| 80 | + block_definition.name = &"simpleending_reset" |
| 81 | + block_definition.target_node_class = _class_name |
| 82 | + block_definition.category = "Lifecycle | Game" |
| 83 | + block_definition.type = Types.BlockType.STATEMENT |
| 84 | + block_definition.display_template = "reset game over" |
| 85 | + block_definition.code_template = "reset()" |
| 86 | + block_definition.description = "Reset the game over label." |
| 87 | + block_list.append(block_definition) |
| 88 | + |
| 89 | + BlocksCatalog.add_custom_blocks(_class_name, block_list) |
0 commit comments