Пример #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!");
            }
        }
Пример #2
0
        private void RegisterBlock(Block b, BlockDataAttribute attributes)
        {
            b.Id = BlockRegistrationId;
            BlockRegistrationId++;

            b.Data = attributes;

            Texture2D[] loadedTextures = new Texture2D[6];
            for(int i = 0; i < 6; i++)
            {
                Texture2D blockTexture = (Texture2D)Resources.Load(b.Data.TextureNames[i]);
                if (blockTexture == null)
                {
                    Debug.LogWarning("Block: " + b.Data.DisplayName + " references a texture that doesn't exist.  Falling back");
                    blockTexture = (Texture2D)Resources.Load("notexture");
                }

                loadedTextures[i] = blockTexture;
            }

            string name = attributes.DisplayName;
            if (RegisteredBlocks.ContainsKey(name))
            {
                throw new InvalidOperationException("Cannot have two blocks with the same name!");
            }

            RegisteredBlocks[name] = new RegisteredBlock()
            {
                Block = b,
            };
        }
Пример #3
0
 public static void LoadBlockType(Block block)
 {
     BlockDataAttribute attrib = block.GetType().GetCustomAttributes(typeof(BlockDataAttribute), true).Cast<BlockDataAttribute>().FirstOrDefault();
     if (attrib == null)
     {
         // Skip blocks with invalid attributes
         Debug.LogError(block.GetType().Name + " must have a data attribute assigned to load properly.");
         return;
     }
     try
     {
         Game.BlockRegistry.RegisterBlock(block, attrib);
     }
     catch (InvalidOperationException e)
     {
         Debug.LogError(block.GetType().Name + " already loaded, so skipping . . .");
         return;
     }
 }
Пример #4
0
 public void ChangeBlock(Vector3 position, Block block)
 {
     World parent = transform.parent.gameObject.GetComponent<World>();
     parent.ChangeBlock(position, block);
 }