Ejemplo n.º 1
0
    private void SetupGrid(Vector3 areaStart, Vector3 areaSize, Vector3 scale)
    {
        int cols = (int)(areaSize.x / gridSize);
        int rows = (int)(areaSize.z / gridSize);

        // Sets up the container for the grid lines
        GameObject gridLineContainer          = new GameObject("GridLineContainer");
        Transform  gridLineContainerTransform = gridLineContainer.GetComponent <Transform>();

        SetupContainerTransform(gridLineContainerTransform);


        // Sets up the container for the grid squares
        GameObject gridSquareContainer = new GameObject("GridSquareContainer");

        Transform gridSquareContainerTransform = gridSquareContainer.GetComponent <Transform>();

        SetupContainerTransform(gridSquareContainerTransform);
        gridSquareContainer.tag = "GridHandler";


        // Spawn the vertical lines.
        for (int i = 0; i <= cols; i++)
        {
            // Prepare the positions for the vertices in the line.
            float     linePosX  = areaStart.x + i * gridSize;
            Vector3[] positions = { new Vector3(linePosX, areaStart.y, areaStart.z),
                                    new Vector3(linePosX, areaStart.y, areaStart.z + areaSize.z) };

            SpawnGridLine(positions, scale.x, gridLineContainerTransform);
        }

        // Spawn the horizontal lines.
        for (int i = 0; i <= rows; i++)
        {
            float     linePosZ  = areaStart.z + i * gridSize;
            Vector3[] positions = { new Vector3(areaStart.x,              areaStart.y, linePosZ),
                                    new Vector3(areaStart.x + areaSize.x, areaStart.y, linePosZ) };

            SpawnGridLine(positions, scale.y, gridLineContainerTransform);
        }

        // Offsets the areaStart to make sure the grid squares are spawned in the correct positions.
        areaStart.x += 0.5f * gridSize;
        areaStart.y -= 0.0025f;
        areaStart.z += 0.5f * gridSize;

        // Adds a GridHandler to the EnvironmentSetup.
        gridSquareContainer.AddComponent <GridHandler>();
        GridHandler gridSquareContainerGridHandler = gridSquareContainer.GetComponent <GridHandler>();

        gridSquareContainerGridHandler.init(rows, cols, gridSize, scale);

        // Used for debugging purposes.
        int added    = 0;
        int notAdded = 0;

        // Create the grid slots.
        for (int x = 0; x < cols; x++)
        {
            // Calculate x-coordinate.
            float xCoord = areaStart.x + x * gridSize;

            for (int z = 0; z < rows; z++)
            {
                // Calculates the z-coordinate.
                float zCoord = areaStart.z + z * gridSize;
                // Position vector of the GridSquare.
                Vector3 pos = new Vector3(xCoord, areaStart.y, zCoord);

                // Spawn the GridSquare and move it to its position.
                GameObject gs = Instantiate(gridSquare, gridSquareContainerTransform);
                gs.GetComponent <Transform>().localPosition = pos;

                // Enable snap to grid if wanted.
                if (enableSnapToGrid)
                {
                    gs.GetComponent <SnapToGrid>().enabled = true;
                }

                // Checks how many of the GridSquares were spawned in proper positions.
                if (gridSquareContainerGridHandler.AddToMap(gs))
                {
                    added++;
                }
                else
                {
                    notAdded++;
                    Destroy(gs);
                }
            }
        }

        Debug.Log("Added " + added + " grid squares and failed to add " + notAdded + " grid squares.");
    }