-
Notifications
You must be signed in to change notification settings - Fork 32
simple_nodes: Add SimpleEnding node #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
addons/block_code/simple_nodes/simple_ending/simple_ending.gd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| @tool | ||
| class_name SimpleEnding | ||
| extends Label | ||
|
|
||
| const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd") | ||
| const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_catalog.gd") | ||
| const OptionData = preload("res://addons/block_code/code_generation/option_data.gd") | ||
| const Types = preload("res://addons/block_code/types/types.gd") | ||
|
|
||
| ## The message that will be shown when the player wins the game. | ||
| @export var win_message: String = "YOU WIN!" | ||
|
|
||
| ## The message that will be shown when the player loses the game. | ||
| @export var lose_message: String = "GAME OVER" | ||
|
|
||
|
|
||
| func game_over(result: String): | ||
| match result: | ||
| "WIN": | ||
| text = win_message | ||
| "LOSE": | ||
| text = lose_message | ||
| _: | ||
| text = "" | ||
| push_warning('Unrecognized game result "%s"' % result) | ||
|
|
||
|
|
||
| func reset(): | ||
| text = "" | ||
|
|
||
|
|
||
| func _ready(): | ||
| simple_setup() | ||
|
|
||
|
|
||
| func simple_setup(): | ||
| if not label_settings: | ||
| label_settings = LabelSettings.new() | ||
| label_settings.font_size = 200 | ||
| horizontal_alignment = HorizontalAlignment.HORIZONTAL_ALIGNMENT_CENTER | ||
| vertical_alignment = VerticalAlignment.VERTICAL_ALIGNMENT_CENTER | ||
|
|
||
|
|
||
| func _enter_tree(): | ||
| # In the editor, show the win message so that adjusting the label | ||
| # properties is visible. Otherwise, clear the text when entering the tree | ||
| # so that the label text persisted in the scene file isn't shown. | ||
| # | ||
| # Normally this would be done in _ready, but a block script might override | ||
| # that and simple_setup is too late. | ||
| if Engine.is_editor_hint(): | ||
| text = win_message | ||
| else: | ||
| text = "" | ||
|
|
||
|
|
||
| func get_custom_class(): | ||
| return "SimpleEnding" | ||
|
|
||
|
|
||
| static func setup_custom_blocks(): | ||
| var _class_name = "SimpleEnding" | ||
| var block_list: Array[BlockDefinition] = [] | ||
| var block_definition: BlockDefinition | ||
|
|
||
| block_definition = BlockDefinition.new() | ||
| block_definition.name = &"simpleending_game_over" | ||
| block_definition.target_node_class = _class_name | ||
| block_definition.category = "Lifecycle | Game" | ||
| block_definition.type = Types.BlockType.STATEMENT | ||
| block_definition.display_template = "game over {result: STRING}" | ||
| block_definition.code_template = "game_over({result})" | ||
| block_definition.defaults = { | ||
| "result": OptionData.new(["WIN", "LOSE"]), | ||
| } | ||
| block_definition.description = "Show the game over label with the win or lose message." | ||
| block_list.append(block_definition) | ||
|
|
||
| block_definition = BlockDefinition.new() | ||
| block_definition.name = &"simpleending_reset" | ||
| block_definition.target_node_class = _class_name | ||
| block_definition.category = "Lifecycle | Game" | ||
| block_definition.type = Types.BlockType.STATEMENT | ||
| block_definition.display_template = "reset game over" | ||
| block_definition.code_template = "reset()" | ||
| block_definition.description = "Reset the game over label." | ||
| block_list.append(block_definition) | ||
|
|
||
| BlocksCatalog.add_custom_blocks(_class_name, block_list) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: this makes it not work when starting, even adding the "wait for scene to be ready" in the middle:

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is broken if you try to run it from
_readysincesimple_setupis run after the node is ready. The problem this is trying to address is that if you set the label text in the editor, it will be serialized to the scene and restored when loading. I tried to figure out how to clear the label text when saving so that it didn't get serialized, but I couldn't figure it out.I think I'm just going to change this so that SimpleEnding descends from Control and the Label is created at runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated now so the Label isn't saved to the scene. Since the Label is created in
simple_setupand that's not run until the scene becomes idle, I had to add a signal and await it to make the blocks work from thewhen startingblock.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I really liked it when it was inheriting Label, because it allowed the user to change the font, text color, etc. I was even going to propose this inheritance for SimpleScoring. I wasn't expecting a reimplementation like this for that little nit observation :) . Can you go back to it? Is actually unlikely that someone would like to set "you win" or "you lose" when starting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A LabelSettings instance is still exposed in this case, but I can try to go back.
Possibly what I can do is toggle the node visibility from
enter/exit_treewhen not in editor mode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it back to a Label but moved the hack to clear the text to
_enter_treewhen not in the editor. That seems to work well enough to use the game over blocks from "when starting".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent!