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

Unity チュートリアルのタワーディフェンステンプレートを触ってみる(5)、(6)、(7) と Tower.cs スクリプトの解説を行ってきた。今回は引き続きUnity チュートリアルのタワーディフェンステンプレートを触ってみる(4)に記載している通り 「DamageCollider.cs」 がその前に継承関係にある「DamageZone.cs」の解説を行っていく。
「DamageCollider.cs」は「DamageZone.cs」を継承している。そのため今回は「DamageCollider.cs」解説する前に「DamageZone.cs」について解説を行っていく。
1.タワー編 – DamageZone.cs
DamageZone.cs について稚拙ながら解説
「DamageZone.cs」は「Assets/Scripts/ActionGameFramework/Health/DamageZone.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 using Core.Health;
using UnityEngine;
namespace ActionGameFramework.Health
{
/// <summary>
/// Damage zone - an area that receives damage and gives it to the damageable behaviour
/// Abstract base class because the actual damage mechanic can be based on Collisions or Triggers or some other mechanism
/// </summary>
[RequireComponent(typeof(Collider))]
public abstract class DamageZone : MonoBehaviour
{
/// <summary>
/// Allow the user to define the damageable behaviour that this zone corresponds to.
/// This allows multiple damage zones to reference one damageable behaviour
/// allowing locational damage. e.g. headshots.
/// </summary>
[Tooltip("If this is empty, DamageZone will try to use a DamageableBehaviour on the same object.")]
public DamageableBehaviour damageableBehaviour;
/// <summary>
/// Allow scaling of the damage. This allows different zones to take different damage from same Damager. e.g. headshots
/// </summary>
public float damageScale = 1f;
/// <summary>
/// Scales the damage.
/// </summary>
/// <returns>The damage.</returns>
/// <param name="damage">Damage.</param>
protected float ScaleDamage(float damage)
{
return damageScale * damage;
}
/// <summary>
/// Looks for the damageableBehaviour if it is not already assigned
/// It may be assigned in editor or from a previous LazyLoad() call
/// </summary>
protected void LazyLoad()
{
if (damageableBehaviour != null)
{
return;
}
damageableBehaviour = GetComponent<DamageableBehaviour>();
}
}
}
10行目 : 「[RequireComponent(typeof(Collider))]」では必須のコンポーネントを定義している。必須のコンポーネントについては「typeof(Collider)」で指定しており、ここでは Collider を必ず定義しなければならないという定型文となっている。
11行目 : 「public abstract class DamageZone : MonoBehaviour」の abstract ではこのクラスが抽象クラスであり、このクラス利用するためには具象クラスが必要となることを表している。
抽象クラス
『ソフトウェア工学における抽象型(ちゅうしょうがた、英: abstract type)は、プログラマが宣言する nominative type system における型である。何らかの宣言された派生型のメンバーも共有するメンバーを含む抽象メソッドやプロパティ[1]を含むこともあるし、含まないこともある。多くのオブジェクト指向プログラミング言語では、抽象型を抽象基底クラス (abstract base class)、インタフェース (interface)、Trait、Mixin、flavors、rolesなどと呼ぶ。これらの名称はそれぞれ異なる言語での抽象型の実装を指している。本項目ではこれを総称して抽象クラス (abstract class) と呼ぶ。
抽象クラスの最大の特徴は、オブジェクト指向プログラミングをよりオブジェクト指向的に保つことと、その性質上それが未完成である点である。』
感覚的に説明をすると Unity での Instantiate や new を行うことができないクラスであり、必ず継承先となるクラスが存在する位の感覚でいると良いだろう。
具象クラス
具象クラスは抽象クラスと異なり、 Unity での Instantiate や new を行うことができるクラスであるというくらいの感覚でいるとよいだろう。
18行目 : 「[Tooltip(“If this is empty, DamageZone will try to use a DamageableBehaviour on the same object.”)] 」では Inspector 上でこのグローバル変数名にカーソルをあわせた際にツールチップが表示される様になる定型文である。文章の内容は “これが空の場合、DamageZone は同じオブジェクトに対して DamageableBehaviour を使用しようとします。“とのこと。
31行目 : 「protected float ScaleDamage(float damage)」ではダメージスケールの計算を行っている。FPS などのゲームでヘッドショットでダメージを変えたり、属性によって変えるなどもあり得るだろう。
40行目 : 「protected void LazyLoad()」 では遅延読込を行っている。具象クラスの初期化が終わらないと damageableBehaviour は設定されないため、具象クラスの初期化後にこの関数を呼び出すことで対応を行っている。