Exemplo n.º 1
0
    /// <summary>
    /// Instantiates a new tile at the coord passed in
    /// </summary>
    /// <param name="coord">The coord to be used</param>
    private void PlaceTile(OffsetCoord coord)
    {
        // Create the object
        var        hexPrefabObject = hexPrefabs[Random.Range(0, hexPrefabs.Count)];
        GameObject hex             = Instantiate(
            hexPrefabObject,
            transform.position + CalculateWorldPosition(new Vector2(coord.Column, coord.Row)),
            Quaternion.Euler(new Vector3(0, Random.Range(0, 5) * 60, 0)),
            this.transform);

        hex.name = string.Format("Hex {0} {1}", coord.Column, coord.Row);

        // Set up the object
        HexObject hexObject = hex.GetComponent <HexObject>();

        hexObject.hex   = new Hex(CubeCoord.OddRowToCube(coord));
        hexObject.Index = Instance.Hexes.Count;
        Instance.Hexes.Add(hexObject);
    }
Exemplo n.º 2
0
    /// <summary>
    /// Main function for creating the objects in the grid
    /// </summary>
    private void CreateCell(int x, int z, int i)
    {
        /////////////////////////////////
        /// Create Cell and populate data
        /////////////////////////////////

        Vector3   position  = new Vector3(((x + z * 0.5f - z / 2) * HexMetrics.Instance.innerRadius * 2f), 0f, -(z * (HexMetrics.Instance.outerRadius * 1.5f)));
        HexObject hexObject = Hexes[i] = Instantiate(hexPrefab);

        hexObject.Index = i;
        hexObject.Hex   = new Hex(CubeCoord.OddRowToCube(new OffsetCoord(x, z)));
        hexObject.Color = MapGenerator.Instance.colorMap[i];
        hexObject.transform.localPosition = position;
        hexObject.name      += " " + hexObject.Hex.cubeCoords.ToString();
        hexObject.Elevation  = MapGenerator.Instance.heightMap[x, z];
        hexObject.WaterLevel = (hexObject.Elevation <= 0.4f) ? .1f : 0f;

        ///////////////////////////////////////////////////////
        /// Set a starting plant level for hex's that are green
        ///////////////////////////////////////////////////////

        if (hexObject.Color == MapGenerator.Instance.regions.Where(r => r.name == "Grass").FirstOrDefault().color ||
            hexObject.Color == MapGenerator.Instance.regions.Where(r => r.name == "Grass 2").FirstOrDefault().color)
        {
            Random.State currentState = Random.state;
            Random.InitState(MapGenerator.Instance.mapSettings.noiseSetting.seed);

            hexObject.PlantLevel = Random.Range(0, 3);

            Random.state = currentState;
        }

        /////////////////////
        /// Set up Neighbours
        /////////////////////

        if (x > 0)
        {
            hexObject.SetNeighbour(HexDirection.W, Hexes[i - 1]);
        }
        if (z > 0)
        {
            if ((z & 1) == 0)
            {
                hexObject.SetNeighbour(HexDirection.NE, Hexes[i - Width]);
                if (x > 0)
                {
                    hexObject.SetNeighbour(HexDirection.NW, Hexes[i - Width - 1]);
                }
            }
            else
            {
                hexObject.SetNeighbour(HexDirection.NW, Hexes[i - Width]);
                if (x < Width - 1)
                {
                    hexObject.SetNeighbour(HexDirection.NE, Hexes[i - Width + 1]);
                }
            }
        }

        AddHexToChunk(x, z, hexObject);
    }