반응형
1. KeyBoardKey.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;
enum Validity { None, Valid, Potential, Invalid }
public class KeyboardKey : MonoBehaviour
{
[Header(" Elements ")]
[SerializeField] private new Image renderer;
[SerializeField] private TextMeshProUGUI letterText;
[Header(" Settings ")]
private Validity validity;
[Header(" Events ")]
public static Action<char> onKeyPressed;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
GetComponent<Button>().onClick.AddListener(SendKeyPressedEvent);
Initialize();
}
// Update is called once per frame
void Update()
{
}
private void SendKeyPressedEvent()
{
onKeyPressed?.Invoke(letterText.text[0]);
}
public char GetLetter()
{
return letterText.text[0];
}
public void Initialize()
{
renderer.color = Color.white;
validity = Validity.None;
}
public void SetValid()
{
renderer.color = Color.green;
validity = Validity.Valid;
}
public void SetPotential()
{
if (validity == Validity.Valid)
return;
renderer.color = Color.yellow;
validity = Validity.Potential;
}
public void SetInvalid()
{
if (validity == Validity.Valid || validity == Validity.Potential)
return;
renderer.color = Color.gray;
validity = Validity.Invalid;
}
}
2. WordContainer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
public class WordContainer : MonoBehaviour
{
[Header(" Elements ")]
private LetterContainer[] letterContainers;
[Header(" Settings ")]
private int currentLetterIndex;
private void Awake()
{
letterContainers = GetComponentsInChildren<LetterContainer>();
//Initialize();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Initialize()
{
for (int i = 0; i < letterContainers.Length; i++)
letterContainers[i].Initialize();
}
public void Add(char letter)
{
letterContainers[currentLetterIndex].SetLetter(letter);
currentLetterIndex++;
}
public bool RemoveLetter()
{
if (currentLetterIndex <= 0)
return false;
currentLetterIndex--;
letterContainers[currentLetterIndex].Initialize();
return true;
}
public string GetWord()
{
string word = "";
for (int i = 0; i < letterContainers.Length; i++)
word += letterContainers[i].GetLetter().ToString();
return word;
}
public void Colorize(string secretWord)
{
List<char> chars = new List<char>(secretWord.ToCharArray());
for (int i = 0; i < letterContainers.Length; i++)
{
char letterToCheck = letterContainers[i].GetLetter();
if(letterToCheck == secretWord[i])
{
//Valid
letterContainers[i].SetValid();
chars.Remove(letterToCheck);
}
else if(chars.Contains(letterToCheck))
{
// Pontential
letterContainers[i].SetPotential();
chars.Remove(letterToCheck);
}
else
{
// Invalid
letterContainers[i].SetInvalid();
}
}
}
public bool IsComplete()
{
return currentLetterIndex >= 5;
}
}
3. LetterContainer.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class LetterContainer : MonoBehaviour
{
[Header(" Elements ")]
[SerializeField] private SpriteRenderer letterContainer;
[SerializeField] private TextMeshPro letter;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Initialize()
{
letter.text = "";
letterContainer.color = Color.white;
}
public void SetLetter(char letter)
{
this.letter.text = letter.ToString();
}
public void SetValid()
{
letterContainer.color = Color.green;
}
public void SetPotential()
{
letterContainer.color = Color.yellow;
}
public void SetInvalid()
{
letterContainer.color = Color.gray;
}
public char GetLetter()
{
return letter.text[0];
}
}
4. WordManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WordManager : MonoBehaviour
{
public static WordManager instance;
[Header(" Elements ")]
[SerializeField] private string secretWord;
private void Awake()
{
if (instance == null)
instance = this;
else
Destroy(gameObject);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public string GetSecretWord()
{
return secretWord.ToUpper();
}
}
5. InputManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class InputManager : MonoBehaviour
{
[Header(" Elements ")]
[SerializeField] private WordContainer[] wordContainers;
[SerializeField] private Button tryButton;
[SerializeField] private KeyboardColorizer keyboardColorizer;
[Header(" Settings ")]
private int currentWordContainerIndex;
private bool canAddLetter = true;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Initialize();
KeyboardKey.onKeyPressed += KeyPressedCallback;
}
// Update is called once per frame
void Update()
{
}
private void Initialize()
{
for (int i = 0; i < wordContainers.Length; i++)
wordContainers[i].Initialize();
}
private void KeyPressedCallback(char letter)
{
if (!canAddLetter)
return;
wordContainers[currentWordContainerIndex].Add(letter);
if (wordContainers[currentWordContainerIndex].IsComplete())
{
canAddLetter = false;
EnableTryButton();
//CheckWord();
//currentWordContainerIndex++;
}
}
public void CheckWord()
{
string wordToCheck = wordContainers[currentWordContainerIndex].GetWord();
string secretWord = WordManager.instance.GetSecretWord();
wordContainers[currentWordContainerIndex].Colorize(secretWord);
keyboardColorizer.Colorize(secretWord, wordToCheck);
if (wordToCheck == secretWord)
Debug.Log("Level Complete");
else
{
Debug.Log("Wrong word");
canAddLetter = true;
DisableTryButton();
currentWordContainerIndex++;
}
}
public void BackspacePressedCallback()
{
bool removedLetter = wordContainers[currentWordContainerIndex].RemoveLetter();
if (removedLetter)
DisableTryButton();
canAddLetter = true;
}
private void EnableTryButton()
{
tryButton.interactable = true;
}
private void DisableTryButton()
{
tryButton.interactable = false;
}
}
6. KeyboardColorizer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KeyboardColorizer : MonoBehaviour
{
[Header(" Elements ")]
private KeyboardKey[] keys;
private void Awake()
{
keys = GetComponentsInChildren<KeyboardKey>();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Colorize(string secretWord, string wordToCheck)
{
for (int i = 0; i < keys.Length; i++)
{
char keyLetter = keys[i].GetLetter();
for (int j = 0; j < wordToCheck.Length; j++)
{
if (keyLetter != wordToCheck[j])
continue;
// the key letter we've pressed is equals to the current wordToCheck letter
if(keyLetter == secretWord[j])
{
// Valid
keys[i].SetValid();
}
else if(secretWord.Contains(keyLetter))
{
// Potential
keys[i].SetPotential();
}
else
{
// InValid
keys[i].SetInvalid();
}
}
}
}
}
반응형
'IT > programming' 카테고리의 다른 글
5-LETTER WORD GAME SCRIPTS 1차 최종 정리 (0) | 2025.07.04 |
---|---|
UIManager.cs & NEXT 까지 2차 코드 정리 (0) | 2025.07.03 |
대장동 사업의 진실 (1) | 2025.06.28 |
이재명 일기장 (3) | 2025.06.23 |
내란의 힘이 이재명을 두려워하는 이유 (0) | 2025.06.23 |