/// <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> /// Returns whether the unit can move to the given coordinate /// </summary> /// <param name="coord">Coordinate to check</param> /// <returns>Whether or not the unit can move to the coordinate</returns> public virtual bool CanMove(CellCoordinate coord) { if (HexDistance.Distance(this.Coordinate, coord) <= this.MaxMoveRange && !this.BoardController.IsOccupied(coord)) { return(true); } return(false); }
/// <summary> /// Determines whether or not an attack against the given unit is possible /// </summary> /// <param name="unit">Unit to attack</param> /// <returns>Whether or not the unit can be attacked</returns> public virtual bool CanAttack(Unit unit) { if (unit != null && unit != this && unit.Owner != this.Owner && HexDistance.Distance(this.Coordinate, unit.Coordinate) <= this.AttackRange) { return(true); } return(false); }
/// <summary> /// Determines whether or not healing the given unit is possible /// </summary> /// <param name="unit">Unit to heal</param> /// <returns>Whether or not the unit can be healed</returns> public bool CanHeal(Unit unit) { if (unit != null && unit != this && unit.Owner == this.Owner && HexDistance.Distance(this.Coordinate, unit.Coordinate) <= this.HealRange) { return(true); } return(false); }
/// <summary> /// Gets the cells neighbors /// </summary> /// <returns>List of neighbors</returns> public List <CellCoordinate> GetNeighbors() { List <CellCoordinate> list = new List <CellCoordinate>(); for (int i = -1 * this.BoardController.GridSize; i <= this.BoardController.GridSize; i++) { for (int j = -1 * this.BoardController.GridSize; j <= this.BoardController.GridSize; j++) { if (HexDistance.Distance(0, 0, i, j) <= this.BoardController.GridSize && HexDistance.Distance(this.Coordinate.X, this.Coordinate.Y, i, j) == 1) { list.Add(new CellCoordinate(i, j)); } } } return(list); }