Exemple #1
0
 /// <summary>
 /// Moves to the given coordinate and update the unit's world position
 /// </summary>
 /// <param name="coord">Cell coordinate</param>
 /// <param name="worldPosition">World position</param>
 public virtual void MoveTo(CellCoordinate coord, Vector3 worldPosition)
 {
     GameLogger.Instance.AddMove(ActionRecord.MoveAction(this, this.Coordinate, coord));
     this.Coordinate     = coord;
     this.AnimationState = AnimationState.Move;
     this.targetPosition = worldPosition;
 }
Exemple #2
0
        /// <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;
                    }
                }
            }
        }
Exemple #3
0
        /// <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);
        }
Exemple #4
0
        /// <summary>
        /// Finds out if the given cell is occupied
        /// </summary>
        /// <param name="coord">Coordinate of the cell</param>
        /// <returns>Whether or not the cell is occupied</returns>
        public bool IsOccupied(CellCoordinate coord)
        {
            if (this.GetUnit(coord.X, coord.Y) != null)
            {
                return(true);
            }

            return(false);
        }
Exemple #5
0
        /// <summary>
        /// Move a unit
        /// </summary>
        /// <param name="source">Cell to move unit from</param>
        /// <param name="target">Cell to move unit to</param>
        public void Move(CellCoordinate source, CellCoordinate target)
        {
            Unit unit = this.GetUnit(source.X, source.Y);

            if (unit != null && unit.CanMove(target))
            {
                this.SetUnit(target.X, target.Y, unit);
                this.SetUnit(source.X, source.Y, null);
                Vector3 cellPosition = this.GetCell(target.X, target.Y).transform.position;
                Vector3 unitPosition = new Vector3(cellPosition.x, unit.transform.position.y, cellPosition.z);
                unit.MoveTo(target, unitPosition);
                this.StartCoroutine(this.WaitForActionToComplete(unit));
            }
        }
Exemple #6
0
        /// <summary>
        /// Computes the distance between two coordinates
        /// </summary>
        /// <param name="c1">First coordinate</param>
        /// <param name="c2">Second coordinate</param>
        /// <returns>Distance between the coordinates in hex cells</returns>
        public static int Distance(CellCoordinate c1, CellCoordinate c2)
        {
            int dx = c2.X - c1.X;
            int dy = c2.Y - c1.Y;

            if (Math.Sign(dx) == Math.Sign(dy))
            {
                return(Math.Abs(dx + dy));
            }
            else
            {
                return(Math.Max(Math.Abs(dx), Math.Abs(dy)));
            }
        }
Exemple #7
0
        /// <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);
            }
        }
Exemple #8
0
 /// <summary>
 /// Sets the unit on a given cell
 /// </summary>
 /// <param name="coord">Coordinate to set</param>
 /// <param name="unit">Unit to place</param>
 private void SetUnit(CellCoordinate coord, Unit unit)
 {
     this.SetUnit(coord.X, coord.Y, unit);
 }
Exemple #9
0
 /// <summary>
 /// Gets any units on a given cell
 /// </summary>
 /// <param name="coord">Coordinate to inspect</param>
 /// <returns>Any units standing on the cell</returns>
 private Unit GetUnit(CellCoordinate coord)
 {
     return(this.GetUnit(coord.X, coord.Y));
 }
Exemple #10
0
 /// <summary>
 /// Gets the cell controller from the given coordinate
 /// </summary>
 /// <param name="coord">Cell coordinate</param>
 /// <returns>Cell controller for the coordinate</returns>
 private CellController GetCell(CellCoordinate coord)
 {
     return(this.GetCell(coord.X, coord.Y));
 }
Exemple #11
0
 /// <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);
 }
Exemple #12
0
 /// <summary>
 /// Moves to the given coordinate and update the unit's world position
 /// </summary>
 /// <param name="coord">Cell coordinate</param>
 /// <param name="worldPosition">World position</param>
 public override void MoveTo(CellCoordinate coord, Vector3 worldPosition)
 {
     this.LookAt(worldPosition);
     base.MoveTo(coord, worldPosition);
 }