Exemple #1
0
 // Update is called once per frame
 void Update()
 {
     timer -= Time.deltaTime;
     if (timer < 0)
     {
         draw.DrawNeighborLinks(destinations);
         timer = 10;
     }
     if (Input.GetMouseButtonDown(0))
     {
         graphGrid.Clear();
         RaycastHit hitInfo = new RaycastHit();
         bool       hit     = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
         if (hit)
         {
             if (hitInfo.transform.parent.tag == "tile")
             {
                 Node    target     = hitInfo.transform.parent.GetComponent <Node>();
                 Vector3 currentPos = actor.transform.position;
                 Debug.Log(currentPos);
                 Node         start     = graphGrid.GetNodeFromGrid(Mathf.RoundToInt(currentPos.x), Mathf.RoundToInt(currentPos.z));
                 Stack <Node> path      = astar.FindPath(graphGrid, start, target);
                 Stack <Node> pathStack = new Stack <Node>(path);
                 draw.SetPath(path);
                 actor.GetComponent <Actor>().MoveOnPath(path);
             }
         }
     }
 }
    void Update()
    {
        if (Input.GetMouseButtonDown(0) && buildLevel)
        {
            RaycastHit hitInfo = new RaycastHit();
            bool       hit     = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
            if (hit)
            {
                if (hitInfo.transform.tag == "tile")
                {
                    hitInfo.transform.GetComponent <Node>().walkable = false;
                    hitInfo.transform.GetComponent <TileEffects>().SetObstacleMat();
                }
            }
        }
        else if (Input.GetMouseButtonDown(0) && disable == false)
        {
            grid.Clear();
            RaycastHit hitInfo = new RaycastHit();
            bool       hit     = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
            if (hit)
            {
                if (hitInfo.transform.tag == "tile")
                {
                    disable = true;
                    Node target = hitInfo.transform.GetComponent <Node>();
                    if (!target.walkable)
                    {
                        disable = false;
                        return;
                    }

                    Vector3 currentPos = actor.transform.position;
                    Debug.Log(currentPos);
                    Node start = grid.GetNodeFromGrid(Mathf.RoundToInt(currentPos.x), Mathf.RoundToInt(currentPos.z));

                    Debug.Log(start + ", " + target);
                    Stack <Node> path = astar.FindPath(grid, start, target);
                    grid.Clear();
                    Stack <Node> pathStack = new Stack <Node>(path);
                    PaintPath(pathStack);
                    actor.GetComponent <Actor>().MoveOnPath(path);
                    disable = false;
                }
            }
        }
    }
Exemple #3
0
    public void RunAStar()
    {
        float time = 0;

        mapGrid.Clear();
        Stopwatch sw = new Stopwatch();

        sw.Start();


        Stack <Node> path = pathfinding.FindPath(mapGrid, startNode, targetNode);

        sw.Stop();
        time = sw.ElapsedMilliseconds;

        timeText.text = "Path found in " + time + " ms";

        //TODO: Implement an entity.
        //Entity will receive the stack, and interally call a coroutine to move from
        //node to node in the path by Popping the next Node in the stack


        // DrawTileColors(openList,closedList);

        // startNode.gameObject.GetComponent<Renderer>().material = startMat;
        // targetNode.gameObject.GetComponent<Renderer>().material = targetMat;
        // bool done = false;
        // // Node pathNode = targetNode;
        // while (!done){
        //  if(pathNode != startNode && pathNode != targetNode){
        //      pathNode.gameObject.GetComponent<Renderer>().material = pathMat;
        //      pathNode.gTxt.text = pathNode.gCost.ToString();
        //      pathNode.hTxt.text = pathNode.hCost.ToString();
        //      pathNode.fTxt.text = pathNode.fCost.ToString();
        //  }
        //  pathNode = pathNode.parent;
        //  if(pathNode == startNode){
        //      done = true;
        //  }
        // }
    }