Ejemplo n.º 1
0
    public static void SetBlockData(int x, int y, int z, byte type, byte data)
    {
        int chunkX = Mathf.FloorToInt(x / 16f);
        int chunkZ = Mathf.FloorToInt(z / 16f);

        int xInChunk = x - chunkX * 16;
        int zInChunk = z - chunkZ * 16;

        NBTChunk    chunk        = GetChunk(chunkX, chunkZ);
        NBTBlock    oldGenerator = NBTGeneratorManager.GetMeshGenerator(chunk.GetBlockByte(xInChunk, y, zInChunk));
        UpdateFlags updateFlag   = GetUpdateFlags(oldGenerator);

        if (type == 0)
        {
            chunk.GetBlockData(xInChunk, y + 1, zInChunk, out byte topType, out byte topData);
            NBTBlock topGenerator = NBTGeneratorManager.GetMeshGenerator(topType);
            if (topGenerator != null && topGenerator.isTransparent)
            {
                BreakBlockEffect.Create(topType, topData, new Vector3(x, y + 1, z));
                chunk.SetBlockByte(xInChunk, y + 1, zInChunk, 0);
                updateFlag |= UpdateFlags.NotCollidable;
            }
        }
        else
        {
            NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(type);
            updateFlag |= GetUpdateFlags(generator);
        }

        chunk.SetBlockData(xInChunk, y, zInChunk, type, data);
        if (updateFlag.HasFlag(UpdateFlags.Lighting))
        {
            UpdateLighting(x, y, z);
        }
        chunk.RebuildMesh(updateFlag);

        if (type == 0 && !oldGenerator.isTransparent)
        {
            // update border chunks if neccesary
            NBTChunk leftChunk = GetChunk(chunkX - 1, chunkZ);
            if (xInChunk == 0)
            {
                leftChunk.RebuildMesh();
            }
            else
            {
                leftChunk.RebuildMeshAsync();
            }

            NBTChunk rightChunk = GetChunk(chunkX + 1, chunkZ);
            if (xInChunk == 15)
            {
                rightChunk.RebuildMesh();
            }
            else
            {
                rightChunk.RebuildMeshAsync();
            }

            NBTChunk backChunk = GetChunk(chunkX, chunkZ - 1);
            if (zInChunk == 0)
            {
                backChunk.RebuildMesh();
            }
            else
            {
                backChunk.RebuildMeshAsync();
            }

            NBTChunk frontChunk = GetChunk(chunkX, chunkZ + 1);
            if (zInChunk == 15)
            {
                frontChunk.RebuildMesh();
            }
            else
            {
                frontChunk.RebuildMeshAsync();
            }
        }
    }