WordContainer, InputManager, WordManager 까지 코드정리

반응형

게임 개발을 시작해 왔는지 1개월이 다 되어 가고 있습니다.

솔직히 유튜뷰 영상 무자막으로 아무것도 모르는 초보자가 시작한다는 것은 말도 안되는 이야기이지만 

누구나 해 낼 수 있다는 것을 보여 줄려고 엄청나게 반복적인 작업을 하고 있는지 모를것입니다.

지우고 또 처음부터 다시, 벌써 10번은 훌쩍 넘ㄱ지 않았나 싶습니다.

빨리 완성을 해야 한다는 열정만 가지고 시작한지도 어느듯 한달을 눈앞에 두고 있습니다.

7~80% 완성되었다 싶으면 어디에선가 ERROR가 나타나 결국 풀지 못하고 처음부터 다시, 또 다시

그래서 이제는 단계별로 기록을 남기기 시작했습니다.

그래서 처음부터 Words 체크까지 1차적으로 작업을 하였습니다.

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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 string GetWord()
    {
        string word = "";

        for (int i = 0; i < letterContainers.Length; i++) 
            word += letterContainers[i].GetLetter().ToString();
        
        return word;
    }

    public bool IsComplete()
    {
        return currentLetterIndex >= 5;
    }
}

 

InputManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputManager : MonoBehaviour
{
    [Header("Elements")]
    [SerializeField] private WordContainer[] wordContainers;

    [Header("Settings")]
    private int currentWordContainerIndex;
    // Start is called before the first frame update
    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)
    {
        wordContainers[currentWordContainerIndex].Add(letter);

        if (wordContainers[currentWordContainerIndex].IsComplete())
        {
            CheckWord();
            //currentWordContainerIndex++;
        }
    }

    private void CheckWord()
    {
        string wordToCheck = wordContainers[currentWordContainerIndex].GetWord();
        string secretWord = WordManager.instance.GetSecretWord();
        if (wordToCheck == secretWord)
            Debug.Log("Level Complete");
        else
        {
            Debug.Log("Wrong word");
            currentWordContainerIndex++;
        }
    }
}

 

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();
    }
}
반응형