//get a direct distance, regardless of the walkable state public int GetDistance(Tile tile1, Tile tile2) { if (GridManager.GetTileType() == _TileType.Hex) { float x = Mathf.Abs(tile1.x - tile2.x); float y = Mathf.Abs(tile1.y - tile2.y); float z = Mathf.Abs(tile1.z - tile2.z); return((int)((x + y + z) / 2)); } else { float tileSize = GridManager.GetTileSize() * GridManager.GetGridToTileSizeRatio(); Vector3 pos1 = tile1.GetPos(); Vector3 pos2 = tile2.GetPos(); int dx = (int)Mathf.Round(Mathf.Abs(pos1.x - pos2.x) / tileSize); int dz = (int)Mathf.Round(Mathf.Abs(pos1.z - pos2.z) / tileSize); if (GridManager.EnableDiagonalNeighbour()) { int min = Mathf.Min(dx, dz); int max = Mathf.Max(dx, dz); return(min + (max - min)); } return(dx + dz); } }
public void Init() { for (int i = 0; i < tileList.Count; i++) { tileList[i].Init(); } GridManager gridManager = GridManager.GetInstance(); //setup the neighbour of each tile if (gridManager.tileType == _TileType.Square) { float distTH = gridManager.tileSize * gridManager.gridToTileRatio * 1.1f; for (int i = 0; i < tileList.Count; i++) { Tile tile = tileList[i]; tile.aStar = new TileAStar(tile); List <Tile> neighbourList = new List <Tile>(); int nID = i + 1; if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH) { neighbourList.Add(tileList[nID]); } nID = i + gridManager.length; if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH) { neighbourList.Add(tileList[nID]); } nID = i - 1; if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH) { neighbourList.Add(tileList[nID]); } nID = i - gridManager.length; if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH) { neighbourList.Add(tileList[nID]); } //tile.aStar.straightNeighbourCount=neighbourList.Count; //diagonal neighbour, not in used if (GridManager.EnableDiagonalNeighbour()) { nID = (i + 1) + gridManager.length; if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH * 1.5f) { neighbourList.Add(tileList[nID]); } nID = (i + 1) - gridManager.length; if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH * 1.5f) { neighbourList.Add(tileList[nID]); } nID = (i - 1) + gridManager.length; if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH * 1.5f) { neighbourList.Add(tileList[nID]); } nID = (i - 1) - gridManager.length; if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH * 1.5f) { neighbourList.Add(tileList[nID]); } } tile.aStar.SetNeighbourList(neighbourList); } } else if (gridManager.tileType == _TileType.Hex) { float distTH = GridGenerator.spaceZHex * gridManager.tileSize * gridManager.gridToTileRatio * 1.1f; for (int i = 0; i < tileList.Count; i++) { Tile tile = tileList[i]; tile.aStar = new TileAStar(tile); List <Tile> neighbourList = new List <Tile>(); int nID = i + 1; if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH) { neighbourList.Add(tileList[nID]); } nID = i + gridManager.length; if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH) { neighbourList.Add(tileList[nID]); } nID = i + gridManager.length - 1; if (nID < tileList.Count && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH) { neighbourList.Add(tileList[nID]); } nID = i - 1; if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH) { neighbourList.Add(tileList[nID]); } nID = i - gridManager.length; if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH) { neighbourList.Add(tileList[nID]); } nID = i - gridManager.length + 1; if (nID >= 0 && Vector3.Distance(tile.GetPosG(), tileList[nID].GetPosG()) <= distTH) { neighbourList.Add(tileList[nID]); } tile.aStar.SetNeighbourList(neighbourList); //tile.aStar.straightNeighbourCount=neighbourList.Count; } } //setup the wall for (int i = 0; i < tileList.Count; i++) { tileList[i].InitWall(); } if (GridManager.EnableDiagonalNeighbour()) { for (int i = 0; i < tileList.Count; i++) { tileList[i].CheckDiagonal(); } } //setup the cover if (GameControl.EnableCover()) { for (int i = 0; i < tileList.Count; i++) { CoverSystem.InitCoverForTile(tileList[i]); } } }