예제 #1
0
    private void generateGrid()
    {
        Vector2 bottomLeft = mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f));
        Vector2 topRight   = mainCam.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0f));

        if (GameSetup.debugMode)
        {
            Debug.Log("Rect2: " + mainCam.ScreenToWorldPoint(panel.GetComponent <RectTransform>().rect.size));
            Debug.Log("Size2: " + mainCam.ScreenToWorldPoint(panel.GetComponent <RectTransform>().sizeDelta));
            Debug.Log("Pos2: " + mainCam.ScreenToWorldPoint(panel.GetComponent <RectTransform>().position));
            Debug.Log("TopRight: " + topRight);
        }
        topRight.x -= menuBarSize;

        int matrixX = 0;

        for (float x = bottomLeft.x + spriteSize / 2f; x < topRight.x; x = x + spriteSize)
        {
            int       matrixY     = 0;
            ArrayList spritesLine = new ArrayList();
            for (float y = bottomLeft.y + spriteSize / 2f; y < topRight.y; y = y + spriteSize)
            {
                GameObject newSpriteObject = (GameObject)Instantiate(gridSprite, new Vector3(x, y, 0), Quaternion.identity);
                spritesLine.Add(newSpriteObject);
                newSpriteObject.GetComponent <Transform>().GetComponent <Renderer>().enabled = true;
                GridElement ownScript = newSpriteObject.GetComponent <GridElement>();
                if (matrixY > 0)
                {
                    GridElement leftNeighbour = ((GameObject)spritesLine[matrixY - 1]).GetComponent <GridElement>();
                    leftNeighbour.addNeighbour(ownScript);
                    ownScript.addNeighbour(leftNeighbour);
                }
                matrixY++;
            }
            sprites.Add(spritesLine);
            if (matrixX > 0)
            {
                ArrayList previousLine = (ArrayList)sprites[matrixX - 1];
                for (int i = 0; i < previousLine.Count; i++)
                {
                    GridElement element1 = ((GameObject)spritesLine[i]).GetComponent <GridElement>();
                    GridElement element2 = ((GameObject)previousLine[i]).GetComponent <GridElement>();
                    element1.addNeighbour(element2);
                    element2.addNeighbour(element1);
                }
            }
            matrixX++;
        }
        numSprites           = new Vector2(sprites.Count, ((ArrayList)sprites[0]).Count);
        numberOfGridElements = (int)numSprites.x * (int)numSprites.y;
        Debug.Log("number of grid elements: " + numSprites.x + " x " + numSprites.y);
    }