Ejemplo n.º 1
0
    /// <summary>
    /// Initialize the hex map to an empty flat plain.
    /// </summary>
    /// <param name="bounds">
    ///     A bounds object representing the dimensions of the hex map. Will
    ///     be scaled to fit within a multiple of MeshConstants.ChunkSize.
    /// </param>
    /// <param name="wrapping">
    ///     Should the horizontal bounds of the grid wrap into their opposite
    ///     side?
    /// </param>
    /// <param name="editMode">
    ///     Should the map be editable immediately after being initialized?
    /// </param>
    /// <param name="hexOuterRadius">
    ///     The distance of each hex from its center to a circle
    ///     intersecting each corner of the hexagon. Scales the size of all
    ///     other visual elements on the hex map.
    /// </param>
    /// <param name="seed">
    ///     The random seed used to initialize the hash grid for the map.
    /// </param>
    public HexMap Initialize(
        Rect bounds,
        int seed,
        float hexOuterRadius,
        bool wrapping,
        bool editMode
        )
    {
        if (!GetComponent <HexMapShaderData>())
        {
            _hexShaderData        = gameObject.AddComponent <HexMapShaderData>();
            _hexShaderData.HexMap = this;
        }

        if (!_hexLabelPrefab)
        {
// TODO: This is a presentation concern and should not be in this class.
            _hexLabelPrefab = Resources.Load <Text>("Hex Label");
        }

        ClearHexUnits(_units);
        ClearColumnTransforms(HexMeshColumnTransforms);

        int columns, rows;

        if (
            bounds.x < 0 || bounds.y < 0 ||
            !bounds.size.IsFactorOf(HexMeshConstants.ChunkSize)
            )
        {
            RootLog.Log(
                "Unsupported map size. Clamping dimensions to chunk size.",
                Severity.Warning,
                "HexMap"
                );

            Vector2 clamped =
                bounds.size.ClampToFactorOf(HexMeshConstants.ChunkSize);

            bounds = new Rect(
                0,
                0,
                clamped.x,
                clamped.y
                );
        }

        columns = (int)bounds.width;
        rows    = (int)bounds.height;

        // Set to -1 so new maps always gets centered.
        CenterHexMeshColumnIndex = -1;

        HexagonPoint.InitializeHashGrid(seed);

        if (!_terrainMaterial)
        {
            _terrainMaterial = Resources.Load <Material>("Terrain");
        }

        HexGrid = new HexGrid <Hex>(
            rows,
            columns,
            wrapping
            );

        for (
            int index = 0, column = 0;
            column < HexOffsetColumns;
            column++
            )
        {
            for (
                int row = 0;
                row < HexOffsetRows;
                row++
                )
            {
                HexGrid[column, row] =
                    CreateHexFromOffsetCoordinates(
                        column, row, index++,
                        hexOuterRadius,
                        HexGrid
                        );
            }
        }

        HexMeshChunks = GetHexMeshChunks(
            HexGrid,
            hexOuterRadius
            );

        HexMeshColumnTransforms =
            GetHexMeshChunkColumns(HexMeshChunks);

        EditMode = editMode;

        return(this);
    }