Unity チュートリアルのタワーディフェンステンプレートを触ってみる(21)

Unity チュートリアルのタワーディフェンステンプレートを触ってみる(20)では LaserTower_0 に追加した HitscanLauncher のインタフェースである ILauncher の解説を行った。今回は LaserTower_0 に追加した HitscanLauncher の継承関係にある Launcher の解説を行っていく。
1.標的設定と砲撃編 – Launcher.cs
Launcher.cs について稚拙ながら解説
「Launcher.cs」は「Assets/Scripts/TowerDefense/Towers/TowerLaunchers/Launcher.cs」の指しておりスクリプトについては以下の通り。内容としては複数個に対する砲撃処理を行っている。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 using System.Collections.Generic;
using ActionGameFramework.Health;
using Core.Utilities;
using UnityEngine;
namespace TowerDefense.Towers.TowerLaunchers
{
public abstract class Launcher : MonoBehaviour, ILauncher
{
public abstract void Launch(Targetable enemy, GameObject attack, Transform firingPoint);
/// <summary>
/// Gets an instance of the attack object from the Pool and Launches it
/// </summary>
/// <param name="enemies">
/// The list of enemies to sample from
/// </param>
/// <param name="attack">
/// The object used to attack
/// </param>
/// <param name="firingPoints"></param>
public virtual void Launch(List<Targetable> enemies, GameObject attack, Transform[] firingPoints)
{
int count = enemies.Count;
int currentFiringPointIndex = 0;
int firingPointLength = firingPoints.Length;
for (int i = 0; i < count; i++)
{
Targetable enemy = enemies[i];
Transform firingPoint = firingPoints[currentFiringPointIndex];
currentFiringPointIndex = (currentFiringPointIndex + 1) % firingPointLength;
var poolable = Poolable.TryGetPoolable<Poolable>(attack);
if (poolable == null)
{
return;
}
Launch(enemy, poolable.gameObject, firingPoint);
}
}
/// <summary>
/// Gets a instance of attack from the Pool and Launches it
/// </summary>
/// <param name="enemy">
/// The enemy launcher is attacking
/// </param>
/// <param name="attack">
/// The object used to attack the enemy
/// </param>
/// <param name="firingPoints"></param>
public virtual void Launch(Targetable enemy, GameObject attack, Transform[] firingPoints)
{
var poolable = Poolable.TryGetPoolable<Poolable>(attack);
if (poolable == null)
{
return;
}
Launch(enemy, poolable.gameObject, GetRandomTransform(firingPoints));
}
/// <summary>
/// Sets up a particle system to provide aiming feedback
/// </summary>
/// <param name="particleSystemToPlay">
/// The Particle system to fire
/// </param>
/// <param name="origin">
/// The position of the particle system
/// </param>
/// <param name="lookPosition">
/// The direction the particle system is looking
/// </param>
public void PlayParticles(ParticleSystem particleSystemToPlay, Vector3 origin, Vector3 lookPosition)
{
if (particleSystemToPlay == null)
{
return;
}
particleSystemToPlay.transform.position = origin;
particleSystemToPlay.transform.LookAt(lookPosition);
particleSystemToPlay.Play();
}
/// <summary>
/// Gets a random transform from a list
/// </summary>
/// <param name="launchPoints">
/// The list of transforms to use
/// </param>
public Transform GetRandomTransform(Transform[] launchPoints)
{
int index = Random.Range(0, launchPoints.Length);
return launchPoints[index];
}
}
}
10行目 : 「public abstract void Launch(Targetable enemy, GameObject attack, Transform firingPoint);」は砲撃処理を作成する関数の定義を行っている。ILauncher で定義を行っているため、抽象クラスであるこのクラスでの定義は不要だと思うのだが・・・。
22行目 : 「public virtual void Launch(List<Targetable> enemies, GameObject attack, Transform[] firingPoints)」は複数の敵に対して複数の砲撃を行っている。内容としては敵の数のループを行い該当するインデックスの砲撃地点からそれぞれ単体に対して砲撃をしている。
51行目 : 「public virtual void Launch(Targetable enemy, GameObject attack, Transform[] firingPoints)」は単体の敵に対して複数の砲撃を行っている。内容としては敵に対して砲撃地点から砲撃をしている。
73行目 : 「public void PlayParticles(ParticleSystem particleSystemToPlay, Vector3 origin, Vector3 lookPosition)」は ParticleSystem の実行を行っている。内容としては ParticleSystem が設定されていれば、引数の origin の地点で ParticleSystem を実行している。
90行目 : 「public Transform GetRandomTransform(Transform[] launchPoints」は引数で与えられた Transform からランダムに一つを選択している。