예제 #1
0
    public void generarGrillaDeNavegacion()
    {
        GameObject auxCol;
        GameObject auxObj;

        auxObj = new GameObject("Navegacion");
        auxObj.transform.parent = transform;

        puntosNavegables = new PuntoNavegable[puntosNavegablesY][];
        distX            = areaCaminable.bounds.extents.x * 2 / puntosNavegablesX;
        distY            = areaCaminable.bounds.extents.y * 2 / puntosNavegablesY;
        float auxX = areaCaminable.bounds.min.x;
        float auxY = areaCaminable.bounds.min.y;

        for (var i = 0; i < puntosNavegablesY; i++)
        {
            puntosNavegables[i] = new PuntoNavegable[puntosNavegablesX];
            for (var j = 0; j < puntosNavegablesX; j++)
            {
                if (areaCaminable.OverlapPoint(new Vector2(auxX, auxY)))
                { //Si el punto esta dentro del polygonCollider2D
                    //Creo el collider del punto de navegacion
                    auxCol = Instantiate(puntoNavegacion, auxObj.transform, true);
                    auxCol.transform.position = new Vector3(auxX, auxY, 0);
                    auxCol.GetComponent <BoxCollider2D>().size = new Vector2(distX, distY);
                    auxCol.GetComponent <NavPunto>().i         = i;
                    auxCol.GetComponent <NavPunto>().j         = j;
                    auxCol.gameObject.name = auxCol.gameObject.name + "-" + i.ToString() + "-" + j.ToString();

                    //Creo el punto navegable
                    puntosNavegables[i][j] = new PuntoNavegable(new Vector3(auxX, auxY, 0), j, i, auxCol);

                    //Armo las aristas
                    if ((j > 0) && (puntosNavegables[i][j - 1] != null))
                    {
                        puntosNavegables[i][j].hermanos.Add(puntosNavegables[i][j - 1]);
                        puntosNavegables[i][j - 1].hermanos.Add(puntosNavegables[i][j]);
                    }

                    if ((i > 0) && (puntosNavegables[i - 1][j] != null))
                    {
                        puntosNavegables[i][j].hermanos.Add(puntosNavegables[i - 1][j]);
                        puntosNavegables[i - 1][j].hermanos.Add(puntosNavegables[i][j]);
                    }

                    if ((i > 0) && (j > 0) && (puntosNavegables[i - 1][j - 1] != null))
                    {
                        puntosNavegables[i][j].hermanos.Add(puntosNavegables[i - 1][j - 1]);
                        puntosNavegables[i - 1][j - 1].hermanos.Add(puntosNavegables[i][j]);
                    }

                    if ((j < puntosNavegables.Length - 2) && (i > 0) && (puntosNavegables[i - 1][j + 1] != null))
                    {
                        puntosNavegables[i][j].hermanos.Add(puntosNavegables[i - 1][j + 1]);
                        puntosNavegables[i - 1][j + 1].hermanos.Add(puntosNavegables[i][j]);
                    }
                }
                else
                {
                    puntosNavegables[i][j] = null;
                }
                auxX += distX;
            }
            auxX  = areaCaminable.bounds.min.x;
            auxY += distY;
        }
        areaCaminable.enabled = false;
    }
예제 #2
0
 //Obtiene la distancia heurística entre nodos
 public float distHeuristica(PuntoNavegable a, PuntoNavegable b)
 {
     return(Vector3.Distance(a.objeto.transform.position, b.objeto.transform.position));
 }