Exemplo n.º 1
0
    /// <summary>
    /// 设置方块
    /// </summary>
    public void SetBlockForLocal(int x, int y, int z, Block block, byte direction)
    {
        int yIndex = y / chunkWidth;
        ChunkSectionData chunkSection = chunkSectionDatas[yIndex];

        chunkSection.SetBlock(x, y % chunkWidth, z, block, direction);
    }
Exemplo n.º 2
0
    public BlockDirectionEnum GetBlockDirection(int x, int y, int z)
    {
        int yIndex = y / chunkWidth;
        ChunkSectionData chunkSection = chunkSectionDatas[yIndex];

        return((BlockDirectionEnum)chunkSection.GetBlockDirection(x, y % chunkWidth, z));
    }
Exemplo n.º 3
0
    public Block GetBlockForLocal(int x, int y, int z)
    {
        int yIndex = y / chunkWidth;
        ChunkSectionData chunkSection = chunkSectionDatas[yIndex];
        int blockType = chunkSection.GetBlock(x, y % chunkWidth, z);

        return(BlockHandler.Instance.manager.GetRegisterBlock(blockType));
    }
Exemplo n.º 4
0
    /// <summary>
    /// 获取方块
    /// </summary>
    public void GetBlockForLocal(int x, int y, int z, out Block block, out BlockDirectionEnum direction)
    {
        int yIndex = y / chunkWidth;
        ChunkSectionData chunkSection = chunkSectionDatas[yIndex];

        chunkSection.GetBlock(x, y % chunkWidth, z, out int blockType, out byte blockDirection);
        block     = BlockHandler.Instance.manager.GetRegisterBlock(blockType);
        direction = (BlockDirectionEnum)blockDirection;
    }
Exemplo n.º 5
0
    public ChunkData(Vector3Int wPosition, int chunkWidth, int chunkHeight)
    {
        this.chunkWidth       = chunkWidth;
        this.chunkHeight      = chunkHeight;
        this.positionForWorld = wPosition;

        int sectionSize = chunkWidth;

        chunkSectionDatas = new ChunkSectionData[chunkHeight / chunkWidth];

        for (int i = 0; i < chunkSectionDatas.Length; i++)
        {
            chunkSectionDatas[i] = new ChunkSectionData(sectionSize, sectionSize * i);
        }
    }
Exemplo n.º 6
0
    /// <summary>
    /// 异步构建chunk
    /// </summary>
    public async void BuildChunkForAsync(Action callBackForComplete)
    {
        //只有初始化之后的chunk才能刷新
        if (!isInit)
        {
            isBuildChunk = false;
            callBackForComplete?.Invoke();
            return;
        }
        if (isBuildChunk)
        {
            return;
        }
        isBuildChunk = true;
        await Task.Run(() =>
        {
            //遍历chunk, 生成其中的每一个Block
            try
            {
                lock (lockForBlcok)
                {
#if UNITY_EDITOR
                    Stopwatch stopwatch = TimeUtil.GetMethodTimeStart();
#endif
                    chunkMeshData = new ChunkMeshData();

                    chunkData.InitRoundChunk();

                    //遍历每一个子区块
                    for (int i = 0; i < chunkData.chunkSectionDatas.Length; i++)
                    {
                        ChunkSectionData chunkSection = chunkData.chunkSectionDatas[i];
                        if (!chunkSection.IsRender())
                        {
                            continue;
                        }

                        for (int x = 0; x < chunkSection.sectionSize; x++)
                        {
                            for (int y = 0; y < chunkSection.sectionSize; y++)
                            {
                                for (int z = 0; z < chunkSection.sectionSize; z++)
                                {
                                    Block block = chunkData.GetBlockForLocal(x, y + chunkSection.yBase, z);;
                                    if (block == null || block.blockType == BlockTypeEnum.None)
                                    {
                                        continue;
                                    }
                                    Vector3Int localPosition = new Vector3Int(x, y + chunkSection.yBase, z);
                                    block.BuildBlock(this, localPosition);
                                    block.InitBlock(this, localPosition, 0);
                                }
                            }
                        }
                    }
#if UNITY_EDITOR
                    TimeUtil.GetMethodTimeEnd("Time_BuildChunkForAsync:", stopwatch);
#endif
                }
            }
            catch (Exception e)
            {
                LogUtil.LogError("BuildChunkForAsync:" + e.ToString());
            }
            finally
            {
            }
        });

        isBuildChunk = false;
        callBackForComplete?.Invoke();
    }