[Unity2D] 새콤달콤 딸기농장 메인 오브젝트 개발(9) - 가게 미니게임 상한 딸기 찾기

SHG·2022년 10월 1일
0

필자에게 마지막 남은 구현 과제인 미니게임 2종이다. 새콤달콤 딸기농장 게임은 평균 연구 레벨이 15가 넘으면 미니게임 4종을 해금할 수 있다. 이 4종 중 2종을 맡았고 오늘 포스팅은 그 중 2번 째 미니게임인 상한 딸기 찾기이다!!

미니게임 구현에서 가장 중요한 점이 있었는데 바로 미니게임 4가지는 독립된 클래스가 아니라 미니게임 부모 클래스에서 상속받는 형태라는 것이다!! 코드를 첨부할 때 필요하다면 부모 클래스의 코드도 첨부하도록 하겠다.

구현 🪄


위와 같이 상한 딸기 찾기는 총 4x4 격자에서 명도가 다른 딸기(상한 딸기) 1개를 찾는 게임이다.

MiniGame2.cs 🎮

Awake() 👀

protected virtual void Awake() // 부모 클래스의 Awake()
{    
   // 미니게임 상단의 시간 제한을 나타내는 스크롤 바를 공통적으로 초기화 해준다.
   size = scroll.fillAmount / 60f;
   // 글로벌 변수를 공통적으로 tag를 기반으로 Find한다.
   global = GameObject.FindGameObjectWithTag("Global").GetComponent<Globalvariable>();
}

protected override void Awake() // 자식 클래스의 Awake()
{
   for (int i = 0; i < 16; i++)
   {
       int _i = i;
       answer_btn_4x4[_i].onClick.AddListener(() => OnClickAnswerButton(_i));
   }
   answerIndex_4x4 = new int[16];       
   base.Awake();
}

자식 클래스의 Awake() 함수에서 우선 16개의 격자(버튼)에 Listener를 할당해준다. 또한 정답 오답을 체크하기 위해 정답이 담길 배열을 초기화 해주는 역할을 한다.

MakeGame() 📖

protected override void MakeGame()
{
   // 음영 계수를 점수에 따라 정하기
   if (score < 100) shade_idx = 0;
   else if (100 <= score && score <= 200) shade_idx = 1;
   else if(score >= 200) shade_idx = 2;

   answer_img_4x4[randomAnswerIndex].color = new Vector4(1, 1, 1, 1);
   for (int i = 0; i < 16; i++)
   {
       answer_img_4x4[i].gameObject.SetActive(true);
       answer_btn_4x4[i].enabled = true;
   }

   //정상 딸기 만들고 이미지 배치
   while (true) // 상한, 무른 딸기를 제외하고 선정
   {
       normalIndex = unlockList[UnityEngine.Random.Range(0, unlockList.Count)];
       if (normalIndex != 4 && normalIndex != 8) break;
   }        

   //랜덤의 상한 딸기 인덱스(0~15)에 상한 딸기 배치
   randomAnswerIndex = UnityEngine.Random.Range(0, 16);
      
   for (int i = 0; i < 16; i++)
   {
       answerIndex_4x4[i] = normalIndex; // 딸기 배치
       answer_img_4x4[i].sprite = global.berryListAll[answerIndex_4x4[i]].GetComponent<SpriteRenderer>().sprite;
       if (randomAnswerIndex == i)
       {
           // 상한 or 무른 딸기 배치(음영으로)               
           float rgb = shaded[shade_idx];
           answer_img_4x4[i].color = new Vector4(rgb, rgb, rgb, 1);
           Debug.Log("answer_img_4x4[i].color.r: " + answer_img_4x4[i].color.r);
       }               
   }
}

MakeGame()은 플레이어가 선택지를 고를 때마다 호출하는 구조를 가진다. shade_idx는 shade배열에서 음영 계수를 담당하며 점수를 얻음에 따라 난이도를 조정하여 더 큰 재미를 느낄 수 있게 shade_idx 변수를 점수에 따라 조정하였다. 또한 0~15까지의 랜덤 인덱스를 선정하여 선정된 인덱스의 음영을 계수값에 따라 조절하였다.

OnClickAnswerButton(int index) & MakeNextQuiz() 📄

public void OnClickAnswerButton(int index)
{
    Color color = answer_img_4x4[index].color;
    //정답 : 10점 추가
    if (!color.Equals(new Vector4(1, 1, 1, 1)))
    {
        O.SetActive(true);
        score += 10;
        score_txt.text = score.ToString();
        AudioManager.instance.RightAudioPlay();

        Sandy.GetComponent<Image>().sprite = SandySprite[0];
    }
    //오답 : 10초 줄기
    else
    {
        X.SetActive(true);       
        scroll.fillAmount -= size * 10;
        time -= 10;
        AudioManager.instance.WrongAudioPlay();

        Sandy.GetComponent<Image>().sprite = SandySprite[1];
    }
    if (time > 0)
    {           
        // 다음문제 출제
        Invoke("MakeNextQuiz", 0.3f);
    }
}

void MakeNextQuiz()
{
    O.SetActive(false);
    X.SetActive(false);
    MakeGame();
}

플레이어가 고른 선택지에 따라 정답, 오답을 판별하고 정답이라면 +10점, 오답이라면 시간 제한 스크롤을 10만큼 줄인다.(남은 시간을 10초 줄임) 이 때 남은 시간이 있다면 MakeNextQuiz()를 통해 다음 문제를 출제한다!!
미니게임2의 플레이 영상으로 마무리~~!

profile
기록에 익숙해지자...!

0개의 댓글