Unity チュートリアルのタワーディフェンステンプレートを触ってみる(40)ではタワーを配置するための TowerPlacementGrid の解説を行った。今回は本拠地の設定に必要な PlayerHomeBase の解説を行っていく。
1.タワーディフェンステンプレートのステージの設定編 – PlayerHomeBase.cs
PlayerHomeBase.cs について稚拙ながら解説
「PlayerHomeBase.cs」は「Assets/Scripts/TowerDefense/Level/PlayerHomeBase.cs」の指しておりスクリプトについては以下の通り。内容としてはグリッド状のタワー設置に関するを行っている。
[cce_csharp]using System.Collections.Generic;
using ActionGameFramework.Audio;
using Core.Health;
using TowerDefense.Agents;
using UnityEngine;
namespace TowerDefense.Level
{
/// <summary>
/// A class representing the home base that players must defend
/// </summary>
public class PlayerHomeBase : DamageableBehaviour
{
/// <summary>
/// The particle system when an attack is charging
/// </summary>
public ParticleSystem chargePfx;
/// <summary>
/// Sound to play when charge effect starts
/// </summary>
public RandomAudioSource chargeSound;
/// <summary>
/// The particle system for an attack
/// </summary>
public ParticleSystem attackPfx;
/// <summary>
/// Sound to play when attack effect starts
/// </summary>
public RandomAudioSource attackSound;
/// <summary>
/// The current Agents within the home base attack zone
/// </summary>
protected List<Agent> m_CurrentAgentsInside = new List<Agent>();
/// <summary>
/// Subscribes to damaged event
/// </summary>
protected virtual void Start()
{
configuration.damaged += OnDamaged;
}
/// <summary>
/// Unsubscribes to damaged event
/// </summary>
protected virtual void OnDestroy()
{
configuration.damaged -= OnDamaged;
}
/// <summary>
/// Plays <see cref="attackPfx"/> if assigned
/// </summary>
protected virtual void OnDamaged(HealthChangeInfo obj)
{
if (attackPfx != null)
{
attackPfx.Play();
}
if (attackSound != null)
{
attackSound.PlayRandomClip();
}
}
/// <summary>
/// Adds triggered Agent to tracked Agents, subscribes to Agent's
/// removed event and plays pfx
/// </summary>
/// <param name="other">Triggered collider</param>
void OnTriggerEnter(Collider other)
{
var homeBaseAttacker = other.GetComponent<HomeBaseAttacker>();
if (homeBaseAttacker == null)
{
return;
}
m_CurrentAgentsInside.Add(homeBaseAttacker.agent);
homeBaseAttacker.agent.removed += OnAgentRemoved;
if (chargePfx != null)
{
chargePfx.Play();
}
if (chargeSound != null)
{
chargeSound.PlayRandomClip();
}
}
/// <summary>
/// If the entity that has entered the collider
/// has an <see cref="Agent"/> component on it
/// </summary>
void OnTriggerExit(Collider other)
{
var homeBaseAttacker = other.GetComponent<HomeBaseAttacker>();
if (homeBaseAttacker == null)
{
return;
}
RemoveTarget(homeBaseAttacker.agent);
}
/// <summary>
/// Removes Agent from tracked <see cref="m_CurrentAgentsInside"/>
/// </summary>
void OnAgentRemoved(DamageableBehaviour targetable)
{
targetable.removed -= OnAgentRemoved;
Agent attackingAgent = targetable as Agent;
RemoveTarget(attackingAgent);
}
/// <summary>
/// Removes <paramref name="agent"/> from <see cref="m_CurrentAgentsInside"/> and stops pfx
/// if there are no more <see cref="Agent"/>s
/// </summary>
/// <param name="agent">
/// The agent to remove
/// </param>
void RemoveTarget(Agent agent)
{
if (agent == null)
{
return;
}
m_CurrentAgentsInside.Remove(agent);
if (m_CurrentAgentsInside.Count == 0 && chargePfx != null)
{
chargePfx.Stop();
}
}
}
}[/cce_csharp]
12行目 : 「public class PlayerHomeBase : DamageableBehaviour」は DamageableBehaviour を継承していることを指している。
DamageableBehaviour の解説については以前に行っているため、こちらを参照してほしい。
| Unity チュートリアルのタワーディフェンステンプレートを触ってみる(4) では作成した TowerLevelData スクリプタブルオブジェクトと TowerLevel プレハブを用いてタワーの作成を進めた。今回はタワーの作成にあたって追加した Tower.cs スクリプトの解説を行っていく。「Tower.cs」 は 「Targetable.cs」 を継承し、「Targetable.cs」 は 「DamageableBehaviour.cs」 を継承している。大本である「DamageableBehaviour.cs」から順に解説していったほうがわかりやすいと思われるため、そちらから順に説明を行っていく。『継承(けい... Unity チュートリアルのタワーディフェンステンプレートを触ってみる(5) - StudioFun |
42行目 : 「protected virtual void Start()」は Unity 固有の開始処理を行っている。内容としてはダメージを受けた時の処理 OnDamaged の設定をしている。
50行目 : 「protected virtual void OnDestroy()」は Unity 固有の破壊処理を行っている。内容としてはダメージを受けた時の処理 OnDamaged の設定を解除している。
58行目 : 「protected virtual void OnDamaged(HealthChangeInfo obj)」はダメージの処理を行っている。内容としてはダメージを受けた時のエフェクトと音声を実行している。
75行目 : 「void OnTriggerEnter(Collider other)」は Unity 固有の他の Collider がこのゲームオブジェクトの Collider に入ったときの処理を行っている。内容としては入ってきた Collider が HomeBaseAttacker であれば、ホームベースにいる Agent として m_CurrentAgentsInside に追加し、OnAgentRemoved 処理を Agent の removed に登録後、HomeBaseAttacker の攻撃エフェクトと攻撃音を実行している。
98行目 : 「void OnTriggerExit(Collider other)」は Unity 固有の他の Collider がこのゲームオブジェクトの Collider から出ていったときの処理を行っている。内容としては出ていった Collider が HomeBaseAttacker であれば、RemoveTarget の処理を行っている。
111行目 : 「void OnAgentRemoved(DamageableBehaviour targetable)」は Agent の removed から OnAgentRemoved 処理を解除後、RemoveTarget の処理を行っている。
125行目 : 「void RemoveTarget(Agent agent)」は m_CurrentAgentsInside 登録されている Agent を削除後、HomeBaseAttacker の攻撃エフェクトを終了している。
