From cba70333710b3511bca6f393bcd9b63dfcf49e99 Mon Sep 17 00:00:00 2001 From: nlevin6 Date: Sat, 29 Mar 2025 23:50:49 -0400 Subject: [PATCH] fixed wordle --- .../Word Puzzle/Scripts/TabletToggle.cs | 24 ++++ .../Word Puzzle/Scripts/TabletToggle.cs.meta | 2 + .../Puzzles/Word Puzzle/Scripts/WordPuzzle.cs | 124 +++++++++--------- Assets/Scenes/level2.unity | 54 +++++++- 4 files changed, 139 insertions(+), 65 deletions(-) create mode 100644 Assets/Puzzles/Word Puzzle/Scripts/TabletToggle.cs create mode 100644 Assets/Puzzles/Word Puzzle/Scripts/TabletToggle.cs.meta diff --git a/Assets/Puzzles/Word Puzzle/Scripts/TabletToggle.cs b/Assets/Puzzles/Word Puzzle/Scripts/TabletToggle.cs new file mode 100644 index 00000000..a4002f8f --- /dev/null +++ b/Assets/Puzzles/Word Puzzle/Scripts/TabletToggle.cs @@ -0,0 +1,24 @@ +using UnityEngine; +using UnityEngine.EventSystems; +using TMPro; + +public class TabletToggle : MonoBehaviour +{ + public GameObject tablet; + + void Update() + { + if (Input.GetKeyDown(KeyCode.T)) + { + TMP_InputField inputField = null; + if (EventSystem.current != null && EventSystem.current.currentSelectedGameObject != null) + { + inputField = EventSystem.current.currentSelectedGameObject.GetComponent(); + } + if (inputField != null && inputField.isFocused) + return; + + tablet.SetActive(!tablet.activeSelf); + } + } +} diff --git a/Assets/Puzzles/Word Puzzle/Scripts/TabletToggle.cs.meta b/Assets/Puzzles/Word Puzzle/Scripts/TabletToggle.cs.meta new file mode 100644 index 00000000..ad506194 --- /dev/null +++ b/Assets/Puzzles/Word Puzzle/Scripts/TabletToggle.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 1f363690cf70e6449a55d819bd9503b6 \ No newline at end of file diff --git a/Assets/Puzzles/Word Puzzle/Scripts/WordPuzzle.cs b/Assets/Puzzles/Word Puzzle/Scripts/WordPuzzle.cs index 066fea3e..9e8bb5e3 100644 --- a/Assets/Puzzles/Word Puzzle/Scripts/WordPuzzle.cs +++ b/Assets/Puzzles/Word Puzzle/Scripts/WordPuzzle.cs @@ -17,11 +17,9 @@ public enum WGameState public class WordPuzzle : MonoBehaviour { private TMP_Text[] allLetterTexts; - private List usedLetterTexts = new List(); private List words = new List(); private int currentCount; private int currentSubmit = 0; - public GameObject congratulations; public GameObject retry; public TMP_Text retryText; @@ -30,7 +28,6 @@ public class WordPuzzle : MonoBehaviour public Color wrongColor = Color.red; public string currentWord = string.Empty; public WGameState gameState = WGameState.Uninitialized; - [SerializeField] private TextAsset textAsset = null; public AudioClip loseSound; public AudioClip winSound; @@ -40,15 +37,14 @@ public class WordPuzzle : MonoBehaviour allLetterTexts = GetComponentsInChildren(); foreach (var text in allLetterTexts) { - text.text = string.Empty; + text.text = ""; + text.color = Color.white; } - - words = Regex.Split(textAsset.text, Environment.NewLine).ToList(); - if (string.IsNullOrEmpty(currentWord)) { - currentWord = words[UnityEngine.Random.Range(0, words.Count)]; - Debug.Log("Chosen random word: " + currentWord); - } - gameState = WGameState.Uninitialized; + words = Regex.Split(textAsset.text, Environment.NewLine) + .Where(x => !string.IsNullOrEmpty(x)) + .Select(x => x.Trim().ToLower()) + .ToList(); + ResetPuzzle(); } private void Update() @@ -89,76 +85,78 @@ public class WordPuzzle : MonoBehaviour public void StartGame(string overrideWord) { - currentWord = overrideWord; + currentWord = overrideWord.ToLower(); ResetPuzzle(); } private void RemoveChar() { - if (!usedLetterTexts.Any() || currentCount <= 0) - return; - - usedLetterTexts.Last().text = string.Empty; - usedLetterTexts.RemoveAt(usedLetterTexts.Count - 1); + if (currentCount <= 0) return; + int index = currentSubmit * 5 + (currentCount - 1); + if (index < 0 || index >= allLetterTexts.Length) return; + allLetterTexts[index].text = ""; + allLetterTexts[index].color = Color.white; currentCount--; } private void DoKeyPress(char character) { - if (usedLetterTexts.Count >= allLetterTexts.Length || currentCount >= 5) - return; - - var currentText = allLetterTexts[usedLetterTexts.Count]; - currentText.text = character.ToString(); - usedLetterTexts.Add(currentText); - currentCount++; + if (currentCount >= 5) return; + int index = currentSubmit * 5 + currentCount; + if (index < allLetterTexts.Length) + { + allLetterTexts[index].text = character.ToString(); + currentCount++; + if (currentCount == 5) + { + SubmitButtonClick(); + } + } } private void SubmitButtonClick() { - if (currentCount != 5) - return; - - var submittedWord = string.Empty; - int ii = 0; - for (int i = 0 + (currentSubmit * 5); i < usedLetterTexts.Count; i++) + if (currentCount != 5) return; + string submittedWord = ""; + for (int i = currentSubmit * 5; i < currentSubmit * 5 + 5; i++) { - TMP_Text texts = usedLetterTexts[i]; - var textsChar = char.Parse(texts.text); - submittedWord += textsChar; - - texts.color = wrongColor; - if (currentWord.Contains(textsChar)) - { - texts.color = inWordColor; - if (textsChar == currentWord[ii]) - texts.color = perfectMatchColor; - } - ii++; + submittedWord += allLetterTexts[i].text; } - - if (words.Contains(submittedWord)) + submittedWord = submittedWord.ToLower(); + if (!words.Contains(submittedWord)) { - if(submittedWord == currentWord) + for (int i = currentSubmit * 5; i < currentSubmit * 5 + 5; i++) { - congratulations.SetActive(true); - SetGameState(WGameState.Win); - SoundFXManager.instance.PlaySound(winSound, transform, 1.0f); - return; + allLetterTexts[i].text = ""; + allLetterTexts[i].color = Color.white; } - currentSubmit++; currentCount = 0; + return; } - else + for (int i = currentSubmit * 5; i < currentSubmit * 5 + 5; i++) { - for (int i = 0 + (currentSubmit * 5); i < usedLetterTexts.Count; i++) + char letter = char.ToLower(allLetterTexts[i].text[0]); + allLetterTexts[i].color = wrongColor; + if (currentWord.Contains(letter)) { - TMP_Text texts = usedLetterTexts[i]; - texts.color = Color.white; + allLetterTexts[i].color = inWordColor; + int pos = i - (currentSubmit * 5); + if (letter == currentWord[pos]) + { + allLetterTexts[i].color = perfectMatchColor; + } } } - - if(currentSubmit == 6) + if (submittedWord == currentWord) + { + congratulations.SetActive(true); + SetGameState(WGameState.Win); + SoundFXManager.instance.PlaySound(winSound, transform, 1.0f); + return; + } + currentSubmit++; + currentCount = 0; + if (currentSubmit == 6) { retry.SetActive(true); retryText.text = currentWord; @@ -172,18 +170,16 @@ public class WordPuzzle : MonoBehaviour allLetterTexts = GetComponentsInChildren(); retry.SetActive(false); congratulations.SetActive(false); - usedLetterTexts.Clear(); - foreach (var texts in allLetterTexts) + foreach (var text in allLetterTexts) { - texts.text = string.Empty; - texts.color = Color.white; + text.text = ""; + text.color = Color.white; } currentSubmit = 0; currentCount = 0; - if (string.IsNullOrEmpty(currentWord)) { - currentWord = words[UnityEngine.Random.Range(0, words.Count)]; - Debug.Log("Chosen random word: " + currentWord); - } + currentWord = words[UnityEngine.Random.Range(0, words.Count)]; + currentWord = currentWord.ToLower(); + Debug.Log("Chosen random word: " + currentWord); SetGameState(WGameState.Playing); } } diff --git a/Assets/Scenes/level2.unity b/Assets/Scenes/level2.unity index 0eba440c..233a7396 100644 --- a/Assets/Scenes/level2.unity +++ b/Assets/Scenes/level2.unity @@ -19334,6 +19334,8 @@ MonoBehaviour: keyCardName: move rack keyCardPlayer: {fileID: 0} interactionDistance: 5 + keyCardItem: {fileID: 0} + playerInventory: {fileID: 0} --- !u!65 &1050845135 BoxCollider: m_ObjectHideFlags: 0 @@ -24105,6 +24107,51 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 69125d056127da746a8a1ed8371ea8d6, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!1 &1328799368 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1328799370} + - component: {fileID: 1328799369} + m_Layer: 0 + m_Name: TabletToggleManager + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1328799369 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1328799368} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1f363690cf70e6449a55d819bd9503b6, type: 3} + m_Name: + m_EditorClassIdentifier: + tablet: {fileID: 0} +--- !u!4 &1328799370 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1328799368} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0.88123596, y: 23.22024, z: -4.681444} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1339936851 GameObject: m_ObjectHideFlags: 0 @@ -37444,6 +37491,8 @@ MonoBehaviour: keyCardName: move rack keyCardPlayer: {fileID: 0} interactionDistance: 5 + keyCardItem: {fileID: 0} + playerInventory: {fileID: 0} --- !u!65 &2090030717 BoxCollider: m_ObjectHideFlags: 0 @@ -39966,6 +40015,8 @@ MonoBehaviour: keyCardName: Deck C key card keyCardPlayer: {fileID: 7685886779921947557} interactionDistance: 5 + keyCardItem: {fileID: 0} + playerInventory: {fileID: 0} --- !u!114 &4244626406387182888 MonoBehaviour: m_ObjectHideFlags: 0 @@ -40787,7 +40838,7 @@ Transform: m_GameObject: {fileID: 7094977339719025836} serializedVersion: 2 m_LocalRotation: {x: -0, y: -0.6649941, z: -0, w: 0.7468486} - m_LocalPosition: {x: -49.75, y: -9.26, z: 23.94} + m_LocalPosition: {x: -49.75, y: 7.59, z: 28.33} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: @@ -41749,3 +41800,4 @@ SceneRoots: - {fileID: 171829751} - {fileID: 1766395669} - {fileID: 1232642964} + - {fileID: 1328799370}