//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 static void InitCoverForTile(Tile tile) { List <Tile> neighbourList = tile.GetNeighbourList(); List <Cover> coverList = new List <Cover>(); for (int i = 0; i < tile.GetNeighbourList().Count; i++) { Vector3 dir = (neighbourList[i].GetPos() - tile.GetPos()).normalized; float dist = GridManager.GetTileSize() * GridManager.GetGridToTileSizeRatio() * .75f; LayerMask mask = 1 << TBTK.GetLayerObstacleFullCover() | 1 << TBTK.GetLayerObstacleHalfCover(); RaycastHit hit; if (Physics.Raycast(tile.GetPos(), dir, out hit, dist, mask)) { Cover cover = new Cover(); cover.angle = Mathf.Round(Utilities.Vector2ToAngle(new Vector2(dir.x, dir.z))); if (GridManager.GetTileType() == _TileType.Square) //when diagonal neighbour is enabled, avoid adding cover to diagonal neighbour { if (cover.angle % 90 != 0) { continue; } } int layer = hit.transform.gameObject.layer; if (layer == TBTK.GetLayerObstacleFullCover()) { cover.type = _CoverType.Full; Debug.DrawLine(tile.GetPos(), tile.GetPos() + dir * dist, Color.red, 2); } else if (layer == TBTK.GetLayerObstacleHalfCover()) { cover.type = _CoverType.Half; Debug.DrawLine(tile.GetPos(), tile.GetPos() + dir * dist, Color.white, 2); } if (GridManager.GetTileType() == _TileType.Square) { cover.overlayPos = tile.GetPos() + dir * dist * 0.4f; } else if (GridManager.GetTileType() == _TileType.Hex) { cover.overlayPos = tile.GetPos() + dir * dist * 0.35f; } float angleY = cover.angle + 90; if (cover.angle == 30) { angleY = cover.angle + 30; } else if (cover.angle == 150) { angleY = cover.angle - 30; } else if (cover.angle == 210) { angleY = cover.angle + 30; } else if (cover.angle == 330) { angleY = cover.angle - 30; } cover.overlayRot = Quaternion.Euler(0, angleY, 0); coverList.Add(cover); } } tile.coverList = coverList; }
public void Init() { if (init) { return; } init = true; thisT = transform; Transform deploymentIndicator = null; Transform moveableIndicator = null; Transform hostileIndicator = null; Transform abRangeIndicator = null; Transform abTargetIndicator = null; if (GridManager.GetTileType() == _TileType.Hex) { deploymentIndicator = hexDeployment; moveableIndicator = hexMoveable; hostileIndicator = hexHostile; abRangeIndicator = hexAbRange; abTargetIndicator = hexAbTarget; cursorIndicatorF = (Transform)Instantiate(hexCursorF); cursorIndicatorH = (Transform)Instantiate(hexCursorH); selectIndicator = (Transform)Instantiate(hexSelected); } else if (GridManager.GetTileType() == _TileType.Square) { deploymentIndicator = sqDeployment; moveableIndicator = sqMoveable; hostileIndicator = sqHostile; abRangeIndicator = sqAbRange; abTargetIndicator = sqAbTarget; cursorIndicatorF = (Transform)Instantiate(sqCursorF); cursorIndicatorH = (Transform)Instantiate(sqCursorH); selectIndicator = (Transform)Instantiate(sqSelected); } cursorIndicatorF.parent = thisT; cursorIndicatorH.parent = thisT; selectIndicator.parent = thisT; for (int i = 0; i < 20; i++) { deploymentIndicatorList.Add((Transform)Instantiate(deploymentIndicator)); deploymentIndicatorList[i].gameObject.SetActive(false); deploymentIndicatorList[i].parent = thisT; } for (int i = 0; i < 20; i++) { moveableIndicatorList.Add((Transform)Instantiate(moveableIndicator)); moveableIndicatorList[i].gameObject.SetActive(false); moveableIndicatorList[i].parent = thisT; } for (int i = 0; i < 10; i++) { hostileIndicatorList.Add((Transform)Instantiate(hostileIndicator)); hostileIndicatorList[i].gameObject.SetActive(false); hostileIndicatorList[i].parent = thisT; } for (int i = 0; i < 20; i++) { abRangeIndicatorList.Add((Transform)Instantiate(abRangeIndicator)); abRangeIndicatorList[i].gameObject.SetActive(false); abRangeIndicatorList[i].parent = thisT; } for (int i = 0; i < 20; i++) { abTargetIndicatorList.Add((Transform)Instantiate(abTargetIndicator)); abTargetIndicatorList[i].gameObject.SetActive(false); abTargetIndicatorList[i].parent = thisT; } if (TurnControl.GetTurnMode() == _TurnMode.FactionPerTurn) { //create the moved indicator for (int i = 0; i < 10; i++) { movedIndicatorList.Add((Transform)Instantiate(movedIndicator)); movedIndicatorList[i].gameObject.SetActive(false); movedIndicatorList[i].parent = thisT; } } if (GameControl.EnableFogOfWar()) { Transform fogObj = null; if (GridManager.GetTileType() == _TileType.Hex) { fogObj = hexFogObj; } else if (GridManager.GetTileType() == _TileType.Square) { fogObj = sqFogObj; } List <Tile> tileList = GridManager.GetTileList(); for (int i = 0; i < tileList.Count; i++) { //if(!tileList[i].walkable) continue; tileList[i].SetFogOfWarObj((Transform)Instantiate(fogObj)); } } if (GameControl.EnableCover()) { float scaleOffset = GridManager.GetTileType() == _TileType.Hex ? 0.5f : 0.8f; float tileSize = GridManager.GetTileSize(); for (int i = 0; i < 5; i++) { coverOverlayHList.Add((Transform)Instantiate(coverOverlayH)); coverOverlayFList.Add((Transform)Instantiate(coverOverlayF)); coverOverlayHList[i].localScale *= tileSize * scaleOffset; coverOverlayHList[i].parent = thisT; coverOverlayHList[i].gameObject.SetActive(false); coverOverlayFList[i].localScale *= tileSize * scaleOffset; coverOverlayFList[i].parent = thisT; coverOverlayFList[i].gameObject.SetActive(false); } } }