Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class BlockController : MonoBehaviour
- {
- public LayerMask lm;
- public GameObject bhPrefab;
- GameObject bh;
- void Awake ()
- {
- bh = GameObject.Instantiate(bhPrefab, Vector3.zero, Quaternion.identity) as GameObject;
- }
- void Update()
- {
- RaycastHit hit;
- if(Physics.Raycast(transform.position, transform.forward, out hit, 7f, lm))
- {
- if(!bh.activeInHierarchy)
- bh.SetActive(true);
- Vector3 worldBlockP = hit.point - (hit.normal / 2);
- Vector3 worldBlockAddP = hit.point + (hit.normal / 2);
- Vector3 blockPos = new Vector3(Mathf.FloorToInt(worldBlockP.x), Mathf.FloorToInt(worldBlockP.y), Mathf.FloorToInt(worldBlockP.z));
- Vector3 blockAddPos = new Vector3(Mathf.FloorToInt(worldBlockAddP.x), Mathf.FloorToInt(worldBlockAddP.y), Mathf.FloorToInt(worldBlockAddP.z));
- bh.transform.position = blockPos;
- if(Input.GetMouseButtonDown(1))
- {
- Chunk c = Chunk.SetWorldBlock(blockAddPos, 2);
- if(c != null)
- {
- c.dirty = true;
- }
- }
- if(Input.GetMouseButtonDown(0))
- {
- Chunk c = Chunk.SetWorldBlock(blockPos, 0);
- byte b = Chunk.GetWorldBlock(blockPos);
- if(b > 0)
- {
- Block selectedBlock = Block.blocks[b -1];
- if(selectedBlock.name == "bedrock")
- {
- return;
- }
- }
- if(c != null)
- {
- c.dirty = true;
- Vector3 localPos = blockPos - c.chunkPos;
- //OTHER
- if(localPos.x == 0)
- {
- Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(-Chunk.width, 0, 0));
- if(nC != null)
- {
- nC.dirty = true;
- }
- }
- if(localPos.x == Chunk.width - 1)
- {
- Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(Chunk.width, 0, 0));
- if(nC != null)
- {
- nC.dirty = true;
- }
- }
- if(localPos.z == 0)
- {
- Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, 0, -Chunk.width));
- if(nC != null)
- {
- nC.dirty = true;
- }
- }
- if(localPos.z == Chunk.width - 1)
- {
- Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, 0, Chunk.width));
- if(nC != null)
- {
- nC.dirty = true;
- }
- }
- if(localPos.y == 0)
- {
- Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, -Chunk.height, 0));
- if(nC != null)
- {
- nC.dirty = true;
- }
- }
- if(localPos.y == Chunk.height - 1)
- {
- Chunk nC = Chunk.GetChunk(c.chunkPos + new Vector3(0, Chunk.height, 0));
- if(nC != null)
- {
- nC.dirty = true;
- }
- }
- }
- }
- }
- else
- {
- bh.SetActive(false);
- }
- }
- }
Add Comment
Please, Sign In to add comment