前提
この質問で掲載するコードはこちらのブログ運営者様のものです(ソースコードのライセンスはWTFPLです)。
Unityのバージョンは問わず、回答をお待ちしております。
また、本質問はAstarアルゴリズムを使用するために必要となる、マス目の管理に関する質問です。
もし、可能であれば「なぜAstarアルゴリズムではそれが必要か」と言う形で回答いただければ幸いです(必須ではない)。
お手数をおかけしますがよろしくお願い申し上げます。
質問
上記のブログ記事で紹介されているソースコード中、ユーザー定義型の計算周りで不明な点がありました。
operator +(あるいは-)は演算子のオーバーロードであり、ユーザー定義型と組み込み型との計算を可能にするものであるということは検索等で知ったところですが、以下の二行が具体的にどのような計算に用いられているのか(あるいはどこで、どういったタイミングでその計算が行われているのか)分からず困っています。
プログラミング初心者のため、稚拙な質問になってしまい申し訳ございませんが、お教えいただければ幸いです。よろしくお願い申し上げます。
csharp
1 public static Coord operator +(Coord a, Coord b) { return new Coord(a.x + b.x, a.y + b.y); } 2 public static Coord operator -(Coord a, Coord b) { return new Coord(a.x - b.x, a.y - b.y); }
該当のソースコード
csharp
1using System.Collections.Generic; 2using UnityEngine; 3 4[System.Serializable] 5public class AStarGrid 6{ 7 public enum GroundType 8 { 9 NotRoad, 10 Road, 11 } 12 13 /// <summary> 14 /// 座標 15 /// </summary> 16 [System.Serializable] 17 public struct Coord 18 { 19 public int x; 20 public int y; 21 22 public static Coord zero = new Coord(){ x = 0, y = 0 }; 23 public static Coord one = new Coord(){ x = 1, y = 1 }; 24 public static Coord left = new Coord(){ x = -1, y = 0 }; 25 public static Coord up = new Coord(){ x = 0, y = 1 }; 26 public static Coord right = new Coord(){ x = 1, y = 0 }; 27 public static Coord down = new Coord(){ x = 0, y = -1 }; 28 public static Coord operator +(Coord a, Coord b) { return new Coord(a.x + b.x, a.y + b.y); } 29 public static Coord operator -(Coord a, Coord b) { return new Coord(a.x - b.x, a.y - b.y); } 30 31 public float SqrMagnitude { get { return x * x + y * y; } } 32 public float Magnitude { get { return Mathf.Sqrt(SqrMagnitude); } } 33 34 public Coord(int x, int y) 35 { 36 this.x = x; 37 this.y = x; 38 } 39 } 40 41 /// <summary> 42 /// セル 43 /// </summary> 44 [System.Serializable] 45 public class Cell 46 { 47 public Coord coord; 48 public GroundType groundType; 49 } 50 51 [SerializeField] 52 private List<Cell> _cells; 53 public List<Cell> Cells { get { return _cells; } } 54 [SerializeField] 55 private int _columnCount; 56 public int ColumnCount { get { return _columnCount; } } 57 [SerializeField] 58 private int _rowCount; 59 public int RowCount { get { return _rowCount; } } 60 [SerializeField] 61 private Coord _startCellCoord; 62 public Cell StartCell { get { return GetCell(_startCellCoord); } set { _startCellCoord = value.coord; } } 63 [SerializeField] 64 private Coord _goalCellCoord; 65 public Cell GoalCell { get { return GetCell(_goalCellCoord); } set { _goalCellCoord = value.coord; } } 66 67 public AStarGrid(int columnCount, int rowCount) 68 { 69 _columnCount = columnCount; 70 _rowCount = rowCount; 71 _cells = new List<Cell>(); 72 for (int i = 0; i < columnCount * rowCount; i++) { 73 var column = Mathf.FloorToInt(i / rowCount); 74 var row = i % rowCount; 75 var coord = new Coord(){ x = column, y = row }; 76 _cells.Add(new Cell(){ coord = coord }); 77 } 78 } 79 80 /// <summary> 81 /// セルを取得する 82 /// </summary> 83 public Cell GetCell(Coord coord){ 84 return GetCell(coord.x, coord.y); 85 } 86 87 /// <summary> 88 /// セルを取得する 89 /// </summary> 90 public Cell GetCell(int x, int y) 91 { 92 if (IsValidCoord(x, y)) { 93 var index = x * _rowCount + y; 94 return _cells[index]; 95 } 96 else { 97 return null; 98 } 99 } 100 101 /// <summary> 102 /// 存在するセルか 103 /// </summary> 104 public bool IsValidCoord(int x, int y) 105 { 106 return x >= 0 && x < _columnCount && y >= 0 && y < _rowCount; 107 } 108 109 /// <summary> 110 /// 隣接するセルを取得する 111 /// </summary> 112 public List<Cell> GetAdjacences(int x, int y) 113 { 114 var adjacences = new List<Cell>(); 115 var offsets = new Coord[] { Coord.left, Coord.up, Coord.right, Coord.down }; 116 for (int i = 0; i < offsets.Length; i++) { 117 var cell = GetCell(x + offsets[i].x, y + offsets[i].y); 118 if (cell != null) { 119 adjacences.Add(cell); 120 } 121 } 122 return adjacences; 123 } 124}

回答2件
あなたの回答
tips
プレビュー