예제 #1
0
        private Node GetNode(int x, int y, int z)
        {
            Node n = null;

            lock (gridBase)
            {
                n = gridBase.GetNode(x, y, z);
            }
            return(n);
        }
예제 #2
0
        private Node GetNode(int x, int y, int z)
        {
            Node n = null;

            //When there are multiple threads, there can be conflicts, this helps prevent that from happening.
            lock (gridBase)
            {
                n = gridBase.GetNode(x, y, z);
            }
            return(n);
        }
        private Node GetNode(int x, int y, int z)
        {
            Node n = null;

            // this ensures that one thread is executing a piece of code at one time.
            // keep in mind,
            // one thread does not enter a critical section of code while another thread is in that critical section.
            lock (gridBase)
            {
                n = gridBase.GetNode(x, y, z);
            }

            return(n);
        }
예제 #4
0
        private Node GetNeighborNode(Node adjPos, bool searchTopDown, Node currentNodePos)
        {
            //This is the meat. We can add all of the checks we need to tweak the alg.
            //First, the usual A* stuff.

            Node retVal = null;

            //Let's take the node from the adjacent positions we passed
            Node node = GetNode(adjPos.x, adjPos.y, adjPos.z);

            //if it's not null and we can walk on it...
            if (node != null && node.isWalkable)
            {
                //We can use this node.
                retVal = node;
            }//Otherwise...
            else if (searchTopDown) //and we want 3d A*
            {
                //Then look at what the node has under it
                adjPos.y -= 1;
                Node bottomBlock = gridBase.GetNode(adjPos.x, adjPos.y, adjPos.z);

                //If there is a bottom block and we can walk on it
                if (bottomBlock != null && bottomBlock.isWalkable)
                {
                    //We can return that.
                    retVal = bottomBlock;
                }
                else
                {
                    //Otherwise, let's look up instead.
                    adjPos.y += 2;
                    Node topBlock = gridBase.GetNode(adjPos.x, adjPos.y, adjPos.z);

                    //Same as above.
                    if (topBlock != null && topBlock.isWalkable)
                    {
                        retVal = topBlock;
                    }
                }
            }

            //If the node is diagonal to the current node then we check it's neighbors.
            //To move diagonally, we need all 4 nodes walkable.
            int originalX = adjPos.x - currentNodePos.x;
            int originalZ = adjPos.z - currentNodePos.z;

            if (Mathf.Abs(originalX) == 1 && Mathf.Abs(originalZ) == 1)
            {
                //The first block is originalX, 0 and the second to check is 0, originalZ.
                //Both need to be walkable.
                Node neighbor1 = gridBase.GetNode(currentNodePos.x + originalX, currentNodePos.y, currentNodePos.z + originalZ);
                if (neighbor1 == null || !neighbor1.isWalkable)
                {
                    retVal = null;
                }

                Node neighbor2 = gridBase.GetNode(currentNodePos.x, currentNodePos.y, currentNodePos.z + originalZ);
                if (neighbor2 == null || !neighbor2.isWalkable)
                {
                    retVal = null;
                }
            }

            //Here we can add additional checks.
            if (retVal != null)
            {
                //EX. Do not approach any nodes from the left

                /*if(node.x < currentNodePos.x)
                 * {
                 *  node = null;
                 * }*/
            }

            return(retVal);
        }