/// <summary> /// Selects a cell /// </summary> /// <param name="cell">Cell to select</param> public void ProcessSelection(CellController cell) { if (this.IsOccupied(cell.Coordinate)) { var unit = this.GetUnit(cell); this.ProcessSelection(unit); return; } if (this.selection != null && this.TurnController.UnitMoved == null) { if (this.selection.CanMove(cell.Coordinate)) { this.Move(this.selection.Coordinate, cell.Coordinate); this.TurnController.HandleUnitMove(this.selection); var temp = this.selection; this.CancelSelection(); this.ProcessSelection(temp); } else { this.CancelSelection(); } } }
/// <summary> /// Initialize the board controller /// </summary> public void Start() { this.Cells = new CellController[(this.GridSize * 2) + 1, (this.GridSize * 2) + 1]; this.Units = new Unit[(this.GridSize * 2) + 1, (this.GridSize * 2) + 1]; for (int i = -1 * this.GridSize; i <= this.GridSize; i++) { for (int j = -1 * this.GridSize; j <= this.GridSize; j++) { if (HexDistance.Distance(0, 0, i, j) <= this.GridSize) { float x = i; if (Math.Abs(j) % 2 == 1) { x = i + 0.5f; } var hex = (CellController)Instantiate(this.HexCellPrefab); hex.transform.position = new Vector3(x * this.XScale, this.YPosition, j * this.ZScale); CellCoordinate cellCoord = WorldToGameConverter.ConvertWorldToCell(hex.transform.position, this.ZScale); hex.gameObject.name = "(" + cellCoord.X + ", " + cellCoord.Y + ")"; this.SetCell(cellCoord.X, cellCoord.Y, hex); CellController cellController = hex.GetComponent <CellController>(); cellController.BoardController = this; cellController.Coordinate = cellCoord; } } } }
/// <summary> /// Highlights a list of cells /// </summary> /// <param name="list">List of coordinates</param> /// <param name="mode">Cell highlight mode</param> public void HighlightCells(IList <CellCoordinate> list, CellHighlightMode mode) { List <CellController> cells = new List <CellController>(); foreach (CellCoordinate c in list) { CellController cell = this.GetCell(c); if (cell != null) { cells.Add(cell); } } this.HighlightCells(cells, mode); }
/// <summary> /// Spawns a unit /// </summary> /// <param name="unitType">Type of unit to spawn</param> /// <param name="coord">Location to spawn it</param> /// <param name="owner">Owner of the unit</param> public void SpawnUnit(UnitType unitType, CellCoordinate coord, PlayerController owner) { Unit prefab; switch (unitType) { case UnitType.Cube: prefab = this.CubeUnitPrefab; break; case UnitType.Pyramid: prefab = this.PyramidUnitPrefab; break; case UnitType.Arrow: prefab = this.ArrowUnitPrefab; break; default: prefab = null; break; } CellController cell = this.GetCell(coord.X, coord.Y); if (prefab != null && cell != null && !this.IsOccupied(coord)) { Vector3 cellLoc = cell.transform.position; var unit = (Unit)Instantiate(prefab); unit.transform.position = new Vector3(cellLoc.x, unit.transform.position.y, cellLoc.z); unit.BoardController = this; unit.Coordinate = coord; unit.Mesh.renderer.material.color = owner.Color; unit.LookAt(new Vector3(0f, 0f, 0f), true); this.SetUnit(coord.X, coord.Y, unit); owner.AddUnit(unit); } }
/// <summary> /// Gets any units on a given cell /// </summary> /// <param name="cell">Cell to inspect</param> /// <returns>Any units standing on the cell</returns> private Unit GetUnit(CellController cell) { return(this.GetUnit(cell.Coordinate)); }
/// <summary> /// Assigns a cell controller to a certain coordinate /// </summary> /// <param name="coord">Cell coordinate</param> /// <param name="cell">Cell to assign</param> private void SetCell(CellCoordinate coord, CellController cell) { this.SetCell(coord.X, coord.Y, cell); }
/// <summary> /// Assigns a cell controller to a certain coordinate /// </summary> /// <param name="x">X coordinate</param> /// <param name="y">Y coordinate</param> /// <param name="cell">Cell to assign</param> private void SetCell(int x, int y, CellController cell) { this.Cells[x + this.GridSize, y + this.GridSize] = cell; }