Example #1
0
    /// <summary>
    /// 增加建筑
    /// </summary>
    /// <param name="addRate"></param>
    /// <param name="randomData"></param>
    /// <param name="startPosition"></param>
    /// <param name="buildingType"></param>
    public static bool AddBuilding(float addRate, uint randomData, Vector3Int startPosition, BuildingTypeEnum buildingType)
    {
        float randomRate;

        if (addRate < 0.00001f)
        {
            //概率小于万分之一的用RandomTools
            int         seed        = WorldCreateHandler.Instance.manager.GetWorldSeed();
            RandomTools randomTools = RandomUtil.GetRandom(seed, startPosition.x, startPosition.y, startPosition.z);
            //生成概率
            randomRate = randomTools.NextFloat();
        }
        else
        {
            randomRate = WorldRandTools.GetValue(startPosition, randomData);
        }
        if (randomRate < addRate)
        {
            BuildingInfoBean buildingInfo = BiomeHandler.Instance.manager.GetBuildingInfo(buildingType);

            List <BuildingBean> listBuildingData = buildingInfo.listBuildingData;

            int randomAngle = WorldRandTools.Range(0, 4, startPosition) * 90;

            for (int i = 0; i < listBuildingData.Count; i++)
            {
                BuildingBean buildingData   = listBuildingData[i];
                Vector3Int   targetPosition = startPosition + buildingData.GetPosition();
                float        createRate     = WorldRandTools.GetValue(targetPosition);

                if (buildingData.randomRate == 0 || createRate < buildingData.randomRate)
                {
                    VectorUtil.GetRotatedPosition(startPosition, targetPosition, new Vector3(0, randomAngle, 0));
                    WorldCreateHandler.Instance.manager.AddUpdateBlock(targetPosition, buildingData.blockId, (BlockDirectionEnum)buildingData.direction);
                }
            }
            return(true);
        }
        return(false);
    }
    /// <summary>
    /// 增加世界之树
    /// </summary>
    /// <param name="startPosition"></param>
    /// <param name="treeData"></param>
    public static void AddTreeForWorld(Vector3Int startPosition, BiomeForTreeData treeData)
    {
        //概率小于万分之一的用RandomTools
        int         seed        = WorldCreateHandler.Instance.manager.GetWorldSeed();
        RandomTools randomTools = RandomUtil.GetRandom(seed, startPosition.x, startPosition.y, startPosition.z);
        //生成概率
        float addRate = randomTools.NextFloat();
        //高度
        int treeHeight = WorldRandTools.Range(treeData.minHeight, treeData.maxHeight);

        if (addRate < treeData.addRate)
        {
            for (int y = 0; y < treeHeight; y++)
            {
                Vector3Int treeTrunkPosition = startPosition + Vector3Int.up * (y + 1);
                int        trunkRange        = treeData.trunkRange;
                for (int x = -trunkRange; x <= trunkRange; x++)
                {
                    for (int z = -trunkRange; z <= trunkRange; z++)
                    {
                        if ((x == -trunkRange || x == trunkRange) && (z == -trunkRange || z == trunkRange))
                        {
                            continue;
                        }
                        else if ((x == -trunkRange + 1 || x == trunkRange - 1) && (z == -trunkRange || z == trunkRange))
                        {
                            continue;
                        }
                        else if ((z == -trunkRange + 1 || z == trunkRange - 1) && (x == -trunkRange || x == trunkRange))
                        {
                            continue;
                        }
                        else
                        {
                            Vector3Int tempTrunkPosition = new(treeTrunkPosition.x + x, treeTrunkPosition.y, treeTrunkPosition.z + z);
                            //生成树干
                            WorldCreateHandler.Instance.manager.AddUpdateBlock(tempTrunkPosition, treeData.treeTrunk);
                        }

                        if ((x == -trunkRange || x == trunkRange || z == -trunkRange || z == trunkRange) ||
                            ((x == -trunkRange + 1 || x == trunkRange - 1) && (z == -trunkRange || z == trunkRange)) ||
                            ((z == -trunkRange + 1 || z == trunkRange - 1) && (x == -trunkRange || x == trunkRange)) ||
                            ((x == -trunkRange + 2 || x == trunkRange - 2) && (z == -trunkRange || z == trunkRange)) ||
                            ((z == -trunkRange + 2 || z == trunkRange - 2) && (x == -trunkRange || x == trunkRange)))
                        {
                            //树盘
                            if (y == 0 || y == 1)
                            {
                                Vector3Int baseStartPosition = treeTrunkPosition + new Vector3Int(x, -1, z);
                                int        baseWith          = WorldRandTools.Range(3, 6);
                                for (int b = 0; b < baseWith; b++)
                                {
                                    BlockDirectionEnum baseDirection =
                                        (x == -trunkRange ? BlockDirectionEnum.LeftForward
                                        : x == trunkRange ? BlockDirectionEnum.RightForward
                                        : z == -trunkRange ? BlockDirectionEnum.ForwardForward
                                        : z == trunkRange ? BlockDirectionEnum.BackForward
                                        : BlockDirectionEnum.LeftForward);

                                    int branchDirectionX = WorldRandTools.Range(0, 2);
                                    int branchDirectionY = WorldRandTools.Range(0, 2);
                                    int branchDirectionZ = WorldRandTools.Range(0, 2);

                                    int addPositionX;
                                    if (x == -trunkRange || x == trunkRange)
                                    {
                                        addPositionX = x > 0 ? 1 : -1;
                                    }
                                    else if (x == -trunkRange + 1 || x == trunkRange - 1 || x == -trunkRange + 2 || x == trunkRange - 2)
                                    {
                                        int tempRandom = WorldRandTools.Range(0, 2);
                                        if (tempRandom == 0)
                                        {
                                            addPositionX = x > 0 ? 1 : -1;
                                        }
                                        else
                                        {
                                            addPositionX = (branchDirectionX == 0 ? -1 : 1);
                                        }
                                    }
                                    else
                                    {
                                        addPositionX = (branchDirectionX == 0 ? -1 : 1);
                                    }

                                    int addPositionZ;
                                    if (z == -trunkRange || z == trunkRange)
                                    {
                                        addPositionZ = z > 0 ? 1 : -1;
                                    }
                                    else if (z == -trunkRange + 1 || z == trunkRange - 1 || z == -trunkRange + 2 || z == trunkRange - 2)
                                    {
                                        int tempRandom = WorldRandTools.Range(0, 2);
                                        if (tempRandom == 0)
                                        {
                                            addPositionZ = z > 0 ? 1 : -1;
                                        }
                                        else
                                        {
                                            addPositionZ = (branchDirectionZ == 0 ? -1 : 1);
                                        }
                                    }
                                    else
                                    {
                                        addPositionZ = (branchDirectionZ == 0 ? -1 : 1);
                                    }

                                    int addPositionY = (branchDirectionY == 0 ? -1 : 0);
                                    if (addPositionY == -1)
                                    {
                                        addPositionX  = 0;
                                        addPositionZ  = 0;
                                        baseDirection = BlockDirectionEnum.UpForward;
                                    }

                                    Vector3Int addPosition = new(addPositionX, addPositionY, addPositionZ);
                                    baseStartPosition += addPosition;
                                    //干
                                    WorldCreateHandler.Instance.manager.AddUpdateBlock(baseStartPosition, treeData.treeTrunk, baseDirection);
                                }
                            }

                            //枝
                            if (y > treeHeight / 2)
                            {
                                int addBranchRate = WorldRandTools.Range(0, 3);
                                if (addBranchRate == 0)
                                {
                                    int        branchWith          = WorldRandTools.Range(0, treeHeight / 2) + treeHeight / 4;
                                    Vector3Int branchStartPosition = treeTrunkPosition + new Vector3Int(x, 0, z);

                                    for (int b = 0; b < branchWith; b++)
                                    {
                                        BlockDirectionEnum branchDirection =
                                            (x == -trunkRange ? BlockDirectionEnum.LeftForward
                                            : x == trunkRange ? BlockDirectionEnum.RightForward
                                            : z == -trunkRange ? BlockDirectionEnum.ForwardForward
                                            : z == trunkRange ? BlockDirectionEnum.BackForward
                                            : BlockDirectionEnum.LeftForward);

                                        int branchDirectionX = WorldRandTools.Range(0, 2);
                                        int branchDirectionY = WorldRandTools.Range(0, 4);
                                        int branchDirectionZ = WorldRandTools.Range(0, 2);

                                        int addPositionX;
                                        if (x == -trunkRange || x == trunkRange)
                                        {
                                            addPositionX = x > 0 ? 1 : -1;
                                        }
                                        else if (x == -trunkRange + 1 || x == trunkRange - 1 || x == -trunkRange + 2 || x == trunkRange - 2)
                                        {
                                            int tempRandom = WorldRandTools.Range(0, 2);
                                            if (tempRandom == 0)
                                            {
                                                addPositionX = x > 0 ? 1 : -1;
                                            }
                                            else
                                            {
                                                addPositionX = (branchDirectionX == 0 ? -1 : 1);
                                            }
                                        }
                                        else
                                        {
                                            addPositionX = (branchDirectionX == 0 ? -1 : 1);
                                        }

                                        int addPositionZ;
                                        if (z == -trunkRange || z == trunkRange)
                                        {
                                            addPositionZ = z > 0 ? 1 : -1;
                                        }
                                        else if (z == -trunkRange + 1 || z == trunkRange - 1 || z == -trunkRange + 2 || z == trunkRange - 2)
                                        {
                                            int tempRandom = WorldRandTools.Range(0, 2);
                                            if (tempRandom == 0)
                                            {
                                                addPositionZ = z > 0 ? 1 : -1;
                                            }
                                            else
                                            {
                                                addPositionZ = (branchDirectionZ == 0 ? -1 : 1);
                                            }
                                        }
                                        else
                                        {
                                            addPositionZ = (branchDirectionZ == 0 ? -1 : 1);
                                        }

                                        int addPositionY = (branchDirectionY == 0 ? 1 : 0);

                                        Vector3Int addPosition = new(addPositionX, addPositionY, addPositionZ);
                                        branchStartPosition += addPosition;
                                        //干
                                        WorldCreateHandler.Instance.manager.AddUpdateBlock(branchStartPosition, treeData.treeTrunk, branchDirection);
                                        //叶
                                        if (b % 4 == 0)
                                        {
                                            int leavesRange = 2;
                                            for (int leavesX = -leavesRange; leavesX <= leavesRange; leavesX++)
                                            {
                                                for (int leavesY = -leavesRange; leavesY <= leavesRange; leavesY++)
                                                {
                                                    for (int leavesZ = -leavesRange; leavesZ <= leavesRange; leavesZ++)
                                                    {
                                                        if (((leavesX == leavesRange || leavesX == -leavesRange) && (leavesY == leavesRange || leavesY == -leavesRange)) ||
                                                            ((leavesY == leavesRange || leavesY == -leavesRange) && (leavesZ == leavesRange || leavesZ == -leavesRange)) ||
                                                            ((leavesX == leavesRange || leavesX == -leavesRange) && (leavesZ == leavesRange || leavesZ == -leavesRange)))
                                                        {
                                                            continue;
                                                        }
                                                        Vector3Int leavesPosition = branchStartPosition + new Vector3Int(leavesX, leavesY, leavesZ);
                                                        WorldCreateHandler.Instance.manager.AddUpdateBlock(leavesPosition, treeData.treeLeaves);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }