Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace Uniblocks {
  5.  
  6. public class FallingBlockEvents : DefaultVoxelEvents {
  7.  
  8.  
  9.     public override void OnBlockPlace ( VoxelInfo voxelInfo ) {
  10.        
  11.         // if the block below is empty, make this block fall
  12.         Index indexBelow = new Index (voxelInfo.index.x, voxelInfo.index.y-1, voxelInfo.index.z); // get index below
  13.        
  14.         if ( voxelInfo.chunk.GetVoxel(indexBelow) == 0 ) { // check if the block below is empty
  15.            
  16.             FallingBlockEvents fallCoroutine = Engine.EngineInstance.gameObject.AddComponent <FallingBlockEvents>(); // create a new object to run the coroutine
  17.             fallCoroutine.DropBlock (voxelInfo); // run the coroutine on the new object
  18.            
  19.         }
  20.        
  21.     }
  22.    
  23.     public override void OnBlockDestroy ( VoxelInfo voxelInfo ) {
  24.        
  25.         // if the block above is a falling block, make it fall (because the currrent block below it is now empty)
  26.         Index indexAbove = new Index (voxelInfo.index.x, voxelInfo.index.y+1, voxelInfo.index.z); // get index above
  27.         ushort voxelAbove = voxelInfo.chunk.GetVoxel(indexAbove); // get voxel above
  28.        
  29.         if (Engine.GetVoxelGameObject(voxelAbove).GetComponent<FallingBlockEvents>() != null) { // if the block above has a FallingBlockEvents component
  30.            
  31.             VoxelInfo blockAbove = new VoxelInfo (indexAbove, voxelInfo.chunk); // create a VoxelInfo pointing to the block above
  32.             FallingBlockEvents fallCoroutine = Engine.EngineInstance.gameObject.AddComponent <FallingBlockEvents>(); // create a new object to run the coroutine
  33.             fallCoroutine.DropBlock (blockAbove); // run the coroutine on the new object
  34.         }
  35.        
  36.     }
  37.    
  38.    
  39.     public void DropBlock (VoxelInfo voxelInfo) {
  40.         StartCoroutine (DoDropBlock (voxelInfo));
  41.     }
  42.    
  43.     private IEnumerator DoDropBlock ( VoxelInfo voxelInfo ) {
  44.        
  45.         Index indexBelow = new Index (voxelInfo.index.x, voxelInfo.index.y-1, voxelInfo.index.z); // get index below
  46.         VoxelInfo blockBelow = new VoxelInfo (indexBelow, voxelInfo.chunk); // create a VoxelInfo pointing to the block below
  47.    
  48.         yield return new WaitForSeconds (0.1f); // wait before falling
  49.        
  50.         Voxel.PlaceBlock (blockBelow, voxelInfo.GetVoxel()); // place the block below using PlaceBlock. This will trigger OnBlockPlace function again at the new position
  51.         Voxel.DestroyBlock (voxelInfo); // destroy the block above. This will trigger OnBlockDestroy on the falling block to check if there is another falling block above.
  52.        
  53.         Destroy (this); // we only needed the object to run the coroutine, so we can get rid of it now
  54.        
  55.     }
  56.    
  57.  
  58. }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement