Esempio n. 1
0
    void Update()
    {
        mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        if (Input.GetMouseButtonDown(0))
        {
            //grid.SetGridObject(mousePos, 100);
            //grid.GetGridObject(mousePos).SetXY(0, 0);
            Debug.Log(grid.GetGridObject(mousePos).GetX());
            Debug.Log(grid.GetGridObject(mousePos).GetY());
        }

        if (Input.GetMouseButtonDown(1))
        {
            grid.SetGridObject(mousePos, default);
        }
    }
Esempio n. 2
0
    public List <PathNode> FindPath(int startX, int startY, int endX, int endY)
    {
        PathNode startNode = pathGrid.GetGridObject(startX, startY);
        PathNode endNode   = pathGrid.GetGridObject(endX, endY);

        openList = new List <PathNode> {
            startNode
        };
        closedList = new List <PathNode>();

        for (int x = 0; x < pathGrid.GetWidth(); x++)
        {
            for (int y = 0; y < pathGrid.GetHeight(); y++)
            {
                PathNode pathNode = pathGrid.GetGridObject(x, y);
                pathNode.gCost = int.MaxValue;
                pathNode.CalculateFCost();
                pathNode.cameFromNode = null;
            }
        }

        startNode.gCost = 0;
        startNode.hCost = CalculateDistanceCost(startNode, endNode);
        startNode.CalculateFCost();

        while (openList.Count > 0)
        {
            PathNode currentNode = GetLowestFCost(openList);
            if (currentNode == endNode)
            {
                //End reached
                return(CalculatePath(endNode));
            }

            openList.Remove(currentNode); //current node already searched
            closedList.Add(currentNode);

            foreach (PathNode neighbourNode in GetNeighbourList(currentNode))
            {
                if (closedList.Contains(neighbourNode))
                {
                    continue;
                }

                int tentativeGCost = currentNode.gCost + CalculateDistanceCost(currentNode, neighbourNode);
                if (tentativeGCost < neighbourNode.gCost)
                {
                    Debug.DrawLine(pathGrid.GetTilePosition(currentNode.x, currentNode.y) + 0.45f * Vector3.one, pathGrid.GetTilePosition(neighbourNode.x, neighbourNode.y) + 0.45f * Vector3.one, Color.yellow, 200f, false);
                    neighbourNode.cameFromNode = currentNode;
                    neighbourNode.gCost        = tentativeGCost;
                    neighbourNode.hCost        = CalculateDistanceCost(neighbourNode, endNode);
                    neighbourNode.CalculateFCost();

                    if (!openList.Contains(neighbourNode))
                    {
                        openList.Add(neighbourNode); // PROBLEM IF MULTIPLE SOLUTIONS???
                    }
                }
            }
        }

        // out of node in the open list
        return(null);
    }
    public List <PathfindingNode> FindTarget(Vector2 startPos, Vector2 endPos)
    {
        if (grid.GetGridObject(endPos) == null)
        {
            return(null);
        }
        PathfindingNode startNode = grid.GetGridObject(startPos);
        PathfindingNode endNode   = grid.GetGridObject(endPos);

        openNodes = new List <PathfindingNode> {
            startNode
        };
        closedNodes = new List <PathfindingNode>();

        for (int i = 0; i < grid.GetGridWidth(); i++)
        {
            for (int j = 0; j < grid.GetGridHeight(); j++)
            {
                PathfindingNode node = grid.GetGridObject(i, j);
                node.gCost = int.MaxValue;
                node.CalculateFCost();
                node.parentNode = null;
            }
        }

        startNode.gCost = 0;
        startNode.hCost = GetDistanceValue(startNode, endNode);
        startNode.CalculateFCost();

        while (openNodes.Count > 0)
        {
            PathfindingNode currentNode = GetLowestValueNode(openNodes);
            if (currentNode == endNode)
            {
                return(CalculateFinalPath(endNode));
            }

            openNodes.Remove(currentNode);
            closedNodes.Add(currentNode);

            foreach (PathfindingNode neighbor in FindNeighbor(currentNode))
            {
                if (closedNodes.Contains(neighbor))
                {
                    continue;
                }

                if (!neighbor.isWalkable)
                {
                    closedNodes.Add(neighbor);

                    /*
                     * for(int i = -2; i < 3; i++)
                     * {
                     *  for(int j = -2; j < 3; j++)
                     *  {
                     *      closedNodes.Add(grid.GetGridObject(neighbor.GetX() + i, neighbor.GetY() + j));
                     *  }
                     * }
                     */
                    continue;
                }

                int newGCost = currentNode.gCost + GetDistanceValue(currentNode, neighbor);

                if (newGCost < neighbor.gCost)
                {
                    neighbor.parentNode = currentNode;
                    neighbor.gCost      = newGCost;
                    neighbor.hCost      = GetDistanceValue(currentNode, endNode);
                    neighbor.CalculateFCost();
                }

                if (!openNodes.Contains(neighbor))
                {
                    openNodes.Add(neighbor);
                }
            }
        }
        return(null);
    }