示例#1
0
    void AddWallSegment(
        Vector3 pivot, HexCell pivotCell,
        Vector3 left, HexCell leftCell,
        Vector3 right, HexCell rightCell
        )
    {
        if (pivotCell.IsUnderwater)
        {
            return;
        }

        bool hasLeftWall = !leftCell.IsUnderwater &&
                           pivotCell.GetEdgeType(leftCell) != HexEdgeType.Cliff;
        bool hasRighWall = !rightCell.IsUnderwater &&
                           pivotCell.GetEdgeType(rightCell) != HexEdgeType.Cliff;

        if (hasLeftWall)
        {
            if (hasRighWall)
            {
                bool hasTower = false;
                if (leftCell.Elevation == rightCell.Elevation)
                {
                    HexHash hash = HexMetrics.SampleHashGrid(
                        (pivot + left + right) * (1f / 3f)
                        );
                    hasTower = hash.e < HexMetrics.wallTowerThreshold;
                }
                AddWallSegment(pivot, left, pivot, right, hasTower);
            }
            else if (leftCell.Elevation < rightCell.Elevation)
            {
                AddWallWedge(pivot, left, right);
            }
            else
            {
                AddWallCap(pivot, left);
            }
        }
        else if (hasRighWall)
        {
            if (rightCell.Elevation < leftCell.Elevation)
            {
                AddWallWedge(right, pivot, left);
            }
            else
            {
                AddWallCap(right, pivot);
            }
        }
    }
示例#2
0
    public static void InitializeHashGrid(int seed)
    {
        hashGrid = new HexHash[hashGridSize * hashGridSize];

        var currentState = Random.state;

        Random.InitState(seed);
        for (var i = 0; i < hashGrid.Length; i++)
        {
            hashGrid[i] = HexHash.Create();
        }

        Random.state = currentState;
    }
示例#3
0
    public void AddFeature(HexCell cell, Vector3 position)
    {
        HexHash   hash   = HexMetrics.SampleHashGrid(position);
        Transform prefab = PickPrefab(
            urbanCollections, cell.UrbanLevel, hash.a, hash.d
            );
        Transform otherPrefab = PickPrefab(
            farmCollections, cell.FarmLevel, hash.b, hash.d
            );
        float usedHash = hash.a;

        if (prefab)
        {
            if (otherPrefab && hash.b < hash.a)
            {
                prefab   = otherPrefab;
                usedHash = hash.b;
            }
        }
        else if (otherPrefab)
        {
            prefab   = otherPrefab;
            usedHash = hash.b;
        }
        otherPrefab = PickPrefab(
            plantCollections, cell.PlantLevel, hash.c, hash.d
            );
        if (prefab)
        {
            if (otherPrefab && hash.c < usedHash)
            {
                prefab = otherPrefab;
            }
        }
        else if (otherPrefab)
        {
            prefab = otherPrefab;
        }
        else
        {
            return;
        }

        Transform instance = Instantiate(prefab);

        position.y            += instance.localScale.y * 0.5f;
        instance.localPosition = HexMetrics.Perturb(position);
        instance.localRotation = Quaternion.Euler(0f, 360f * hash.e, 0f);
        instance.SetParent(container, false);
    }
示例#4
0
    public static void InitializeHashGrid(int seed)
    {
        Instance._hashGrid = new HexHash[Instance._hashGridSize * Instance._hashGridSize];

        Random.State currentState = Random.state;
        Random.InitState(seed);

        for (int i = 0; i < Instance._hashGrid.Length; i++)
        {
            Instance._hashGrid[i] = HexHash.Create();
        }

        Random.state = currentState;
    }
    public void AddFeature(HexCell cell, Vector3 position)
    {
        HexHash hash = HexMetrics.SampleHashGrid(position);

        if (hash.a >= cell.UrbanLevel * 0.25f)
        {
            return;
        }
        Transform instance = Instantiate(featurePrefab);

        position.y            += instance.localScale.y * 0.5f;
        instance.localPosition = HexMetrics.Perturb(position);
        instance.localRotation = Quaternion.Euler(0f, 360f * hash.b, 0f);
        instance.SetParent(container, false);
    }
    /// <summary>
    /// Inserts random values into the hashgrid.
    /// </summary>
    public static void InitializeHashGrid(int seed)
    {
        UnityEngine.Random.State currentState = UnityEngine.Random.state;

        _hashGrid = new HexHash[HashGridSize * HashGridSize];
        UnityEngine.Random.InitState(seed);
        for (int i = 0; i < _hashGrid.Length; i++)
        {
            _hashGrid[i] = HexHash.Create();
        }

        // Random generator need to restore its previous status
        // in order to avoid all random event to be exactly the same
        UnityEngine.Random.state = currentState;
    }
示例#7
0
    /// <summary>
    /// 创建一段围墙(角落围墙)
    /// 当第一个六边形没有围墙时,第二第三个六边形必须要有围墙
    /// 当第一个六边形有围墙时,第二第三个六边形必须没有围墙
    /// </summary>
    /// <param name="pivot">与自身距离最近的三角形顶点</param>
    /// <param name="pivotCell">自身六边形</param>
    /// <param name="left">三角形左边顶点</param>
    /// <param name="leftCell">角落左边的邻居</param>
    /// <param name="right">三角形右边顶点</param>
    /// <param name="rightCell">角落右边的邻居</param>
    void AddWallSegment(Vector3 pivot, HexCell pivotCell, Vector3 left, HexCell leftCell, Vector3 right, HexCell rightCell)
    {
        // 如果中枢六边形在水下
        if (pivotCell.IsUnderwater)
        {
            return;
        }

        // 左边是否要有围墙(左侧六边形不在水下并且与中枢六边形之间不是陡坡)
        bool hasLeftWall = !leftCell.IsUnderwater && pivotCell.GetEdgeType(leftCell) != HexEdgeType.Cliff;
        // 右边是否要有围墙(右侧六边形不在水下并且与中枢六边形之间不是陡坡)
        bool hasRighWall = !rightCell.IsUnderwater && pivotCell.GetEdgeType(rightCell) != HexEdgeType.Cliff;

        if (hasLeftWall)
        {
            if (hasRighWall)
            {
                bool hasTower = false;
                // 判断两边高度, 斜坡不生成塔楼
                if (leftCell.Elevation == rightCell.Elevation)
                {
                    HexHash hash = HexMetrics.SampleHashGrid((pivot + left + right) * (1f / 3f)); // 用角落中点对散列网络进行取样,得到坐标对应的随机值
                    hasTower = hash.e < HexMetrics.wallTowerThreshold;
                }
                AddWallSegment(pivot, left, pivot, right, hasTower);
            }
            // 右边没有墙壁且右侧六边形比左侧高,则表明右侧一个陡坡
            else if (leftCell.Elevation < rightCell.Elevation)
            {
                AddWallWedge(pivot, left, right);
            }
            else
            {
                AddWallCap(pivot, left);         // 有一边没有围墙的时候两侧墙边需要添加四边形
            }
        }
        else if (hasRighWall)
        {
            // 左边没有墙壁且左侧六边形比右侧高,则表明左侧一个陡坡
            if (rightCell.Elevation < leftCell.Elevation)
            {
                AddWallWedge(right, pivot, left);
            }
            {
                AddWallCap(right, pivot);        // 有一边没有围墙的时候两侧墙边需要添加四边形
            }
        }
    }
    public void AddFeature(Vector3 position, HexCell cell)
    {
        HexHash   hash   = HexMetric.SampleHashGrid(position);
        Transform prefab = PickPrefab(cell.UrbanLevel, 0, 0);

        if (!prefab)
        {
            return;
        }
        Transform instance = Instantiate(prefab);

        position.y            += instance.localScale.y * 0.5f;
        instance.localPosition = HexMetric.Perturb(position);
        instance.localRotation = Quaternion.Euler(0f, 360f, 0f);
        instance.SetParent(container, false);
    }
    public void AddSpecialFeature(HexCell cell, Vector3 position)
    {
        HexHash   hash   = HexMetrics.SampleHashGrid(position);
        Transform prefab = null;

        prefab = PickSpecialPrefab(
            specialCollection, cell, hash.a
            );

        Transform instance = Instantiate(prefab);

        instance.localPosition = position;
        instance.localRotation = Quaternion.Euler(0f,
                                                  lockRotation[Mathf.Clamp(Mathf.RoundToInt(
                                                                               lockRotation.Length * hash.d), 0, 5)], 0f);
        instance.SetParent(container, false);
    }
示例#10
0
    public void AddFeature(HexCell cell, Vector3 position)
    {
        if (cell.HasSpecialFeature)
        {
            return;
        }

        HexHash hexHash = HexMetrics.SampleHashGrid(position);
        //if (hexHash.a >= 0.25f * cell.UrbanDensityLevel)
        //{
        //    return;
        //}


        Transform urbanFeature = PickFeaturePrefab(UrbanFeatureCollections, cell.UrbanDensityLevel, hexHash.a, hexHash.choice);
        Transform farmFeature  = PickFeaturePrefab(FarmFeatureCollections, cell.FarmDensityLevel, hexHash.b, hexHash.choice);
        Transform plantFeature = PickFeaturePrefab(PlantFeatureCollections, cell.PlantDensityLevel, hexHash.c, hexHash.choice);

        float     defaultMax      = 1000;
        Transform chosenTransform = null;
        float     usedHash        = Mathf.Min(urbanFeature != null ? hexHash.a : defaultMax, farmFeature != null ? hexHash.b : defaultMax, plantFeature != null ? hexHash.c : defaultMax);

        if (usedHash == hexHash.a)
        {
            chosenTransform = urbanFeature;
        }
        else if (usedHash == hexHash.b)
        {
            chosenTransform = farmFeature;
        }
        else if (usedHash == hexHash.c)
        {
            chosenTransform = plantFeature;
        }

        if (chosenTransform != null)
        {
            Transform instance = Instantiate(chosenTransform);
            position.y            += instance.localScale.y * 0.5f;
            instance.localPosition = HexMetrics.PerturbVector(position);
            instance.localRotation = Quaternion.Euler(0f, 360f * hexHash.randomRotation, 0f);
            instance.SetParent(m_container, false);
        }
    }
示例#11
0
    public void AddFeature(HexCell cell, Vector3 position)
    {
        if (cell.IsSpecial)
        {
            return;
        }

        HexHash   hash   = HexMetrics.SampleHashGrid(position);
        Transform prefab = PickPrefab(
            urbanCollections, cell.UrbanLevel, hash.a, hash.d
            );

        if (!prefab)
        {
            prefab = PickPrefab(
                farmCollections, cell.FarmLevel, hash.b, hash.d
                );
        }
        if (!prefab)
        {
            prefab = PickPrefab(
                plantCollections, cell.PlantLevel, hash.c, hash.d
                );
        }
        if (!prefab)
        {
            prefab = PickPrefab(
                mineCollections, cell.MineLevel, hash.c, hash.d
                );
        }
        if (!prefab)
        {
            return;
        }

        Transform instance = Instantiate(prefab);

        position.y            += instance.localScale.y * 0.5f;
        instance.localPosition = HexMetrics.Perturb(position);
        instance.localRotation = Quaternion.Euler(0f, 360f * hash.e, 0f);
        instance.SetParent(container, false);
    }
示例#12
0
    //Special Features
    public void AddSpecialFeature(HexCell cell, Vector3 position)
    {
        if (special.Length >= cell.SpecialIndex)
        {
            Transform instance = Instantiate(special[cell.SpecialIndex - 1]);

            //Find the HexInteractable component in the special feature
            HexInteractable[] hexint = instance.GetComponentsInChildren <HexInteractable>();
            if (hexint.Length > 0)
            {
                foreach (HexInteractable hi in hexint)
                {
                    hi.Cell = cell;
                }
            }

            instance.localPosition = HexMetrics.Perturb(position);
            HexHash hash = HexMetrics.SampleHashGrid(position);
            instance.localRotation = Quaternion.Euler(0f, 360f * hash.e, 0f);
            instance.SetParent(container, false);
        }
    }
示例#13
0
    public void AddFeature(HexCell hexCell, Vector3 position)
    {
        HexHash   hash        = Hex.SampleHashGrid(position);
        Transform prefab      = PickPrefab(treeCollections, hexCell.TreeLevel, hash.a, hash.d);
        Transform otherPrefab = PickPrefab(stoneCollection, hexCell.StoneLevel, hash.b, hash.d);

        if (prefab)
        {
            if (otherPrefab && hash.b < hash.a)
            {
                prefab = otherPrefab;
            }
        }
        else if (otherPrefab)
        {
            prefab = otherPrefab;
        }
        else
        {
            return; // third option
        }

        Transform instance = Instantiate(prefab);

        Vector3 result = Hex.Perturb(position);

        /*
         * if(!isStone)
         *  result.y += 3f;
         */
        //float offset = 0.5f;
        //result.x += offset;
        //result.z += offset;

        instance.localPosition = result;
        instance.localRotation = Quaternion.Euler(0f, 360f * hash.e, 0f);
        instance.SetParent(container, false);
    }
    public void AddFeature(HexCell cell, Vector3 position)
    {
        if (cell.IsSpecialFeature)
        {
            return;
        }

        HexHash hash = HexMetrics.SampleHashGrid(position);

        Transform prefab      = null;
        Transform otherPrefab = null;

        switch (cell.FeatureType)
        {
        case 0:
            prefab = null;
            break;

        case 1:
            prefab = PickPrefab(
                pineTreeCollections, cell, hash.a, hash.c
                );
            break;

        case 2:
            prefab = PickPrefab(
                smallTreeCollections, cell, hash.a, hash.c
                );
            break;

        case 3:
            prefab = PickPrefab(
                largeTreeCollections, cell, hash.a, hash.c
                );
            break;
        }

        switch (cell.FeatureTwoType)
        {
        case 0:
            otherPrefab = null;
            break;

        case 1:
            otherPrefab = PickPrefabTwo(
                pineTreeCollections, cell, hash.b, hash.c
                );
            break;

        case 2:
            otherPrefab = PickPrefabTwo(
                smallTreeCollections, cell, hash.b, hash.c
                );
            break;

        case 3:
            otherPrefab = PickPrefabTwo(
                largeTreeCollections, cell, hash.b, hash.c
                );
            break;
        }

        //Pick prefab with lowest hash value
        if (cell.FeatureType != cell.FeatureTwoType)
        {
            if (prefab)
            {
                if (otherPrefab && hash.b < hash.a)
                {
                    prefab = otherPrefab;
                }
            }
            else if (otherPrefab)
            {
                prefab = otherPrefab;
            }
            else
            {
                return;
            }
        }

        if (prefab == null)
        {
            if (cell.GrassVariant > 0)
            {
                prefab = grassCollection[cell.GrassVariant - 1].Pick(hash.c);
            }
            else
            {
                return;
            }
        }

        Transform instance = Instantiate(prefab);

        instance.localPosition = position;
        instance.localRotation = Quaternion.Euler(0f, 360f * hash.d, 0f);
        instance.SetParent(container, false);
    }