Exemplo n.º 1
0
        private void AddHexagonalNeighbours(List <Vector2Int> inNeighbours, Vector2Int inCurrentCoord, Vector2Int inParentCoord, FindPathInput inInput)
        {
            Direction movingDirection = HexagonGridHelper.GetDirection(inParentCoord, inCurrentCoord);

            Vector2Int nextCoord = HexagonGridHelper.GetNeighbourInDirection(inCurrentCoord, movingDirection);

            if (IsTileWalkable(nextCoord, inInput.AreaMask))
            {
                inNeighbours.Add(nextCoord);
            }

            Vector2Int aboveCoord = HexagonGridHelper.GetNeighbourInDirection(inCurrentCoord, HexagonGridHelper.GetRelativeDirection(movingDirection, 1));

            if (IsTileWalkable(aboveCoord, inInput.AreaMask))
            {
                inNeighbours.Add(aboveCoord);
            }

            Vector2Int belowCoord = HexagonGridHelper.GetNeighbourInDirection(inCurrentCoord, HexagonGridHelper.GetRelativeDirection(movingDirection, -1));

            if (IsTileWalkable(belowCoord, inInput.AreaMask))
            {
                inNeighbours.Add(belowCoord);
            }
        }
Exemplo n.º 2
0
        private Vector2Int?Jump(Vector2Int inNeighbourCoordinate, Vector2Int inParentCoordinate, FindPathInput inInput)
        {
            if (!IsTileWalkable(inNeighbourCoordinate, inInput.AreaMask))
            {
                return(null);
            }

            if (inNeighbourCoordinate == inInput.TargetCoordinate)
            {
                return(inNeighbourCoordinate);
            }

            if (NavTileManager.Instance.SurfaceManager.GridInfo.CellLayout == GridLayout.CellLayout.Hexagon)
            {
                // Identify forced neighbours.
                Direction  movingDirection = HexagonGridHelper.GetDirection(inParentCoordinate, inNeighbourCoordinate);
                Vector2Int aboveFarCoord   = HexagonGridHelper.GetNeighbourInDirection(inNeighbourCoordinate, HexagonGridHelper.GetRelativeDirection(movingDirection, 2));
                Vector2Int aboveCloseCoord = HexagonGridHelper.GetNeighbourInDirection(inNeighbourCoordinate, HexagonGridHelper.GetRelativeDirection(movingDirection, 1));

                if (!IsTileWalkable(aboveFarCoord, inInput.AreaMask) && IsTileWalkable(aboveCloseCoord, inInput.AreaMask))
                {
                    return(inNeighbourCoordinate);
                }

                Vector2Int belowFarCoord   = HexagonGridHelper.GetNeighbourInDirection(inNeighbourCoordinate, HexagonGridHelper.GetRelativeDirection(movingDirection, -2));
                Vector2Int belowCloseCoord = HexagonGridHelper.GetNeighbourInDirection(inNeighbourCoordinate, HexagonGridHelper.GetRelativeDirection(movingDirection, -1));

                if (!IsTileWalkable(belowFarCoord, inInput.AreaMask) && IsTileWalkable(belowCloseCoord, inInput.AreaMask))
                {
                    return(inNeighbourCoordinate);
                }

                // No forced neighbours, jump further.
                if ((int)movingDirection % 2 == 0)
                {
                    // Even directions have to check adjacent directions.
                    if (Jump(aboveCloseCoord, inNeighbourCoordinate, inInput) != null ||
                        Jump(belowCloseCoord, inNeighbourCoordinate, inInput) != null)
                    {
                        return(inNeighbourCoordinate);
                    }
                }

                return(Jump(HexagonGridHelper.GetNeighbourInDirection(inNeighbourCoordinate, movingDirection), inNeighbourCoordinate, inInput));
            }

            Vector2Int horizontalDelta = new Vector2Int(inNeighbourCoordinate.x - inParentCoordinate.x, 0);
            Vector2Int verticalDelta   = new Vector2Int(0, inNeighbourCoordinate.y - inParentCoordinate.y);

            if (inInput.DiagonalAllowed && horizontalDelta.x != 0 && verticalDelta.y != 0)
            {
                // Diagonal movement.
                if (inInput.CutCorners)
                {
                    if (!IsTileWalkable(inNeighbourCoordinate - horizontalDelta, inInput.AreaMask) && IsTileWalkable(inNeighbourCoordinate - horizontalDelta + verticalDelta, inInput.AreaMask) ||
                        !IsTileWalkable(inNeighbourCoordinate - verticalDelta, inInput.AreaMask) && IsTileWalkable(inNeighbourCoordinate + horizontalDelta - verticalDelta, inInput.AreaMask))
                    {
                        return(inNeighbourCoordinate);
                    }
                }

                // Straight jumps.
                if (Jump(inNeighbourCoordinate + horizontalDelta, inNeighbourCoordinate, inInput) != null ||
                    Jump(inNeighbourCoordinate + verticalDelta, inNeighbourCoordinate, inInput) != null)
                {
                    return(inNeighbourCoordinate);
                }
            }
            else
            {
                // Straight movement.
                if (horizontalDelta.x != 0 && CheckStraightJumpMovement(inNeighbourCoordinate, horizontalDelta, inInput))
                {
                    return(inNeighbourCoordinate);
                }
                else if (verticalDelta.y != 0)
                {
                    if (CheckStraightJumpMovement(inNeighbourCoordinate, verticalDelta, inInput))
                    {
                        return(inNeighbourCoordinate);
                    }

                    if (!inInput.DiagonalAllowed)
                    {
                        // Jump horizontally when moving vertically during non-diagonal search.
                        if (Jump(inNeighbourCoordinate + Vector2Int.right, inNeighbourCoordinate, inInput) != null ||
                            Jump(inNeighbourCoordinate + Vector2Int.left, inNeighbourCoordinate, inInput) != null)
                        {
                            return(inNeighbourCoordinate);
                        }
                    }
                }
            }

            if (inInput.DiagonalAllowed && inInput.CutCorners)
            {
                if (IsTileWalkable(inNeighbourCoordinate + horizontalDelta, inInput.AreaMask) && IsTileWalkable(inNeighbourCoordinate + verticalDelta, inInput.AreaMask))
                {
                    return(Jump(inNeighbourCoordinate + horizontalDelta + verticalDelta, inNeighbourCoordinate, inInput));
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(Jump(inNeighbourCoordinate + horizontalDelta + verticalDelta, inNeighbourCoordinate, inInput));
            }
        }