課題9: ゲーム終了時のタイマー待ちのコード更新修正

目的: ゲーム終了時に不要なタイマー待ちが発生しないように、GameDirector のコードを改善し、ゲーム終了後の処理を効率的に行う方法を学ぶ。

背景:

  • 現在のコードでは、タイマーが0になった後もUpdateメソッドが継続的に実行され、無意味な処理が発生する可能性があります。これを改善することで、ゲーム終了後のリソース消費を減らし、パフォーマンスを向上させます。

ステップ:

1. ゲーム終了処理の追加

  • ゲーム終了時に、Updateメソッドの処理を停止し、必要な終了処理(例: ゲームオーバー画面の表示)を行います。

2. GameDirector にゲーム終了フラグを追加

  • ゲーム終了時に処理を停止するためのフラグを追加します。
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class GameDirector : MonoBehaviour
{
    public List<GenerationParameters> generationParametersList; // ScriptableObjectのリスト
    private int currentStage = 0;
    private float elapsedTime = 0.0f;

    GameObject timerText;
    GameObject pointText;
    float time = 30.0f;
    public int point = 0;
    GameObject generator;
    bool isGameOver = false; // ゲーム終了フラグ

    public GameObject gameOverUI;

    void Start()
    {
        this.timerText = GameObject.Find("Time");
        this.pointText = GameObject.Find("Point");
        this.generator = GameObject.Find("ItemGenerator");

        // 初期パラメータセットを適用
        ApplyCurrentGenerationParameters();
    }

    void Update()
    {
        if (isGameOver)
            return; // ゲーム終了後は処理を停止

        this.time -= Time.deltaTime;
        elapsedTime += Time.deltaTime;

        // パラメータセットの切り替え
        if (currentStage < generationParametersList.Count && elapsedTime > generationParametersList[currentStage].duration)
        {
            elapsedTime = 0.0f;  // elapsedTime をリセット

            ApplyCurrentGenerationParameters(); // currentStage が有効な範囲内でパラメータを適用

            currentStage++; // パラメータ適用後にインクリメント
        }

        // タイマー終了時の処理
        if (this.time < 0)
        {
            this.time = 0;
            this.generator.GetComponent<ItemGenerator>().SetParameter(10000.0f, 0, 0);
            EndGame(); // ゲーム終了処理を呼び出す
        }

        this.timerText.GetComponent<TextMeshProUGUI>().text = this.time.ToString("F1");
        UpdateScoreUI();
    }

    // ゲーム終了処理
    private void EndGame()
    {
        isGameOver = true; // フラグを立ててUpdateメソッドを停止
        // ゲームオーバー画面を表示するなどの処理を追加
        Debug.Log("Game Over!");

        // 例: ゲームオーバー画面のUIを表示
        // if(gameOverUI != null)
        // {
        //     gameOverUI.SetActive(true);
        // }
    }

    // パラメータセットを適用するメソッド
    private void ApplyCurrentGenerationParameters()
    {
        GenerationParameters currentParams = generationParametersList[currentStage];
        this.generator.GetComponent<ItemGenerator>().SetParameter(currentParams.span, currentParams.speed, currentParams.ratio);
    }

    // スコア変更イベントハンドラ
    public void HandleScoreChange(object sender, ScoreEventArgs e)
    {
        this.point += e.ScoreChange;
        UpdateScoreUI();
    }

    // スコアUIの更新
    private void UpdateScoreUI()
    {
        this.pointText.GetComponent<TextMeshProUGUI>().text = this.point.ToString() + " point";
    }
}

3. ゲームオーバー画面の作成(オプション)

  • Unityエディタで新しいUI Canvasを作成し、ゲームオーバー時に表示するUIを配置します(例: “GameOverUI")。
  • EndGame() メソッド内で、このUIをアクティブにする処理を追加します。
// 最初にUIをドラッグ&ドロップしておく
public GameObject gameOverUI;

private void EndGame()
{
    isGameOver = true;
    Debug.Log("Game Over!");

    // ゲームオーバー画面のUIを表示
    if(gameOverUI != null)
    {
        gameOverUI.SetActive(true);
    }
}

4. タイマー待ちの削減:

  • ゲーム終了後にUpdateメソッドが不要な処理を行わないように、isGameOver フラグを利用して処理を停止します。

5. テストと確認:

  • ゲームをプレイし、タイマーが0になった際にゲームが終了し、不要なタイマー待ちの処理が行われないことを確認する。
  • ゲームオーバー画面が正しく表示されることを確認する(オプション)。

学習ポイント:

  • ゲーム終了時の処理方法。
  • フラグを用いた処理の制御。
  • 不要な処理の停止によるパフォーマンス向上。

Unity

Posted by hidepon