Пример #1
0
        public void ChangeBlock(Vector3 position, Block block)
        {
            int id = block == null ? 0 : block.Id;
            //If we are breaking a block, trigger it's on break event
            if(block == null)
            {
                Block previousBlock = GetBlockAt(position);
                previousBlock.Destroyed(position);
            }

            //Set the block in the data source
            Debug.Log("Placing: " + position.ToString());
            dataSource.Set(position.x, position.y, position.z, new VoxelData() { Material = (byte) id });

            //If the block isn't null, notify that it's been placed
            if(block != null)
            {
                block.Placed(position);
            }

            //Notify the chunk that it's data source has changed and it should probably remesh
            Chunk c = LoadedChunks.Values.SingleOrDefault(x => x.bounds.Contains(position));

            if (c != null)
            {
                //rebuild neighor chunks if we are on the border
                int xx = (int)position.x / 16;
                int yy = (int)position.y / 16;
                int zz = (int)position.z / 16;

                IEnumerable<Chunk> neighbors = LoadedChunks.Values.Where(x =>
                    {
                        int cx = (int)x.bounds.min.x / 16;
                        int cy = (int)x.bounds.min.y / 16;
                        int cz = (int)x.bounds.min.z / 16;

                        return Vector3.Distance(new Vector3(xx, yy, zz), new Vector3(cx, cy, cz)) <= 1;
                    });
                foreach(Chunk cnk in neighbors)
                {
                    cnk.BuildChunk();
                }

                c.BuildChunk();
            }
            else
            {
                Debug.Log("Chunk Null!");
            }
        }