Esempio n. 1
0
    public void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 mousePos   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

            RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
            if (hit.collider != null && hit.collider.tag == "Point")
            {
                if (EndPoint != null)
                {
                    StartPoint = EndPoint;
                }
                EndPoint = hit.collider.gameObject;
            }
            brojProlaza = 1;
            Terrain_Points.ResetColorsInScene();
        }

        if (Input.GetMouseButtonDown(1))
        {
            Vector3 mousePos   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

            RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
            if (hit.collider != null && hit.collider.tag == "Point")
            {
                Destroy(hit.collider.gameObject);
            }
            brojProlaza = 1;
            Terrain_Points.ResetColorsInScene();
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (EndPoint != null && StartPoint != null)
            {
                Point newStartPoint = NameToPoint(StartPoint.name);
                Point newEndPoint   = NameToPoint(EndPoint.name);
                pathfinding.brojProlaza = brojProlaza;
                List <Point> newPath = pathfinding.FindPath(newStartPoint, newEndPoint);
                if (newPath != null)
                {
                    brojProlaza = 1;
                    Terrain_Points.ResetColorsInScene();
                    foreach (Point pathPoint in newPath)
                    {
                        Terrain_Points.SetColor(pathPoint, 4);
                    }
                    Terrain_Points.SetColor(newPath[0], 1);
                    Terrain_Points.SetColor(newPath[newPath.Count - 1], 2);
                }
                else
                {
                    brojProlaza++;
                }
            }
        }
    }
Esempio n. 2
0
    public List <Point> FindPath(Point StartPoint, Point EndPoint)   //Vraća listu točaka po kojoj će objekt ići
    {
        openList.Clear();
        closedList.Clear();

        openList.Add(StartPoint);

        StartPoint.g_cost = 0;
        StartPoint.h_cost = CalculateFastestRoute(StartPoint, EndPoint);
        StartPoint.g_cost = int.MaxValue;

        //while (openList.Count > 0)
        while (brojProlaza > 0)
        {
            Point curPoint = GetLowestFcostPoint();

            //Debug.Log("prolaz: " + brojProlaza + ", end: " + EndPoint.toStr() + ", cur: " + curPoint.toStr());

            if (curPoint.toStr() == EndPoint.toStr())
            {
                return(CalculatePath(curPoint)); //ovo je kraj, treba još izračunati path
            }

            openList.Remove(curPoint);
            closedList.Add(curPoint);

            Terrain_Points.SetColor(curPoint, 5);

            foreach (Point nbPoint in AllNeighbourList(curPoint))
            {
                if (closedList.Contains(nbPoint))
                {
                    continue;
                }

                Terrain_Points.SetColor(nbPoint, 3);
                int pre_gcost = curPoint.g_cost + CalculateFastestRoute(curPoint, nbPoint);

                if (pre_gcost < nbPoint.g_cost)
                {
                    nbPoint.parent = curPoint;
                    nbPoint.g_cost = pre_gcost;
                    nbPoint.h_cost = CalculateFastestRoute(nbPoint, EndPoint);
                    nbPoint.SetF();

                    if (!openList.Contains(nbPoint))
                    {
                        openList.Add(nbPoint);
                    }
                }
            }
            brojProlaza--;
        }

        //ako dode do ovdje program znači da je prošao sve pointove
        //Debug.Log("Every point has been checked!");
        return(null);
    }
Esempio n. 3
0
    public static void ResetColorsInScene() //osim starting & ending pointa
    {
        Player playerScr = GameObject.Find("GameManager").GetComponent <Player>();

        GameObject[] allPoints = GameObject.FindGameObjectsWithTag("Point");
        foreach (GameObject onePoint in allPoints)
        {
            if (onePoint == playerScr.StartPoint)
            {
                Terrain_Points.SetColor(onePoint, 1);
            }
            else if (onePoint == playerScr.EndPoint)
            {
                Terrain_Points.SetColor(onePoint, 2);
            }
            else
            {
                Terrain_Points.SetColor(onePoint, 0);
            }
        }
    }