Ejemplo n.º 1
0
    // Start is called before the first frame update
    IEnumerator Start()
    {
        // 参照キャッシュ
        HUDController           = GameObject.Find("HUD").GetComponent <HUDController>();
        answerButtonController  = FindObjectOfType <AnswerButtonController>();
        handController          = FindObjectOfType <HandController>();
        timeLimitAreaController = GameObject.Find("TimeLimitArea").GetComponent <TimeLimitAreaController>();
        scoreAreaController     = GameObject.Find("ScoreArea").GetComponent <ScoreAreaController>();

        // このオブジェクトのコンポーネント
        audioSource = GetComponent <AudioSource>();

        session = new Session(scoreAreaController,
                              timeLimitAreaController,
                              InitialTimeLimitSeconds,
                              wrongAnswerPenaltySeconds);


        // 中断されるまで繰り返す
        while (session.CurrentTimeLimitSeconds > 0)
        {
            session.elapsedTimeInTurn = 0f;

            // カードを配る
            session.Deal();

            // 手札をクリア
            handController.ClearCards();

            // カード表示
            handController.PlaceCards(session.Hand.Cards);

            // 選択肢を用意する
            var choices = session.GenerateChoices();
            // 選択肢表示
            answerButtonController.SetChoices(choices);

            do
            {
                // プレイヤーが答えるまで待つ
                playerHasAnswered = false;

                // 時間切れの際、WaitPlayerToAnswerは中断される
                yield return(StartCoroutine(WaitPlayerToAnswer()));

                // 中断時はdo-whileを抜ける
                if (!playerHasAnswered)
                {
                    break;
                }

                if (session.AnswerIsCorrect)
                {
                    playerHasAnswered = true;

                    session.rule.OnCorrectAnswer();

                    HUDController.DisplaycorrectOverlay();

                    audioSource.PlayOneShot(CorrectAnswerAudioClip);
                }
                else
                {
                    // 失敗時ペナルティ
                    session.rule.OnWrongAnswer();

                    audioSource.PlayOneShot(WrongAnswerAudioClip);
                }
            } while (!session.rule.ShouldProceedToNextTurn());
        }

        // 時間切れになったらゲームオーバー
        audioSource.Stop();
        audioSource.PlayOneShot(TimeUpAudioClip);
        answerButtonController.Disable();

        var modalWindow = GameObject.Find("ResultModal").GetComponent <ModalWindowManager>();

        modalWindow.titleText       = "Result";
        modalWindow.descriptionText = $"Score: {session.Score}\nMiss: 0";
        modalWindow.UpdateUI();
        modalWindow.OpenWindow();

        yield return(new WaitForSeconds(3.0f));

        audioSource.clip = ResultAudioClip;
        audioSource.Play();
    }