Exemplo n.º 1
0
        private unsafe static void TestNeighbour(NativeArray2D <MapNode> gridCopy, ref MapNode kNeighbour, MapNode *currentNodePtr, NativeList <MapNode> neighbours)
        {
            //If node is open, (eg. already tested previously), we compare the g distances to see if traversing the neighbouring node will be more efficient from this node than its parents node.
            if (kNeighbour.state == NodeState.Open)
            {
                float gDistance     = math.distancesq(kNeighbour.position, kNeighbour.parent->position);
                float tempGDistance = currentNodePtr->g + gDistance;

                if (tempGDistance < kNeighbour.g)
                {
                    MapNode *neighbourPtr = gridCopy.GetPointerToElement(kNeighbour.gridPosition);

                    neighbourPtr->SetParent(currentNodePtr);
                    neighbours.Add(*neighbourPtr);
                }
            }
            //If we're untested, we don't have a parent already, so set parent and add to list.
            else
            {
                MapNode *neighbourPtr = gridCopy.GetPointerToElement(kNeighbour.gridPosition);

                neighbourPtr->SetParent(currentNodePtr);
                neighbourPtr->state = NodeState.Open;
                neighbours.Add(*neighbourPtr);
            }
        }
Exemplo n.º 2
0
        private static unsafe bool SearchForPath(int2 currentNode, int2 targetNode, NativeArray2D <MapNode> gridCopy)
        {
            MapNode *currentNodePtr = gridCopy.GetPointerToElement(currentNode);

            NativeList <MapNode> passableNeighbours = GetPassableNeighbours(currentNode, gridCopy, currentNodePtr);

            NodeCompare nodeCompare = new NodeCompare();

            passableNeighbours.Sort(nodeCompare);

            currentNodePtr->state = NodeState.Closed;
            for (int i = 0; i < passableNeighbours.Length; ++i)
            {
                MapNode neighbour = passableNeighbours[i];
                currentNodePtr->child = gridCopy.GetPointerToElement(neighbour.gridPosition);

                //Target has been reached.
                if (neighbour.gridPosition.Equals(targetNode))
                {
                    passableNeighbours.Dispose();
                    return(true);
                }

                //Recursively search deeper.
                if (SearchForPath(neighbour.gridPosition, targetNode, gridCopy))
                {
                    passableNeighbours.Dispose();
                    return(true);
                }
            }

            passableNeighbours.Dispose();
            return(false);
        }