Some checks failed
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (push) Successful in 16m25s
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (push) Successful in 13m9s
Build project / Publish to itch.io (StandaloneLinux64) (push) Successful in 9s
Build project / Publish to itch.io (StandaloneWindows64) (push) Successful in 9s
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (pull_request) Has been cancelled
Build project / Publish to itch.io (StandaloneLinux64) (pull_request) Has been cancelled
Build project / Publish to itch.io (StandaloneWindows64) (pull_request) Has been cancelled
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (pull_request) Has been cancelled
201 lines
5.7 KiB
C#
201 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using System.Text.RegularExpressions;
|
|
using UnityEngine.UI;
|
|
|
|
public enum WGameState
|
|
{
|
|
Uninitialized,
|
|
Playing,
|
|
Win,
|
|
Lose
|
|
}
|
|
|
|
public class WordPuzzle : MonoBehaviour
|
|
{
|
|
private TMP_Text[] allLetterTexts;
|
|
private List<string> words = new List<string>();
|
|
private int currentCount;
|
|
private int currentSubmit = 0;
|
|
public GameObject congratulations;
|
|
public GameObject retry;
|
|
public TMP_Text retryText;
|
|
public Color inWordColor = Color.yellow;
|
|
public Color perfectMatchColor = Color.green;
|
|
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;
|
|
|
|
private void Awake()
|
|
{
|
|
allLetterTexts = GetComponentsInChildren<TMP_Text>();
|
|
foreach (var text in allLetterTexts)
|
|
{
|
|
text.text = "";
|
|
text.color = Color.white;
|
|
}
|
|
words = Regex.Split(textAsset.text, Environment.NewLine)
|
|
.Where(x => !string.IsNullOrEmpty(x))
|
|
.Select(x => x.Trim().ToLower())
|
|
.ToList();
|
|
// add begin, found, doors to words
|
|
words.Add("begin");
|
|
words.Add("found");
|
|
words.Add("doors");
|
|
// remove duplicates
|
|
// words = words.Distinct().ToList();
|
|
Debug.Log("Loaded words: " + words.Count);
|
|
ResetPuzzle();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (gameState == WGameState.Playing)
|
|
{
|
|
HandleInput();
|
|
}
|
|
}
|
|
|
|
private void HandleInput()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Backspace))
|
|
{
|
|
RemoveChar();
|
|
}
|
|
else if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
|
|
{
|
|
SubmitButtonClick();
|
|
}
|
|
else
|
|
{
|
|
foreach (KeyCode key in Enum.GetValues(typeof(KeyCode)))
|
|
{
|
|
if (Input.GetKeyDown(key) && key >= KeyCode.A && key <= KeyCode.Z)
|
|
{
|
|
DoKeyPress(key.ToString().ToLower()[0]);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetGameState(WGameState state)
|
|
{
|
|
gameState = state;
|
|
}
|
|
|
|
public void StartGame(string overrideWord)
|
|
{
|
|
// currentWord = overrideWord.ToLower();
|
|
ResetPuzzle(overrideWord);
|
|
}
|
|
|
|
private void RemoveChar()
|
|
{
|
|
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 (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;
|
|
string submittedWord = "";
|
|
for (int i = currentSubmit * 5; i < currentSubmit * 5 + 5; i++)
|
|
{
|
|
submittedWord += allLetterTexts[i].text;
|
|
}
|
|
submittedWord = submittedWord.ToLower();
|
|
if (!words.Contains(submittedWord))
|
|
{
|
|
for (int i = currentSubmit * 5; i < currentSubmit * 5 + 5; i++)
|
|
{
|
|
allLetterTexts[i].text = "";
|
|
allLetterTexts[i].color = Color.white;
|
|
}
|
|
currentCount = 0;
|
|
return;
|
|
}
|
|
for (int i = currentSubmit * 5; i < currentSubmit * 5 + 5; i++)
|
|
{
|
|
char letter = char.ToLower(allLetterTexts[i].text[0]);
|
|
allLetterTexts[i].color = wrongColor;
|
|
if (currentWord.Contains(letter))
|
|
{
|
|
allLetterTexts[i].color = inWordColor;
|
|
int pos = i - (currentSubmit * 5);
|
|
if (letter == currentWord[pos])
|
|
{
|
|
allLetterTexts[i].color = perfectMatchColor;
|
|
}
|
|
}
|
|
}
|
|
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;
|
|
SetGameState(WGameState.Lose);
|
|
SoundFXManager.instance.PlaySound(loseSound, transform, 1.0f);
|
|
}
|
|
}
|
|
|
|
public void ResetPuzzle(string overrideWord = "")
|
|
{
|
|
if (!string.IsNullOrEmpty(overrideWord))
|
|
{
|
|
currentWord = overrideWord.ToLower();
|
|
}
|
|
else
|
|
{
|
|
currentWord = words[UnityEngine.Random.Range(0, words.Count)];
|
|
currentWord = currentWord.ToLower();
|
|
}
|
|
// Debug.Log("Chosen random word: " + currentWord);
|
|
allLetterTexts = GetComponentsInChildren<TMP_Text>();
|
|
retry.SetActive(false);
|
|
congratulations.SetActive(false);
|
|
foreach (var text in allLetterTexts)
|
|
{
|
|
text.text = "";
|
|
text.color = Color.white;
|
|
}
|
|
currentSubmit = 0;
|
|
currentCount = 0;
|
|
Debug.Log("Chosen word: " + currentWord);
|
|
SetGameState(WGameState.Playing);
|
|
}
|
|
}
|