void OnMapReset()
 {
     if (openHeap == null)
     {
         openHeap = new NodeHeap();
     }
     openHeap.InitHeap(navMesh.nodes);
     readyToPathfind = true;
 }
    void Start()
    {
        nodes = new Heap <NodeHeap> ();

        GameObject nodeGo = (GameObject)Instantiate(nodeprefab);

        NodeHeap node = nodeGo.GetComponent <NodeVisual> ().node;

        node.g = nodeGo;


        nodes.Add(node);
        node.Position = Vector3.zero;
    }
 // Start is called before the first frame update
 void Start()
 {
     navMesh       = GetComponent <NavMesh3>();
     moves         = new List <Vector3>();
     smoothMoves   = new List <Vector3>();
     roughMoves    = new List <Vector3>();
     funneledMoves = new List <Vector3>();
     if (openHeap == null)
     {
         openHeap = new NodeHeap();
     }
     if (navMesh != null && navMesh.nodes != null)
     {
         openHeap.InitHeap(navMesh.nodes);
         readyToPathfind = true;
     }
 }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            inputPos.z = 0;

            RaycastHit2D hit = Physics2D.Raycast(inputPos, Vector3.zero, int.MaxValue);
            if (hit)
            {
                hit.transform.GetComponent <NodeVisual> ().node.nodeValue = Random.Range(0, 100);
                hit.transform.GetComponentInChildren <TextMesh> ().text   = hit.transform.GetComponent <NodeVisual> ().node.nodeValue.ToString();
                nodes.UpdateItem(hit.transform.GetComponent <NodeVisual> ().node);
            }
            else
            {
                GameObject nodeGo = (GameObject)Instantiate(nodeprefab);
                NodeHeap   node   = nodeGo.GetComponent <NodeVisual> ().node;
                node.g = nodeGo;
                nodes.Add(node);
            }

            foreach (NodeHeap n in nodes)
            {
                n.g.transform.position = n.Position;
            }
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            NodeHeap removingNode = nodes.RemoveFirst();
            Destroy(removingNode.g);

            foreach (NodeHeap n in nodes)
            {
                n.g.transform.position = n.Position;
            }
        }
    }
Exemple #5
0
    public List <Vector3> FindPath(Node deptNode, Node destNode)
    {
        NodeHeap       openHeap  = new NodeHeap();
        HashSet <Node> closedSet = new HashSet <Node>();

        openHeap.Add(deptNode);
        deptNode.fromCost = deptNode.toCost = 0;
        deptNode.from     = destNode.from = null;

        while (openHeap.Count > 0)
        {
            Node nodeToVisit = openHeap.Pop();
            closedSet.Add(nodeToVisit);

            if (nodeToVisit == destNode)
            {
                break;
            }

            foreach (var adjacent in GetAdjacents(nodeToVisit))
            {
                if (closedSet.Contains(adjacent))
                {
                    continue;
                }

                Node fromNode = null;
                if (nodeToVisit.from != null && nodeToVisit.from.IsLineOfSight(adjacent))
                {
                    fromNode = nodeToVisit.from;
                }
                else
                {
                    fromNode = nodeToVisit;
                }

                int toAdjacentCost = fromNode.toCost + PredictDistanceCost(fromNode, adjacent);
                if (!openHeap.Contains(adjacent) || toAdjacentCost < adjacent.toCost)
                {
                    adjacent.toCost   = toAdjacentCost;
                    adjacent.fromCost = PredictDistanceCost(adjacent, destNode);
                    adjacent.from     = fromNode;

                    if (openHeap.Contains(adjacent))
                    {
                        openHeap.UpdateItem(adjacent);
                    }
                    else
                    {
                        openHeap.Add(adjacent);
                    }
                }
            }
        }

        List <Node> path = new List <Node>();

        if (destNode.from != null)
        {
            Node currentNode = destNode;

            while (currentNode != null)
            {
                path.Add(currentNode);
                currentNode = currentNode.from;
            }
            path.Reverse();
        }

        // PS
        // SmoothingPath(path);

        List <Vector3> positionPath = new List <Vector3>();

        // Visit departure node looks wired when path is changed
        // Skip first(departure) node
        for (int i = 1; i < path.Count; i++)
        {
            positionPath.Add(path[i].tile.transform.position);
        }
        return(positionPath);
    }
Exemple #6
0
 void Awake()
 {
     node = new NodeHeap();
     GetComponentInChildren <TextMesh> ().text = node.nodeValue.ToString();
 }
Exemple #7
0
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos, NodeGrid grid)
    {
        Vector3[] waypoints  = new Vector3[0];
        bool      pathSucces = false;

        if (grid != null)
        {
            Node startNode  = grid.GetNodeFromWorldPoint(startPos);
            Node targetNode = grid.GetNodeFromWorldPoint(targetPos);

            if (/*startNode.walkable && */ targetNode.walkable)
            {
                NodeHeap       openSet   = new NodeHeap(grid.MaxSize);
                HashSet <Node> closedSet = new HashSet <Node>();
                openSet.Add(startNode);

                while (openSet.Count > 0)
                {
                    Node currentNode = openSet.RemoveFirst();
                    closedSet.Add(currentNode);

                    if (currentNode == targetNode)
                    {
                        pathSucces = true;
                        break;
                    }

                    foreach (Node neighbour in grid.GetNeighbours(currentNode))
                    {
                        if (!neighbour.walkable || closedSet.Contains(neighbour))
                        {
                            continue;
                        }

                        int newCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);

                        if (newCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                        {
                            neighbour.gCost  = newCostToNeighbour;
                            neighbour.hCost  = GetDistance(neighbour, targetNode);
                            neighbour.parent = currentNode;

                            if (!openSet.Contains(neighbour))
                            {
                                openSet.Add(neighbour);
                            }
                            else
                            {
                                openSet.UpdateItem(neighbour);
                            }
                        }
                    }
                }
            }

            yield return(null);

            if (pathSucces)
            {
                waypoints  = RetracePath(startNode, targetNode, grid);
                pathSucces = (waypoints.Length > 0);
            }

            requestManager.FinishProcessingPath(waypoints, pathSucces);
        }
    }