Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- namespace Uniblocks {
- public class FallingBlockEvents : DefaultVoxelEvents {
- public override void OnBlockPlace ( VoxelInfo voxelInfo ) {
- // if the block below is empty, make this block fall
- Index indexBelow = new Index (voxelInfo.index.x, voxelInfo.index.y-1, voxelInfo.index.z); // get index below
- if ( voxelInfo.chunk.GetVoxel(indexBelow) == 0 ) { // check if the block below is empty
- FallingBlockEvents fallCoroutine = Engine.EngineInstance.gameObject.AddComponent <FallingBlockEvents>(); // create a new object to run the coroutine
- fallCoroutine.DropBlock (voxelInfo); // run the coroutine on the new object
- }
- }
- public override void OnBlockDestroy ( VoxelInfo voxelInfo ) {
- // if the block above is a falling block, make it fall (because the currrent block below it is now empty)
- Index indexAbove = new Index (voxelInfo.index.x, voxelInfo.index.y+1, voxelInfo.index.z); // get index above
- ushort voxelAbove = voxelInfo.chunk.GetVoxel(indexAbove); // get voxel above
- if (Engine.GetVoxelGameObject(voxelAbove).GetComponent<FallingBlockEvents>() != null) { // if the block above has a FallingBlockEvents component
- VoxelInfo blockAbove = new VoxelInfo (indexAbove, voxelInfo.chunk); // create a VoxelInfo pointing to the block above
- FallingBlockEvents fallCoroutine = Engine.EngineInstance.gameObject.AddComponent <FallingBlockEvents>(); // create a new object to run the coroutine
- fallCoroutine.DropBlock (blockAbove); // run the coroutine on the new object
- }
- }
- public void DropBlock (VoxelInfo voxelInfo) {
- StartCoroutine (DoDropBlock (voxelInfo));
- }
- private IEnumerator DoDropBlock ( VoxelInfo voxelInfo ) {
- Index indexBelow = new Index (voxelInfo.index.x, voxelInfo.index.y-1, voxelInfo.index.z); // get index below
- VoxelInfo blockBelow = new VoxelInfo (indexBelow, voxelInfo.chunk); // create a VoxelInfo pointing to the block below
- yield return new WaitForSeconds (0.1f); // wait before falling
- Voxel.PlaceBlock (blockBelow, voxelInfo.GetVoxel()); // place the block below using PlaceBlock. This will trigger OnBlockPlace function again at the new position
- Voxel.DestroyBlock (voxelInfo); // destroy the block above. This will trigger OnBlockDestroy on the falling block to check if there is another falling block above.
- Destroy (this); // we only needed the object to run the coroutine, so we can get rid of it now
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement