diff --git a/src/Main.gd b/src/Main.gd index e48cdcb..faa4672 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -16,6 +16,7 @@ var letters := [] var target_word: String var current_guess: int var ended: bool +var input_guess: String onready var error_text_color_default = $C/V/V/ErrorText.get("custom_colors/font_color") @@ -45,27 +46,53 @@ func _ready() -> void: current_guess = 0 ended = false - $C/V/V/H/GuessText.grab_focus() + +func type_letter(letter: String) -> void: + if ended or input_guess.length() >= 5: + return + var index := input_guess.length() + letter = letter[0] + input_guess += letter + var letter_instance := letters[current_guess][index] as ColorRect + letter_instance.get_node("Label").text = letter + + +func backspace() -> void: + if ended or input_guess.length() <= 0: + return + input_guess = input_guess.substr(0, input_guess.length() - 1) + var index := input_guess.length() + var letter_instance := letters[current_guess][index] as ColorRect + letter_instance.get_node("Label").text = "" + + +func _unhandled_key_input(event: InputEventKey) -> void: + if event.is_pressed(): + var scancode = event.get_scancode() + if scancode >= KEY_A and scancode <= KEY_Z: + var letter := OS.get_scancode_string(scancode) + type_letter(letter) + get_tree().set_input_as_handled() + elif scancode == KEY_BACKSPACE: + backspace() + get_tree().set_input_as_handled() + elif scancode == KEY_ENTER: + guess_entered() + get_tree().set_input_as_handled() func _on_GuessButton_pressed() -> void: - var guess_text_node := find_node("GuessText") as LineEdit - var guess := guess_text_node.text.to_upper() - guess_entered(guess) + guess_entered() -func _on_GuessText_text_entered(new_text: String) -> void: - guess_entered(new_text) - - -func guess_entered(guess: String) -> void: +func guess_entered() -> void: if current_guess >= guess_count: return - guess = guess.to_upper() - if guess.length() != letter_count: + input_guess = input_guess.to_upper() + if input_guess.length() != letter_count: show_error("Word must be five characters.") return - if not Global.is_valid_word(guess): + if not Global.is_valid_word(input_guess): show_error("Not a recognized word.") return @@ -77,7 +104,7 @@ func guess_entered(guess: String) -> void: # Mark all greens for i in range(letter_count): - var guess_letter := guess[i] + var guess_letter := input_guess[i] var target_letter := target_word[i] var letter_instance := letters[current_guess][i] as ColorRect @@ -96,7 +123,7 @@ func guess_entered(guess: String) -> void: if letter_instance.color == color_correct: # Ignore things that are already green continue - var guess_letter := guess[i] + var guess_letter := input_guess[i] var found := false for j in range(letters_remaining.size()): if letters_remaining[j] == guess_letter: @@ -109,20 +136,19 @@ func guess_entered(guess: String) -> void: letter_instance.color = color_incorrect current_guess += 1 - var guess_text_node := find_node("GuessText") as LineEdit - guess_text_node.text = "" var won := false - if guess == target_word: + if input_guess == target_word: ended = true won = true elif current_guess >= guess_count: ended = true + input_guess = "" + if ended: find_node("GuessButton").disabled = true - find_node("GuessText").editable = false if won: show_error("Congrats! You won!", Color(0.4, 0.9, 0.6)) else: diff --git a/src/Main.tscn b/src/Main.tscn index 41707e8..7999b26 100644 --- a/src/Main.tscn +++ b/src/Main.tscn @@ -49,26 +49,26 @@ __meta__ = { } [node name="V" type="VBoxContainer" parent="C"] -margin_left = 70.0 -margin_top = 377.0 -margin_right = 570.0 -margin_bottom = 582.0 +margin_left = 209.0 +margin_top = 379.0 +margin_right = 430.0 +margin_bottom = 580.0 custom_constants/separation = 20 __meta__ = { "_edit_use_anchors_": false } [node name="Title" type="Label" parent="C/V"] -margin_right = 500.0 +margin_right = 221.0 margin_bottom = 82.0 custom_fonts/font = SubResource( 1 ) text = "Gordle" align = 1 [node name="LetterGrid" type="GridContainer" parent="C/V"] -margin_left = 250.0 +margin_left = 110.0 margin_top = 102.0 -margin_right = 250.0 +margin_right = 110.0 margin_bottom = 102.0 grow_horizontal = 2 size_flags_horizontal = 4 @@ -78,36 +78,22 @@ columns = 5 [node name="V" type="VBoxContainer" parent="C/V"] margin_top = 122.0 -margin_right = 500.0 -margin_bottom = 205.0 +margin_right = 221.0 +margin_bottom = 201.0 [node name="ErrorText" type="Label" parent="C/V/V"] -margin_right = 500.0 +margin_right = 221.0 margin_bottom = 28.0 custom_colors/font_color = Color( 1, 0.31, 0.31, 1 ) custom_fonts/font = SubResource( 2 ) align = 1 -[node name="H" type="HBoxContainer" parent="C/V/V"] +[node name="GuessButton" type="Button" parent="C/V/V"] +margin_left = 53.0 margin_top = 32.0 -margin_right = 500.0 -margin_bottom = 83.0 -rect_min_size = Vector2( 500, 0 ) +margin_right = 167.0 +margin_bottom = 79.0 size_flags_horizontal = 4 -custom_constants/separation = 12 - -[node name="GuessText" type="LineEdit" parent="C/V/V/H"] -margin_right = 374.0 -margin_bottom = 51.0 -size_flags_horizontal = 3 -max_length = 5 -caret_blink = true -caret_blink_speed = 0.5 - -[node name="GuessButton" type="Button" parent="C/V/V/H"] -margin_left = 386.0 -margin_right = 500.0 -margin_bottom = 51.0 text = "Guess" [node name="MenuButton" type="Button" parent="."] @@ -126,6 +112,5 @@ __meta__ = { [node name="ErrorFadeOut" type="AnimationPlayer" parent="."] anims/ErrorFadeOut = SubResource( 3 ) -[connection signal="text_entered" from="C/V/V/H/GuessText" to="." method="_on_GuessText_text_entered"] -[connection signal="pressed" from="C/V/V/H/GuessButton" to="." method="_on_GuessButton_pressed"] +[connection signal="pressed" from="C/V/V/GuessButton" to="." method="_on_GuessButton_pressed"] [connection signal="pressed" from="MenuButton" to="." method="_on_MenuButton_pressed"] diff --git a/src/Menu.tscn b/src/Menu.tscn index cdf343a..c9783c0 100644 --- a/src/Menu.tscn +++ b/src/Menu.tscn @@ -18,9 +18,6 @@ font_data = ExtResource( 1 ) anchor_right = 1.0 anchor_bottom = 1.0 script = ExtResource( 2 ) -__meta__ = { -"_edit_use_anchors_": false -} [node name="C" type="CenterContainer" parent="."] anchor_right = 1.0