// 上下左右斜めのグリッドに対してリンクを作成する public override void MakeRelation(AstarCell parent) { parent.Related.Clear(); if (parent.CellType == AstarCell.Type.Block) { return; } int x = (int)parent.Position.x; int y = (int)parent.Position.y; for (int dx = -1; dx < 2; ++dx) { for (int dy = -1; dy < 2; ++dy) { if (dx == 0 && dy == 0) { continue; } float nx = x + dx * this.TileSize; float ny = y + dy * this.TileSize; Vector2 n = new Vector2(nx, ny); if (!this.MapRect.Contains(n)) { continue; } var cell = this.CellMap(n); if (cell != null && cell.CellType != AstarCell.Type.Block) { parent.AddRelated(this.CellMap(n), parent.Heuristic(cell)); } } } parent.RelationBuilt = true; }
public override void MakeRelation(AstarCell cell) { if (this.logic.cells == null) { Debug.LogError("this.logic.cells is null"); throw new System.InvalidOperationException(); } cell.Related.Clear(); for (int iy = -1; iy <= 1; ++iy) { float y = cell.Position.y + iy * sin60; for (int ix = -1; ix <= 1; ix += 2) { float x; if (iy == 0) { x = cell.Position.x + ix; } else { x = cell.Position.x + ix * cos60; } var c = FindCell(new Vector2(x, y)); if (c != cell && c != null) { if (!cell.Contains(c)) { cell.AddRelated(c, c.MoveCost); } if (!c.Contains(cell)) { c.AddRelated(cell, cell.MoveCost); } } } } cell.RelationBuilt = true; }
// 到達可能なノードを全ノードからレイキャストして調べる public void setGridRelatedSearchRaycast(AstarCell parent, bool newCell = false) { foreach (var cell in this.logic.cells.Where(c => c.IsValidCell())) { if (cell == parent) { continue; } var fromCell = cell.Find(parent); if (fromCell.cell != null) { // 相手から自分が見えている場合 if (!parent.Contains(cell)) { parent.AddRelated(cell, fromCell.cost); } continue; } // raycast float cost = 0; var prevPos = parent.Position; if (!this.GridMode) { // 中間グリッドを飛ばして最短距離で結ぶ場合 RaycastCell(parent.Position, cell.Position, AstarCell.Type.Removed, (ix, iy) => { var cellType = this.cellType[ix, iy]; if (cellType != AstarCell.Type.Removed) { // 何かあった if (cellType != AstarCell.Type.Block) { // ブロックもしくは圏外ではない場合 var rcell = this.Cell(ix, iy); if ((rcell.Position - cell.Position).sqrMagnitude < 0.1f) { // 見つかった! if (!parent.Contains(cell)) { cost = (cell.Position - parent.Position).magnitude; parent.AddRelated(cell, cost); if (newCell && !cell.Contains(parent)) { cell.AddRelated(parent, cost); } } } } return(true); } else { return(false); } }); } else { // グリッドモード RaycastCell(parent.Position, cell.Position, AstarCell.Type.NoIgnore, (ix, iy) => { var rcell = this.Cell(ix, iy); // グリッドモードではコストを計算する var nowpos = rcell.Position; cost += (nowpos - prevPos).magnitude; prevPos = nowpos; if (rcell.CellType != AstarCell.Type.Removed) { // 何かあった if (rcell.CellType != AstarCell.Type.Block) { // ブロックもしくは圏外ではない場合 if (rcell == cell) { // 見つかった! if (!parent.Contains(cell)) { if (!this.GridMode) { cost = (cell.Position - parent.Position).magnitude; } parent.AddRelated(cell, cost); if (newCell && !cell.Contains(parent)) { cell.AddRelated(parent, cost); } } } } return(true); } else { return(false); } }); } } }