Unity チュートリアルのタワーディフェンステンプレートを触ってみる(45)
Unity チュートリアルのタワーディフェンステンプレートを触ってみる(44)では Agent の Wave を登録する Wave について解説を行った。今回は Wave を登録し管理する WaveManager に付いて解説を行っていく。
1.タワーディフェンステンプレートのステージの設定編 – WaveManager.cs
WaveManager.cs について稚拙ながら解説
「WaveManager.cs」は「Assets/Scripts/TowerDefense/Level/WaveManager.cs」の指しておりスクリプトについては以下の通り。内容としては Wave の生成管理を行っている。
[cce_csharp]using System;
using System.Collections.Generic;
using Core.Extensions;
using UnityEngine;
namespace TowerDefense.Level
{
/// <summary>
/// WaveManager - handles wave initialisation and completion
/// </summary>
public class WaveManager : MonoBehaviour
{
/// <summary>
/// Current wave being used
/// </summary>
protected int m_CurrentIndex;
/// <summary>
/// Whether the WaveManager starts waves on Awake - defaulted to null since the LevelManager should call this function
/// </summary>
public bool startWavesOnAwake;
/// <summary>
/// The waves to run in order
/// </summary>
[Tooltip("Specify this list in order")]
public List<Wave> waves = new List<Wave>();
/// <summary>
/// The current wave number
/// </summary>
public int waveNumber
{
get { return m_CurrentIndex + 1; }
}
/// <summary>
/// The total number of waves
/// </summary>
public int totalWaves
{
get { return waves.Count; }
}
public float waveProgress
{
get
{
if (waves == null || waves.Count <= m_CurrentIndex)
{
return 0;
}
return waves[m_CurrentIndex].progress;
}
}
/// <summary>
/// Called when a wave begins
/// </summary>
public event Action waveChanged;
/// <summary>
/// Called when all waves are finished
/// </summary>
public event Action spawningCompleted;
/// <summary>
/// Starts the waves
/// </summary>
public virtual void StartWaves()
{
if (waves.Count > 0)
{
InitCurrentWave();
}
else
{
Debug.LogWarning("[LEVEL] No Waves on wave manager. Calling spawningCompleted");
SafelyCallSpawningCompleted();
}
}
/// <summary>
/// Inits the first wave
/// </summary>
protected virtual void Awake()
{
if (startWavesOnAwake)
{
StartWaves();
}
}
/// <summary>
/// Sets up the next wave
/// </summary>
protected virtual void NextWave()
{
waves[m_CurrentIndex].waveCompleted -= NextWave;
if (waves.Next(ref m_CurrentIndex))
{
InitCurrentWave();
}
else
{
SafelyCallSpawningCompleted();
}
}
/// <summary>
/// Initialize the current wave
/// </summary>
protected virtual void InitCurrentWave()
{
Wave wave = waves[m_CurrentIndex];
wave.waveCompleted += NextWave;
wave.Init();
if (waveChanged != null)
{
waveChanged();
}
}
/// <summary>
/// Calls spawningCompleted event
/// </summary>
protected virtual void SafelyCallSpawningCompleted()
{
if (spawningCompleted != null)
{
spawningCompleted();
}
}
}
}[/cce_csharp]
70行目 : 「public virtual void StartWaves()」は Wave の開始処理を行っている。内容としては Wave の設定があれば InitCurrentWave を行い、設定がなければ警告メッセージを出力後、SafelyCallSpawningCompleted を行っている。
86行目 : 「protected virtual void Awake()」は Unity 固有の起動処理を行っている。内容としては startWavesOnAwake が True であれば StartWaves を行っている。
97行目 : 「protected virtual void NextWave()」は次の Wave 出現処理を行っている。内容としては Wave の waveCompleted から NextWave を解除後、次の Wave が存在していれば、 InitCurrentWave を行い、存在していなければ SafelyCallSpawningCompleted を行っている。
113行目 : 「protected virtual void InitCurrentWave()」は Wave の初期化処理を行っている。Wave の配列から Wave を取得し、Wave の waveCompleted に NextWave を設定後、waveChanged の設定があれば waveChanged を実行している。
127行目 : 「protected virtual void SafelyCallSpawningCompleted()」は Agent のスポーンが完了時の処理を行っており、spawningCompleted の設定があれば spawningCompleted の処理を行っている。