示例#1
0
    static void UpdateWorldData(World world)
    {
        Matrix <BlockData>     mat  = new Matrix <BlockData>(Chunk.chunkSize + 2, Chunk.chunkSize + 2, Chunk.chunkSize + 2);
        MatrixView <BlockData> view = new MatrixView <BlockData>(mat, 0, 0, 0);

        for (int i = 0; i < world.chunkNb; i++)
        {
            for (int j = 0; j < world.chunkNb; j++)
            {
                var chunk  = world.GetChunk(i, j);
                var layers = chunk.GetLayers();

                foreach (var l in layers)
                {
                    world.GetLocalMatrix(i * Chunk.chunkSize - 1, l * Chunk.chunkSize - 1, j * Chunk.chunkSize - 1, mat);

                    for (int x = 0; x < Chunk.chunkSize; x++)
                    {
                        for (int y = 0; y < Chunk.chunkSize; y++)
                        {
                            for (int z = 0; z < Chunk.chunkSize; z++)
                            {
                                view.SetPos(x + 1, y + 1, z + 1);
                                view.Set(0, 0, 0, BlockTypeList.instance.Get(view.GetCenter().id).UpdateBlock(view));
                            }
                        }
                    }

                    for (int y = 0; y < Chunk.chunkSize; y++)
                    {
                        int height = chunk.LayerToHeight(l, y);
                        for (int x = 0; x < Chunk.chunkSize; x++)
                        {
                            for (int z = 0; z < Chunk.chunkSize; z++)
                            {
                                chunk.SetBlock(x, height, z, mat.Get(x + 1, y + 1, z + 1));
                            }
                        }
                    }
                }
            }
        }
    }
示例#2
0
    public override void Render(Chunk c, int x, int y, int z, MeshParams <WorldVertexDefinition> meshParams)
    {
        var layer = c.GetLayer(y);

        if (layer == null)
        {
            return;
        }

        var world = c.world;

        int minX, minY, minZ;

        world.BlockPosInChunkToPos(Chunk.chunkSize - 1, Chunk.chunkSize - 1, c.x - 1, c.z - 1, out minX, out minZ);
        minY = c.LayerToHeight(y - 1, Chunk.chunkSize - 1);

        world.GetLocalMatrix(minX, minY, minZ, m_matrix);

        m_view.mat = m_matrix;

        for (int i = 0; i < Chunk.chunkSize; i++)
        {
            for (int j = 0; j < Chunk.chunkSize; j++)
            {
                for (int k = 0; k < Chunk.chunkSize; k++)
                {
                    m_view.SetPos(i + 1, j + 1, k + 1);
                    int centerID = m_matrix.Get(i + 1, j + 1, k + 1).id;
                    if (centerID == 0)
                    {
                        continue;
                    }

                    Vector3 pos = new Vector3(i, j, k);

                    BlockTypeList.instance.Get(centerID).Render(pos, m_view, meshParams);
                }
            }
        }
    }
示例#3
0
    public void SetBlock(int x, int y, int z, BlockData block, bool updateData = true)
    {
        if (updateData)
        {
            //create a local 5*5*5 matrix
            var matrix = GetLocalMatrix(x - 2, y - 2, z - 2, 5, 5, 5);
            var view   = new MatrixView <BlockData>(matrix, 2, 2, 2);
            view.Set(0, 0, 0, block);

            Vector3Int[] updatePositions = new Vector3Int[]
            {
                //center
                new Vector3Int(2, 2, 2),
                //cross
                new Vector3Int(2, 1, 2),
                new Vector3Int(1, 2, 2),
                new Vector3Int(2, 2, 1),
                new Vector3Int(3, 2, 2),
                new Vector3Int(2, 2, 3),
                new Vector3Int(2, 3, 2),
                //borders
                new Vector3Int(1, 1, 2), //down
                new Vector3Int(2, 1, 1),
                new Vector3Int(3, 1, 2),
                new Vector3Int(2, 1, 3),
                new Vector3Int(1, 2, 1), //midle
                new Vector3Int(3, 2, 1),
                new Vector3Int(1, 2, 3),
                new Vector3Int(3, 2, 3),
                new Vector3Int(1, 3, 2), //up
                new Vector3Int(2, 3, 1),
                new Vector3Int(3, 3, 2),
                new Vector3Int(2, 3, 3),
                //corners
                new Vector3Int(1, 1, 1), //down
                new Vector3Int(1, 1, 3),
                new Vector3Int(3, 1, 1),
                new Vector3Int(3, 1, 3),
                new Vector3Int(1, 3, 1), //up
                new Vector3Int(1, 3, 3),
                new Vector3Int(3, 3, 1),
                new Vector3Int(3, 3, 3),
            };

            foreach (var pos in updatePositions)
            {
                view.SetPos(pos.x, pos.y, pos.z);
                view.Set(0, 0, 0, BlockTypeList.instance.Get(view.GetCenter().id).UpdateBlock(view));
            }

            //only update the 3*3* center of the matrix
            SetBlocks(x - 1, y - 1, z - 1, matrix, 1, 1, 1, 3, 3, 3, false);
        }
        else
        {
            int chunkX;
            int chunkZ;
            int blockX;
            int blockZ;
            PosToBlockPosAndChunkPos(x, z, out blockX, out blockZ, out chunkX, out chunkZ);

            var chunk = GetChunk(chunkX, chunkZ);
            Debug.Assert(chunk != null);

            lock (dataLock)
            {
                chunk.SetBlock(blockX, y, blockZ, block);
            }
        }
    }