Beispiel #1
0
    protected void ComeMineMe(UnitComponent unit)   // should probs make a delegate. // Should probably move this to a manager script for this.
    {
        if (unit == null)
        {
            return;
        }

        Vector3 selectedUnitPosition = unit.transform.position;

        List <MapNode> neighbourListUnder = referenceTerrain.GetNeighbours(dBUnderlyingTile.nodeReference);

        foreach (MapNode neighbour in neighbourListUnder)
        {
            if (!neighbour.walkableNode)
            {
                neighbourListUnder.Remove(neighbour);
                continue;
            }
            int     xCoord     = neighbour.lvlGrid_X;
            int     zCoord     = neighbour.lvlGrid_YorZ;
            Vector3 testCoords = new Vector3(xCoord, 0, zCoord);

            PathFinderRequestManager.RequestPath(new PathRequest(selectedUnitPosition, testCoords, PathCompare));
        }
    }
    //public void QueueGoToTile (Vector3 startPos, Vector3 destPos) {  No longer needed since we will now call GoToTile directly.
    //    //Debug.Log("QueueGoToTile called");
    //    StartCoroutine(GoToTile(startPos, destPos));
    //}

    public void GoToTile(PathRequest request, Action <PathResult> callback)
    {
        //Clean A* first

        //Debug.Log("GoToTile started for: "+startPos+" to "+destPos);
        Vector3[] waypoints     = new Vector3[0];
        bool      pathProcessed = false;

        MapNode startNode = mapGrid.NodeFromWorldPoint(request.pStart); // could be modified later on. Was startPos
        //Debug.Log("GTT Start node: "+ startNode.lvlGrid_X + "; " + startNode.lvlGrid_YorZ +" walkable= "+ startNode.walkableNode);
        MapNode destNode = mapGrid.NodeFromWorldPoint(request.pEnd);    // was destPos . NOTE: corresponds to "goThereModif" in the UnitComponent class.

        //Debug.Log("GTT End node: " + destNode.lvlGrid_X + "; " + destNode.lvlGrid_YorZ + " walkable= " +destNode.walkableNode);

        if (startNode.walkableNode && destNode.walkableNode)
        {
            //Debug.Log("GTT Both are walkable, proceeding... MapTotalSize= "+ mapGrid.TotalMapSize);

            HeapOptim <MapNode> openListHeap = new HeapOptim <MapNode>(mapGrid.TotalMapSize);
            HashSet <MapNode>   closedList   = new HashSet <MapNode>();

            openListHeap.HeapAdd(startNode);

            while (openListHeap.HeapCount > 0)
            {
                //Debug.Log("GTT while Loop initiated"); // starts properly
                MapNode currentNode = openListHeap.RemoveFirst();
                closedList.Add(currentNode);

                if (currentNode == destNode)
                {
                    //Need to add destNode to path array.


                    //Debug.Log("GTT "+(currentNode == destNode) + " Path computed. current;dest: "+ currentNode.worldPosition +"; "+ destNode.worldPosition);
                    pathProcessed = true;
                    break;
                }

                foreach (MapNode neighbour in mapGrid.GetNeighbours(currentNode))
                {
                    if (!neighbour.walkableNode || closedList.Contains(neighbour))    // add crossable condition here, not in GetNeighbours.
                    {
                        continue;
                    }

                    int newCostToNeighbour = currentNode.gCost + ReturnDistance(currentNode, neighbour) + neighbour.movementModifier; // This is where we take into account a tile's unit movement penalty for pathfinding.

                    //Debug.Log("newCostToNeighbour: "+newCostToNeighbour);
                    if (newCostToNeighbour < neighbour.gCost || !openListHeap.Contains(neighbour))
                    {
                        neighbour.gCost          = newCostToNeighbour;
                        neighbour.hCost          = ReturnDistance(neighbour, destNode);
                        neighbour.parentTileNode = currentNode;

                        if (!openListHeap.Contains(neighbour))
                        {
                            openListHeap.HeapAdd(neighbour);
                        }
                        else
                        {
                            openListHeap.UpdateHeapNode(neighbour);
                        }
                    }
                }
            }
        }
        if (pathProcessed)
        {
            //Debug.Log("Retracing path from: "+ startNode.worldPosition +" to "+ destNode.worldPosition);
            waypoints     = RetracePath(startNode, destNode, request.pEnd);
            pathProcessed = waypoints.Length > 0;
        }
        //pfReqManager.DoneProcessing(waypoints, pathProcessed);
        callback(new PathResult(waypoints, pathProcessed, request.callback));
    }