Exemple #1
0
    private Vector3 mouseCellPos;          // The grid cell the mouse is currently in.

    void Start()
    {
        towerToPlace   = tower[GameManager.instance.towerSelection];
        highLightImage = Instantiate(highLightImage, transform.position, Quaternion.identity);
        highLightImage.SetActive(false);

        // Create the main grid for renderering all objects.
        grid = new WorldGrid(Width, Height);
        for (int i = 0; i < grid.Width; i++)
        {
            for (int j = 0; j < grid.Height; j++)
            {
                // Create the inital game objects used for rendering the grid and the towers
                GameObject tileBlock  = new GameObject(i.ToString() + ", " + j.ToString());
                GameObject tileBorder = new GameObject("Border");
                GameObject tileObject = new GameObject("Tile");

                // Set the position of the grid cells on the screen. (Centered)
                tileBorder.transform.position = new Vector3(i - ((grid.Width - 1f) / 2), j - ((grid.Height - 1f) / 2), 0);
                tileObject.transform.position = new Vector3(i - ((grid.Width - 1f) / 2), j - ((grid.Height - 1f) / 2), 0);

                // Create the sprite renderers for holding the sprites
                SpriteRenderer tileBorderRenderer = tileBorder.AddComponent <SpriteRenderer>();
                SpriteRenderer TileObjRenderer    = tileObject.AddComponent <SpriteRenderer>();

                // Set the layers rendering order. (Might not be necessary in this case.)
                tileBorderRenderer.sortingLayerName = "Grid Border";
                TileObjRenderer.sortingLayerName    = "Grid Object";

                // Just used for organizing game objects in the editor.
                tileBorder.transform.SetParent(tileBlock.transform);
                tileObject.transform.SetParent(tileBlock.transform);
                tileBlock.transform.SetParent(transform);

                GridTile t = grid.GetTile(i, j);
                if (t.TileBorder == null)
                {
                    // Set the current tile grid sprite
                    t.TileBorder = gridImage;

                    // Render initial border tiles to the screen (Make them black and slightly transparent)
                    tileBorderRenderer.sprite = t.TileBorder.GetComponent <SpriteRenderer>().sprite;
                    tileBorderRenderer.GetComponent <SpriteRenderer>().color = new Color(0f, 0f, 0f, .25f);
                }

                // Pass the grid objects created to update function so we can update the grid later. Register the callback.
                t.RegisterCB((GridTile) => { OnGridCellUpdated(t, tileBorder, tileObject); });
            }
        }
    }