예제 #1
0
    /// <summary>
    /// Calculates the path.
    /// </summary>
    /// <param name="g">The g.</param>
    /// <returns>the path is possible</returns>
    public bool CalculatePath(GameObject g)
    {
        to = g.transform.localPosition;
        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();
        UltimaCasilla = new Vector2Int((int)to.x, (int)to.y);
        PQ            = new Priority_Queue.SimplePriorityQueue <Vector2Int, int>();
        DistTo        = mapa.getDistTo((int)transform.localPosition.y, (int)transform.localPosition.x);
        EdgeTo        = new Vector2Int[mapa.altoMapa, mapa.anchoMapa];
        for (int i = 0; i < mapa.altoMapa; i++)
        {
            for (int j = 0; j < mapa.anchoMapa; j++)
            {
                EdgeTo[i, j].x = -1;
                EdgeTo[i, j].y = -1;
            }
        }
        Vector2Int from = new Vector2Int((int)transform.localPosition.x, (int)transform.localPosition.y);

        mapa.setOccupied(from.y, from.x, true);
        PQ.EnqueueWithoutDuplicates(from, 0);
        caminoPosible = false;


        int k = 0;

        while (PQ.Count > 0 && !caminoPosible)
        {
            k++;
            Vector2Int top = PQ.Dequeue();
            if (top != UltimaCasilla)
            {
                for (int i = 0; i < directions.Length; i++)
                {
                    relax(top, directions[i]);
                }
            }
            else
            {
                caminoPosible = true;
            }
        }
        stopwatch.Stop();
        if (caminoPosible)
        {
            GetComponent <Unidad>().SetPath(GetPath(ref UltimaCasilla, ref from));
        }
        GameManager.instance.updateDiagnostico(caminoPosible);
        GameManager.instance.updateDiagnostico(k, stopwatch.Elapsed.TotalMilliseconds, stopwatch.ElapsedTicks);


        return(caminoPosible);
    }
예제 #2
0
    public bool CalculatePath(Vector2Int casilla)
    {
        bestOption = new Vector2Int(-1, -1);
        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();
        UltimaCasilla = new Vector2Int((int)casilla.x, (int)casilla.y);
        PQ            = new Priority_Queue.SimplePriorityQueue <Vector2Int, int>();
        Vector2Int from = new Vector2Int((int)transform.localPosition.x, (int)transform.localPosition.y);

        DistTo = mapa.getDistTo(from.y, from.x);
        EdgeTo = new Vector2Int[mapa.altoMapa, mapa.anchoMapa];
        for (int i = 0; i < mapa.altoMapa; i++)
        {
            for (int j = 0; j < mapa.anchoMapa; j++)
            {
                EdgeTo[i, j] = new Vector2Int(-1, -1);
            }
        }
        PQ.EnqueueWithoutDuplicates(from, 0);
        caminoPosible = false;


        int k = 0;

        while (PQ.Count > 0)
        {
            k++;
            Vector2Int top = PQ.Dequeue();
            if (top != UltimaCasilla)
            {
                for (int i = 0; i < directions.Length; i++)
                {
                    relax(top, directions[i]);
                }
            }
            else
            {
                caminoPosible = true;
            }
        }
        stopwatch.Stop();
        if (caminoPosible)
        {
            GetComponent <Agente>().setPath(GetPath(ref UltimaCasilla, ref from));
        }



        return(caminoPosible);
    }
예제 #3
0
    public Stack <Vector2Int> CalculatePath(Vector2Int casilla)
    {
        UltimaCasilla = new Vector2Int((int)casilla.x, (int)casilla.y);
        PQ            = new Priority_Queue.SimplePriorityQueue <Vector2Int, int>();
        Vector2Int from = new Vector2Int((int)transform.localPosition.x, (int)transform.localPosition.y);

        DistTo = mapa.getDistTo(from.y, from.x);
        EdgeTo = new Vector2Int[mapa.altoMapa, mapa.anchoMapa];
        for (int i = 0; i < mapa.altoMapa; i++)
        {
            for (int j = 0; j < mapa.anchoMapa; j++)
            {
                EdgeTo[i, j] = new Vector2Int(-1, -1);
            }
        }
        PQ.EnqueueWithoutDuplicates(from, 0);
        caminoPosible = false;


        int k = 0;

        while (PQ.Count > 0)
        {
            k++;
            Vector2Int top = PQ.Dequeue();
            if (top != UltimaCasilla)
            {
                for (int i = 0; i < directions.Length; i++)
                {
                    relax(top, directions[i]);
                }
            }
            else
            {
                caminoPosible = true;
            }
        }



        return(GetPath(ref UltimaCasilla, ref from));
    }