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

Unity チュートリアルのタワーディフェンステンプレートを触ってみる(37)では NavigationNodes に追加した Node で作成される AreaMeshCreator の解説を行った。今回は SingleTowerPlacementArea とTowerPlacementGrid のインタフェースである IPlacementArea の解説を行っていく。
1.タワーディフェンステンプレートのステージの設定編 – IPlacementArea.cs
IPlacementArea.cs について稚拙ながら解説
「IPlacementArea.cs」は「Assets/Scripts/TowerDefense/Towers/Placement/IPlacementArea.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 using Core.Utilities;
using UnityEngine;
namespace TowerDefense.Towers.Placement
{
/// <summary>
/// An interface for a placement area that can contain a tower
/// </summary>
public interface IPlacementArea
{
/// <summary>
/// Gets this object's transform
/// </summary>
Transform transform { get; }
/// <summary>
/// Calculates the grid position from a given world position, offset to center for a specific size object
/// </summary>
IntVector2 WorldToGrid(Vector3 worldPosition, IntVector2 sizeOffset);
/// <summary>
/// Calculates the snapped world position from a given grid position
/// </summary>
Vector3 GridToWorld(IntVector2 gridPosition, IntVector2 sizeOffset);
/// <summary>
/// Gets whether an object of a given size would fit on this grid at the given location
/// </summary>
/// <param name="gridPos">The grid location</param>
/// <param name="size">The size of the item</param>
/// <returns>True if the item would fit at <paramref name="gridPos"/></returns>
TowerFitStatus Fits(IntVector2 gridPos, IntVector2 size);
/// <summary>
/// Occupy the given space on this placement area
/// </summary>
/// <param name="gridPos">The grid location</param>
/// <param name="size">The size of the item</param>
void Occupy(IntVector2 gridPos, IntVector2 size);
/// <summary>
/// Clear the given space on this placement area
/// </summary>
/// <param name="gridPos">The grid location</param>
/// <param name="size">The size of the item</param>
void Clear(IntVector2 gridPos, IntVector2 size);
}
public static class PlacementAreaExtensions
{
/// <summary>
/// Snaps a given world positionn to this grid
/// </summary>
public static Vector3 Snap(this IPlacementArea placementArea, Vector3 worldPosition, IntVector2 sizeOffset)
{
// Calculate the nearest grid location and then change that back to world space
return placementArea.GridToWorld(placementArea.WorldToGrid(worldPosition, sizeOffset), sizeOffset);
}
}
}
14行目 : 「Transform transform { get; }」は transform の get が必要であることを指している。
19行目 : 「IntVector2 WorldToGrid(Vector3 worldPosition, IntVector2 sizeOffset);」は WorldToGrid 関数が必要であることを指している。内容としては世界座標からグリッドに変換する関数である。
24行目 : 「Vector3 GridToWorld(IntVector2 gridPosition, IntVector2 sizeOffset);」は GridToWorld 関数が必要であることを指している。内容としてはグリッドを世界座標に変換する関数である
32行目 : 「TowerFitStatus Fits(IntVector2 gridPos, IntVector2 size);」は Fits 関数が必要であることを指している。内容としてはグリッドの位置とタワーのサイズを比較しグリッド内に収まっているか判定を行う関数である。
39行目 : 「void Occupy(IntVector2 gridPos, IntVector2 size);」は Occupy 関数が必要であることを指している。内容としてはグリッドの占有を行う関数である。
46行目 : 「void Clear(IntVector2 gridPos, IntVector2 size);」は Clear 関数が必要であることを指している。内容としてはグリッドのクリアを行う関数である。
49行目 : 「public static class PlacementAreaExtensions」は PlacementAreaExtensions という名称の内部クラスである。今回のタワーディフェンスチュートリアルでは使用していないため省略させていただく。
54行目 : 「public static Vector3 Snap(this IPlacementArea placementArea, Vector3 worldPosition, IntVector2 sizeOffset)」は今回のタワーディフェンスチュートリアルでは使用していないため省略させていただく。