Esempio n. 1
0
 /// <summary>
 /// Can the given args can traverse this region?
 /// </summary>
 /// <param name="reachableArgs"></param>
 /// <returns></returns>
 public bool RegionTraversable(ReachableArgs reachableArgs)
 {
     // The assumption is that all nodes in a region have the same pathfinding traits
     // As such we can just use the origin node for checking.
     return(PathfindingHelpers.Traversable(reachableArgs.CollisionMask, reachableArgs.Access,
                                           OriginNode));
 }
        /// <summary>
        /// Check to see if the node is a jump point (only works for cardinal directions)
        /// </summary>
        private bool IsCardinalJumpPoint(Direction direction, PathfindingNode currentNode)
        {
            PathfindingNode openNeighborOne;
            PathfindingNode closedNeighborOne;
            PathfindingNode openNeighborTwo;
            PathfindingNode closedNeighborTwo;

            switch (direction)
            {
            case Direction.North:
                openNeighborOne   = currentNode.GetNeighbor(Direction.NorthEast);
                closedNeighborOne = currentNode.GetNeighbor(Direction.East);

                openNeighborTwo   = currentNode.GetNeighbor(Direction.NorthWest);
                closedNeighborTwo = currentNode.GetNeighbor(Direction.West);
                break;

            case Direction.East:
                openNeighborOne   = currentNode.GetNeighbor(Direction.NorthEast);
                closedNeighborOne = currentNode.GetNeighbor(Direction.North);

                openNeighborTwo   = currentNode.GetNeighbor(Direction.SouthEast);
                closedNeighborTwo = currentNode.GetNeighbor(Direction.South);
                break;

            case Direction.South:
                openNeighborOne   = currentNode.GetNeighbor(Direction.SouthEast);
                closedNeighborOne = currentNode.GetNeighbor(Direction.East);

                openNeighborTwo   = currentNode.GetNeighbor(Direction.SouthWest);
                closedNeighborTwo = currentNode.GetNeighbor(Direction.West);
                break;

            case Direction.West:
                openNeighborOne   = currentNode.GetNeighbor(Direction.NorthWest);
                closedNeighborOne = currentNode.GetNeighbor(Direction.North);

                openNeighborTwo   = currentNode.GetNeighbor(Direction.SouthWest);
                closedNeighborTwo = currentNode.GetNeighbor(Direction.South);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if ((closedNeighborOne == null || !PathfindingHelpers.Traversable(_pathfindingArgs.CollisionMask, _pathfindingArgs.Access, closedNeighborOne)) &&
                (openNeighborOne != null && PathfindingHelpers.Traversable(_pathfindingArgs.CollisionMask, _pathfindingArgs.Access, openNeighborOne)))
            {
                return(true);
            }

            if ((closedNeighborTwo == null || !PathfindingHelpers.Traversable(_pathfindingArgs.CollisionMask, _pathfindingArgs.Access, closedNeighborTwo)) &&
                (openNeighborTwo != null && PathfindingHelpers.Traversable(_pathfindingArgs.CollisionMask, _pathfindingArgs.Access, openNeighborTwo)))
            {
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Check to see if the node is a jump point (only works for cardinal directions)
        /// </summary>
        private bool IsCardinalJumpPoint(Direction direction, PathfindingNode currentNode)
        {
            PathfindingNode?openNeighborOne   = null;
            PathfindingNode?closedNeighborOne = null;
            PathfindingNode?openNeighborTwo   = null;
            PathfindingNode?closedNeighborTwo = null;

            switch (direction)
            {
            case Direction.North:
                foreach (var neighbor in currentNode.GetNeighbors())
                {
                    var neighborDirection = PathfindingHelpers.RelativeDirection(neighbor, currentNode);
                    switch (neighborDirection)
                    {
                    case Direction.NorthEast:
                        openNeighborOne = neighbor;
                        break;

                    case Direction.East:
                        closedNeighborOne = neighbor;
                        break;

                    case Direction.NorthWest:
                        openNeighborTwo = neighbor;
                        break;

                    case Direction.West:
                        closedNeighborTwo = neighbor;
                        break;
                    }
                }
                break;

            case Direction.East:
                foreach (var neighbor in currentNode.GetNeighbors())
                {
                    var neighborDirection = PathfindingHelpers.RelativeDirection(neighbor, currentNode);
                    switch (neighborDirection)
                    {
                    case Direction.NorthEast:
                        openNeighborOne = neighbor;
                        break;

                    case Direction.North:
                        closedNeighborOne = neighbor;
                        break;

                    case Direction.SouthEast:
                        openNeighborTwo = neighbor;
                        break;

                    case Direction.South:
                        closedNeighborTwo = neighbor;
                        break;
                    }
                }
                break;

            case Direction.South:
                foreach (var neighbor in currentNode.GetNeighbors())
                {
                    var neighborDirection = PathfindingHelpers.RelativeDirection(neighbor, currentNode);
                    switch (neighborDirection)
                    {
                    case Direction.SouthEast:
                        openNeighborOne = neighbor;
                        break;

                    case Direction.East:
                        closedNeighborOne = neighbor;
                        break;

                    case Direction.SouthWest:
                        openNeighborTwo = neighbor;
                        break;

                    case Direction.West:
                        closedNeighborTwo = neighbor;
                        break;
                    }
                }
                break;

            case Direction.West:
                foreach (var neighbor in currentNode.GetNeighbors())
                {
                    var neighborDirection = PathfindingHelpers.RelativeDirection(neighbor, currentNode);
                    switch (neighborDirection)
                    {
                    case Direction.NorthWest:
                        openNeighborOne = neighbor;
                        break;

                    case Direction.North:
                        closedNeighborOne = neighbor;
                        break;

                    case Direction.SouthWest:
                        openNeighborTwo = neighbor;
                        break;

                    case Direction.South:
                        closedNeighborTwo = neighbor;
                        break;
                    }
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if ((closedNeighborOne == null || !PathfindingHelpers.Traversable(_pathfindingArgs.CollisionMask, _pathfindingArgs.Access, closedNeighborOne)) &&
                openNeighborOne != null && PathfindingHelpers.Traversable(_pathfindingArgs.CollisionMask, _pathfindingArgs.Access, openNeighborOne))
            {
                return(true);
            }

            if ((closedNeighborTwo == null || !PathfindingHelpers.Traversable(_pathfindingArgs.CollisionMask, _pathfindingArgs.Access, closedNeighborTwo)) &&
                openNeighborTwo != null && PathfindingHelpers.Traversable(_pathfindingArgs.CollisionMask, _pathfindingArgs.Access, openNeighborTwo))
            {
                return(true);
            }

            return(false);
        }