Guest User

Untitled

a guest
Oct 27th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BlockController : MonoBehaviour
  6. {
  7.     public LayerMask lm;
  8.     public GameObject bhPrefab;
  9.  
  10.     GameObject bh;
  11.  
  12.     void Awake ()
  13.     {
  14.         bh = GameObject.Instantiate(bhPrefab, Vector3.zero, Quaternion.identity) as GameObject;
  15.     }
  16.  
  17.     void Update()
  18.     {
  19.         RaycastHit hit;
  20.         if(Physics.Raycast(transform.position, transform.forward, out hit, 7f, lm))
  21.         {
  22.             if(!bh.activeInHierarchy)
  23.                 bh.SetActive(true);
  24.  
  25.             Vector3 worldBlockP = hit.point - (hit.normal / 2);
  26.             Vector3 worldBlockAddP = hit.point + (hit.normal / 2);
  27.  
  28.             Vector3 blockPos = new Vector3(Mathf.FloorToInt(worldBlockP.x), Mathf.FloorToInt(worldBlockP.y), Mathf.FloorToInt(worldBlockP.z));
  29.             Vector3 blockAddPos = new Vector3(Mathf.FloorToInt(worldBlockAddP.x), Mathf.FloorToInt(worldBlockAddP.y), Mathf.FloorToInt(worldBlockAddP.z));
  30.  
  31.             bh.transform.position = blockPos;
  32.  
  33.             if(Input.GetMouseButtonDown(1))
  34.             {
  35.                 Chunk c = Chunk.SetWorldBlock(blockAddPos, 2);
  36.  
  37.                 if(c != null)
  38.                 {
  39.                     c.dirty = true;
  40.                 }
  41.             }
  42.  
  43.             if(Input.GetMouseButtonDown(0))
  44.             {
  45.                 Chunk c = Chunk.SetWorldBlock(blockPos, 0);
  46.  
  47.                 byte b = Chunk.GetWorldBlock(blockPos);
  48.  
  49.                 if(b > 0)
  50.                 {
  51.                     Block selectedBlock = Block.blocks[b -1];
  52.  
  53.                     if(selectedBlock.name == "bedrock")
  54.                     {
  55.                         return;
  56.                     }
  57.                 }
  58.  
  59.                 if(c != null)
  60.                 {
  61.                     c.dirty = true;
  62.  
  63.                     Vector3 localPos = blockPos - c.chunkPos;
  64.  
  65.                     //OTHER
  66.                     if(localPos.x == 0)
  67.                     {
  68.                         Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(-Chunk.width, 0, 0));
  69.                         if(nC != null)
  70.                         {
  71.                             nC.dirty = true;
  72.                         }
  73.                     }
  74.  
  75.                     if(localPos.x == Chunk.width - 1)
  76.                     {
  77.                         Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(Chunk.width, 0, 0));
  78.                         if(nC != null)
  79.                         {
  80.                             nC.dirty = true;
  81.                         }
  82.                     }
  83.  
  84.                     if(localPos.z == 0)
  85.                     {
  86.                         Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, 0, -Chunk.width));
  87.                         if(nC != null)
  88.                         {
  89.                             nC.dirty = true;
  90.                         }
  91.                     }
  92.  
  93.                     if(localPos.z == Chunk.width - 1)
  94.                     {
  95.                         Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, 0, Chunk.width));
  96.                         if(nC != null)
  97.                         {
  98.                             nC.dirty = true;
  99.                         }
  100.                     }
  101.  
  102.                     if(localPos.y == 0)
  103.                     {
  104.                         Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, -Chunk.height, 0));
  105.                         if(nC != null)
  106.                         {
  107.                             nC.dirty = true;
  108.                         }
  109.                     }
  110.  
  111.                     if(localPos.y == Chunk.height - 1)
  112.                     {
  113.                         Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, Chunk.height, 0));
  114.                         if(nC != null)
  115.                         {
  116.                             nC.dirty = true;
  117.                         }
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.         else
  123.         {
  124.             bh.SetActive(false);
  125.         }
  126.     }  
  127. }
Add Comment
Please, Sign In to add comment