Esempio n. 1
0
    // Methods
    ////////////////
    // Path Finding
    ////////////////
    public List <Vector3> FindPath(Vector3 startingVector, int monsterType)
    {
        this.RunSetup();
        // Data Set up
        bool    pathFound     = false;
        Vector3 currentVector = threatMap.scaleWorldPos(startingVector);

        Vector3[] adjacentVectors = new Vector3[4];
        Path      adjacentPath;
        Path      smallestPath;
        Path      currentPath = new Path(currentVector, null, TileWeight(currentVector, monsterType));

        // Add starting path to open list
        openList.Add(currentPath);
        do
        {
            // Find the smallest vector/tile in open list
            smallestPath = FindSmallest();
            // Make the smallest the current location
            currentPath = smallestPath;
            // Add current location to the closed list
            closedList.Add(currentPath);
            // Remove current location from the open list
            openList.Remove(currentPath);

            // Check to see if we arrived at destination
            currentVector = smallestPath.GetPosition();
            if ((currentVector.x > playerLocation.x - 0.6f) && (currentVector.x < playerLocation.x + 0.6f))
            {
                if ((currentVector.y > playerLocation.y - 0.6f) && (currentVector.y < playerLocation.y + 0.6f))
                {
                    pathFound = true;
                    break;
                }
            }

            // retreive all adjacent squares
            adjacentVectors[0] = new Vector3(currentVector.x, currentVector.y + 1f, 0);
            adjacentVectors[1] = new Vector3(currentVector.x, currentVector.y - 1f, 0);
            adjacentVectors[2] = new Vector3(currentVector.x + 1f, currentVector.y, 0);
            adjacentVectors[3] = new Vector3(currentVector.x - 1f, currentVector.y, 0);

            // for loop through the adjacent square
            for (int i = 0; i < 4; i += 1)
            {
                // check if its out of bounds
                if ((adjacentVectors[i].x > -18) && (adjacentVectors[i].x < 18) && (adjacentVectors[i].y > -18) && (adjacentVectors[i].y < 18))
                {
                    int adjacentScore = TileWeight(adjacentVectors[i], monsterType);
                    // Verify we want to go there (wall)
                    if (adjacentScore < 1000)
                    {
                        adjacentPath = new Path(adjacentVectors[i], currentPath, adjacentScore);
                        // Check the closed list for it, if found ignore it
                        if (closedList.Find(x => x.GetPosition() == adjacentPath.GetPosition()) == null)
                        {
                            // Check the open list for it, if found ignore it
                            if (openList.Find(x => x.GetPosition() == adjacentPath.GetPosition()) == null)
                            {
                                openList.Add(adjacentPath);
                            }
                        }
                    }
                }
            }
            // Continue until there is no more available square in the open list (which means there is no path)
        } while (openList.Count > 0);
        // If a path was found
        if (pathFound == true)
        {
            List <Vector3> thePath = new List <Vector3>();
            // Follow it backwards
            while (currentPath.GetParent() != null)
            {
                thePath.Add(currentPath.GetPosition());
                currentPath = currentPath.GetParent();
            }
            // Return it
            return(thePath);
        }
        // If no path was found
        else
        {
            // rekt something
            return(null);
        }
    }