Esempio n. 1
0
    List <edgeScript> initEdgesFromFile()
    {
        List <edgeScript> edges = new List <edgeScript>();

        string path;

        if (graphNo == 1)
        {
            path = "Assets/vertices1.json";
        }
        else
        {
            path = "Assets/vertices2.json";
        }
        string[]     vertexData;
        int[][]      vertexDataInt;
        StreamReader reader           = new StreamReader(path);
        string       verticesFromFile = reader.ReadToEnd();

        reader.Close();
        verticesFromFile = verticesFromFile.Substring(1, verticesFromFile.Length - 2);
        vertexData       = verticesFromFile.Split(']');

        vertexDataInt = new int[vertexData.Length - 1][];

        for (int i = 0; i < vertexData.Length; i++)
        {
            vertexData[i] = vertexData[i].Trim(' ', ',', '[');

            if (i < vertexData.Length - 1)
            {
                vertexDataInt[i] = stringToInt(vertexData[i]);
            }
        }

        int counter = 0;

        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                string       name = graphNo + " - " + i + "," + j;
                GameObject   v    = GameObject.Find(name);
                vertexScript vs   = v.GetComponent <vertexScript>();

                if (i > 0 && horAndVer)//Add North Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3(j, (-i + 0.5f), 0), Quaternion.Euler(0, 0, 90));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 90));
                    }
                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i - 1), j);
                    if (vertexDataInt[counter][1] == 1)
                    {
                        string       nameB = graphNo + " - " + (i - 1) + "," + j;
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i - 1), j, edge, e, vs, vsB);
                        edges.Add(e);
                    }
                    else
                    {
                        Destroy(edge);
                    }
                }

                if (i > 0 && j > 0 && diag)//Add North-West Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j - 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, -45));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, -45));
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i - 1), (j - 1));
                    if (vertexDataInt[counter][0] == 1)
                    {
                        string       nameB = graphNo + " - " + (i - 1) + "," + (j - 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i - 1), (j - 1), edge, e, vs, vsB);
                        edges.Add(e);
                    }
                    else
                    {
                        Destroy(edge);
                    }
                }

                if (i > 0 && j < size - 1 && diag)//Add North-East Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 45));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 45));
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i - 1), (j + 1));
                    if (vertexDataInt[counter][2] == 1)
                    {
                        string       nameB = graphNo + " - " + (i - 1) + "," + (j + 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i - 1), (j + 1), edge, e, vs, vsB);
                        edges.Add(e);
                    }
                    else
                    {
                        Destroy(edge);
                    }
                }

                if (i < size - 1 && horAndVer)//Add South Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3(j, (-i - 0.5f), 0), Quaternion.Euler(0, 0, 90));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3(j + size, (-i - 0.5f), 0), Quaternion.Euler(0, 0, 90));
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i + 1), j);
                    if (vertexDataInt[counter][6] == 1)
                    {
                        string       nameB = graphNo + " - " + (i + 1) + "," + j;
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i + 1), j, edge, e, vs, vsB);
                        edges.Add(e);
                    }

                    else
                    {
                        Destroy(edge);
                    }
                }

                if (i < size - 1 && j > 0 && diag)//Add South-West Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j - 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, 45));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, 45));
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i + 1), (j - 1));
                    if (vertexDataInt[counter][5] == 1)
                    {
                        string       nameB = graphNo + " - " + (i + 1) + "," + (j - 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i + 1), (j - 1), edge, e, vs, vsB);
                        edges.Add(e);
                    }
                    else
                    {
                        Destroy(edge);
                    }
                }

                if (i < size - 1 && j < size - 1 && diag)//Add South-East Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, -45));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, -45));
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i + 1), (j + 1));
                    if (vertexDataInt[counter][7] == 1)
                    {
                        string       nameB = graphNo + " - " + (i + 1) + "," + (j + 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i + 1), (j + 1), edge, e, vs, vsB);
                        edges.Add(e);
                    }
                    else
                    {
                        Destroy(edge);
                    }
                }

                if (j > 0 && horAndVer)//Add West Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j - 0.5f), -i, 0), Quaternion.identity);
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), -i, 0), Quaternion.identity);
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, i, (j - 1));
                    if (vertexDataInt[counter][3] == 1)
                    {
                        string       nameB = graphNo + " - " + i + "," + (j - 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, i, (j - 1), edge, e, vs, vsB);
                        edges.Add(e);
                    }

                    else
                    {
                        Destroy(edge);
                    }
                }

                if (j < size - 1 && horAndVer)//Add East Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + 0.5f), -i, 0), Quaternion.identity);
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), -i, 0), Quaternion.identity);
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, i, (j + 1));
                    if (vertexDataInt[counter][4] == 1)
                    {
                        string       nameB = graphNo + " - " + i + "," + (j + 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, i, (j + 1), edge, e, vs, vsB);
                        edges.Add(e);
                    }

                    else
                    {
                        Destroy(edge);
                    }
                }

                counter++;
            }
        }

        return(edges);
    }
Esempio n. 2
0
    void initEdges()
    {
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                string       name = graphNo + " - " + i + "," + j;
                GameObject   v    = GameObject.Find(name);
                vertexScript vs   = v.GetComponent <vertexScript>();

                if (i > 0 && horAndVer)//Add North Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3(j, (-i + 0.5f), 0), Quaternion.Euler(0, 0, 90));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 90));
                    }
                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i - 1), j);
                    if (!edgeExists(e))
                    {
                        string       nameB = graphNo + " - " + (i - 1) + "," + j;
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i - 1), j, edge, e, vs, vsB);
                    }
                    else
                    {
                        Destroy(edge);
                    }
                }

                if (i > 0 && j > 0 && diag)//Add North-West Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j - 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, -45));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, -45));
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i - 1), (j - 1));
                    if (!edgeExists(e))
                    {
                        string       nameB = graphNo + " - " + (i - 1) + "," + (j - 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i - 1), (j - 1), edge, e, vs, vsB);
                    }
                    else
                    {
                        Destroy(edge);
                    }
                }

                if (i > 0 && j < size - 1 && diag)//Add North-East Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 45));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 45));
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i - 1), (j + 1));
                    if (!edgeExists(e))
                    {
                        string       nameB = graphNo + " - " + (i - 1) + "," + (j + 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i - 1), (j + 1), edge, e, vs, vsB);
                    }
                    else
                    {
                        Destroy(edge);
                    }
                }

                if (i < size - 1 && horAndVer)//Add South Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3(j, (-i - 0.5f), 0), Quaternion.Euler(0, 0, 90));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3(j + size, (-i - 0.5f), 0), Quaternion.Euler(0, 0, 90));
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i + 1), j);
                    if (!edgeExists(e))
                    {
                        string       nameB = graphNo + " - " + (i + 1) + "," + j;
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i + 1), j, edge, e, vs, vsB);
                    }

                    else
                    {
                        Destroy(edge);
                    }
                }

                if (i < size - 1 && j > 0 && diag)//Add South-West Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j - 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, 45));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, 45));
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i + 1), (j - 1));
                    if (!edgeExists(e))
                    {
                        string       nameB = graphNo + " - " + (i + 1) + "," + (j - 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i + 1), (j - 1), edge, e, vs, vsB);
                    }
                    else
                    {
                        Destroy(edge);
                    }
                }

                if (i < size - 1 && j < size - 1 && diag)//Add South-East Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, -45));
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, -45));
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, (i + 1), (j + 1));
                    if (!edgeExists(e))
                    {
                        string       nameB = graphNo + " - " + (i + 1) + "," + (j + 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, (i + 1), (j + 1), edge, e, vs, vsB);
                    }
                    else
                    {
                        Destroy(edge);
                    }
                }

                if (j > 0 && horAndVer)//Add West Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j - 0.5f), -i, 0), Quaternion.identity);
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), -i, 0), Quaternion.identity);
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, i, (j - 1));
                    if (!edgeExists(e))
                    {
                        string       nameB = graphNo + " - " + i + "," + (j - 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, i, (j - 1), edge, e, vs, vsB);
                    }

                    else
                    {
                        Destroy(edge);
                    }
                }

                if (j < size - 1 && horAndVer)//Add East Edge
                {
                    GameObject edge;

                    if (graphNo == 1)
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + 0.5f), -i, 0), Quaternion.identity);
                    }
                    else
                    {
                        edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), -i, 0), Quaternion.identity);
                    }

                    edge.transform.parent = v.transform;
                    edgeScript e = edge.GetComponent <edgeScript>();
                    e.setVertices(i, j, i, (j + 1));
                    if (!edgeExists(e))
                    {
                        string       nameB = graphNo + " - " + i + "," + (j + 1);
                        GameObject   vB    = GameObject.Find(nameB);
                        vertexScript vsB   = vB.GetComponent <vertexScript>();
                        createEdge(graphNo, i, j, i, (j + 1), edge, e, vs, vsB);
                    }

                    else
                    {
                        Destroy(edge);
                    }
                }

                //vertices[i,j] = vs;
            }
        }
    }