示例#1
0
        public bool IsValidAttackTarget(HexCell attackingUnit, HexCell target, int playerId)
        {
            if (!(attackingUnit is UnitCell unit))
            {
                return(false);
            }
            if (attackingUnit == null || attackingUnit.PlayerId != playerId)
            {
                return(false);
            }
            if (target == null || target.PlayerId == playerId)
            {
                return(false);
            }

            return(unit.GetStats().Range >= HexCoordinates.Distance(attackingUnit.Coordinates, target.Coordinates));
        }
示例#2
0
        public void UpdateAttackCursor(UnitCell unit, HexCoordinates coords)
        {
            var target = cells.GetCellAtPosition(coords);

            if (target == null)
            {
                ui.SetGameCursor(HexCursor.Invalid);
            }
            else if (cells.IsValidAttackTarget(unit, target, unit.PlayerId))
            {
                ui.SetGameCursor(HexCursor.Attack);
            }
            else
            {
                ui.SetGameCursor(HexCursor.Invalid);
            }
        }
        public void CmdMoveCell(Int16 playerID, Int16 cellID, Int16 targetX, Int16 targetZ)
        {
            if (!game.IsCurrentPlayer(playerID))
            {
                return;
            }

            var cell = game.Cells.GetCell(cellID);

            var target = new HexCoordinates(targetX, targetZ);
            var path   = game.Cells.GetPathWithValidityCheck(cell, target, ID);

            if (path != null)
            {
                game.MoveCell(cell, target);
            }
        }
示例#4
0
        public void UpdateBuildCursor(HexCoordinates coords)
        {
            if (projectedCell == null)
            {
                return;
            }

            projectedCell.SetCoordinates(coords);

            if (IsProjectedCellInPlacementGrid())
            {
                projectedCell.ShowAsValid();
            }
            else
            {
                projectedCell.ShowAsInvalid();
            }
        }
示例#5
0
        private void HandleBoardClick(HexCoordinates coords)
        {
            Clear();

            var cell = game.Cells.GetCellAtPosition(coords);

            if (cell != null)
            {
                if (cell is UnitCell u && u.PlayerId == player.ID)
                {
                    SetOwnUnitSelected(u);
                }

                selectedCell = cell;
                cell.SetSelected();
                game.UI.ShowCellInfo(cell);
            }
        }
示例#6
0
        public void Update()
        {
            input.HandleInput();

            var boardPosition = input.GetMousePositionOnBoard();
            var coords        = HexCoordinates.FromPosition(boardPosition);

            if (coords.Equals(currentCoords))
            {
                return;
            }
            currentCoords = coords;

            if (isPlayerTurn)
            {
                UpdateCursor(coords);
            }
        }
示例#7
0
        private static List <Vector3> ConstructPath(PathCell target, int maxDistance)
        {
            var path = new Stack <Vector3>();
            var cell = target;

            while (cell != null)
            {
                if (path.Count - 1 >= maxDistance)
                {
                    return(null);
                }

                path.Push(HexCoordinates.ToPosition(cell.X, cell.Z));
                cell = cell.PreviousCell;
            }

            return(path.ToList());
        }
示例#8
0
        private bool MoveIfValid(UnitCell unit, HexCoordinates coords)
        {
            if (unit.HasMoved)
            {
                return(false);
            }
            var path        = game.Cells.GetPathWithValidityCheck(unit, coords, player.ID);
            var isValidPath = path != null;

            if (isValidPath)
            {
                state = States.None;
                indicator.Clear();
                player.CmdMoveCell(
                    (Int16)player.ID, (Int16)unit.ID,
                    (Int16)coords.X, (Int16)coords.Z);
            }
            return(isValidPath);
        }
示例#9
0
        public List <Vector3> FindPath(HexCoordinates origin, HexCoordinates target, int maxDistance)
        {
            if (cells.IsPieceInCell(target))
            {
                return(null);
            }

            var distance      = HexCoordinates.Distance(origin, target);
            var pathableCells = GetCellsWithinDistance(origin, distance);

            foreach (var cell in pathableCells)
            {
                if (cell.X == target.X && cell.Z == target.Z)
                {
                    return(ConstructPath(cell, maxDistance));
                }
            }

            return(null);
        }
示例#10
0
        private void UpdateCursor(HexCoordinates coords)
        {
            switch (state)
            {
            case States.Building:
                indicator.UpdateBuildCursor(coords);
                break;

            case States.OwnUnitSelected:
                indicator.UpdateUnitCursor(unit, coords);
                break;

            case States.Moving:
                indicator.UpdateMoveCursor(unit, coords);
                break;

            case States.Attacking:
                indicator.UpdateAttackCursor(unit, coords);
                break;
            }
        }
示例#11
0
        public void UpdateUnitCursor(UnitCell unit, HexCoordinates coords)
        {
            if (unit == null)
            {
                return;
            }

            if (!unit.HasAttacked)
            {
                var target = cells.GetCellAtPosition(coords);
                if (target != null)
                {
                    cells.HidePath();
                    if (target.PlayerId == unit.PlayerId)
                    {
                        ui.SetGameCursor(HexCursor.Default);
                    }
                    else if (cells.IsValidAttackTarget(unit, target, unit.PlayerId))
                    {
                        ui.SetGameCursor(HexCursor.Attack);
                    }
                    else
                    {
                        ui.SetGameCursor(HexCursor.Invalid);
                    }
                    return;
                }
            }

            if (unit.HasMoved)
            {
                ui.SetGameCursor(HexCursor.Default);
            }
            else
            {
                UpdateMoveCursor(unit, coords);
            }
        }
示例#12
0
        public static bool IsValidPlacementDistance(int radius, HexCoordinates cellCoords, HexCoordinates origin)
        {
            var distance = (Mathf.Abs(cellCoords.X - origin.X)
                            + Mathf.Abs(cellCoords.Y - origin.Y)
                            + Mathf.Abs(cellCoords.Z - origin.Z)) / 2;

            return(distance <= radius);
        }
示例#13
0
 public bool IsPieceInCell(HexCoordinates coordinates)
 {
     return(AllCells.Any(c => c.IsPlaced && c.Coordinates.Equals(coordinates)));
 }
示例#14
0
 public HexCell GetCellAtPosition(HexCoordinates coordinates)
 {
     return(AllCells.FirstOrDefault(c => c.IsPlaced && c.Coordinates.Equals(coordinates)));
 }
示例#15
0
 public List <Vector3> GetPath(UnitCell unit, HexCoordinates targetCoords)
 {
     return(pathfinder.FindPath(unit.Coordinates, targetCoords, unit.GetStats().Speed));
 }
示例#16
0
 public void MoveCell(HexCell cell, HexCoordinates to)
 {
     Grid.RpcMoveCell((Int16)cell.ID, (Int16)to.X, (Int16)to.Z);
 }
示例#17
0
 public virtual void MoveTo(HexCoordinates coords)
 {
     SetCoordinates(coords);
 }
示例#18
0
 public static Vector3 ToPosition(HexCoordinates coordinates)
 {
     return(ToPosition(coordinates.X, coordinates.Z));
 }
示例#19
0
 public static int Distance(HexCoordinates c1, HexCoordinates c2)
 {
     return((Mathf.Abs(c1.X - c2.X)
             + Mathf.Abs(c1.Y - c2.Y)
             + Mathf.Abs(c1.Z - c2.Z)) / 2);
 }
示例#20
0
 public void SetCoordinates(HexCoordinates coords)
 {
     Coordinates        = coords;
     transform.position = HexCoordinates.ToPosition(coords);
 }