public void AddBlock(RaycastHit hitInfo, Vector3 direction, bool trueLocation, int blockType) { Vector3 hitInLocalSpace = transform.InverseTransformPoint(hitInfo.point - direction.normalized * .001f); int xCoord = (int)hitInLocalSpace.x; int yCoord = (int)hitInLocalSpace.y; int zCoord = (int)hitInLocalSpace.z; if (hitInLocalSpace.x % 1 > .5f) { xCoord++; } if (hitInLocalSpace.y % 1 > .5f) { yCoord++; } if (hitInLocalSpace.z % 1 > .5f) { zCoord++; } // Debug.Log("x: " + xCoord + ", Y: " + yCoord + "Z: " + zCoord); //check if added in next chunk. if (xCoord > 15 && !trueLocation) { rightChunk.AddBlock(hitInfo, direction, true, blockType); return; } if (hitInLocalSpace.x < 0 && !trueLocation) { leftChunk.AddBlock(hitInfo, direction, true, blockType); return; } if (zCoord > 15 && !trueLocation) { forwardChunk.AddBlock(hitInfo, direction, true, blockType); return; } if (hitInLocalSpace.z < 0 && !trueLocation) { backwardChunk.AddBlock(hitInfo, direction, true, blockType); return; } if (yCoord > 90) { return; } if (zCoord > 15) { zCoord = 15; } if (zCoord < 0) { zCoord = 0; } if (xCoord > 15) { xCoord = 15; } if (xCoord < 0) { xCoord = 0; } blockDataArr[xCoord, yCoord, zCoord] = new BlockDataInternal(); blockDataArr[xCoord, yCoord, zCoord].blockType = blockType; blockExistsArr[xCoord, yCoord, zCoord] = true; //Check to see if any block will be hidden: BlockDataInternal addedBlock = blockDataArr[xCoord, yCoord, zCoord]; addedBlock.visible = true; if (blockExistsArr[xCoord, yCoord - 1, zCoord]) //up { addedBlock.DownExists = true; blockDataArr[xCoord, yCoord - 1, zCoord].UpExists = true; } if (yCoord < 63 && blockExistsArr[xCoord, yCoord + 1, zCoord]) // down { addedBlock.UpExists = true; blockDataArr[xCoord, yCoord + 1, zCoord].DownExists = true; } if (xCoord > 0) { if (blockExistsArr[xCoord - 1, yCoord, zCoord]) // left { addedBlock.LeftExists = true; blockDataArr[xCoord - 1, yCoord, zCoord].RightExists = true; } } else { if (leftChunk.blockExistsArr[15, yCoord, zCoord]) { addedBlock.LeftExists = true; leftChunk.blockDataArr[15, yCoord, zCoord].RightExists = true; leftChunk.HideInvisible(); } } if (xCoord < 15) //right { if (blockExistsArr[xCoord + 1, yCoord, zCoord]) { addedBlock.RightExists = true; blockDataArr[xCoord + 1, yCoord, zCoord].LeftExists = true; } } else { if (rightChunk.blockExistsArr[0, yCoord, zCoord]) { addedBlock.RightExists = true; rightChunk.blockDataArr[0, yCoord, zCoord].LeftExists = true; rightChunk.HideInvisible(); } } if (zCoord > 0) { if (blockExistsArr[xCoord, yCoord, zCoord - 1]) // backward { addedBlock.BackExists = true; blockDataArr[xCoord, yCoord, zCoord - 1].ForwardExists = true; } } else { if (backwardChunk.blockExistsArr[xCoord, yCoord, 15]) { addedBlock.BackExists = true; backwardChunk.blockDataArr[xCoord, yCoord, 15].ForwardExists = true; backwardChunk.HideInvisible(); } } if (zCoord < 15) { if (blockExistsArr[xCoord, yCoord, zCoord + 1]) // forward { addedBlock.ForwardExists = true; blockDataArr[xCoord, yCoord, zCoord + 1].BackExists = true; } } else { if (forwardChunk.blockExistsArr[xCoord, yCoord, 0]) { addedBlock.ForwardExists = true; forwardChunk.blockDataArr[xCoord, yCoord, 0].BackExists = true; forwardChunk.HideInvisible(); } } HideInvisible(); }
public void DestroyBlock(RaycastHit hitInfo, Vector3 direction) { Vector3 hitInLocalSpace = transform.InverseTransformPoint(hitInfo.point + direction.normalized * .001f); int xCoord = (int)hitInLocalSpace.x; if (hitInLocalSpace.x % 1 > .5f) { xCoord++; } int yCoord = (int)hitInLocalSpace.y; if (hitInLocalSpace.y % 1 > .5f) { yCoord++; } int zCoord = (int)hitInLocalSpace.z; if (hitInLocalSpace.z % 1 > .5f) { zCoord++; } if (!blockExistsArr[xCoord, yCoord, zCoord]) { Debug.Log("Garbage hit"); return; } if (yCoord == 0) { return; } blockExistsArr[xCoord, yCoord, zCoord] = false; //Check to see if any block will be revealed: BlockDataInternal deletedBlock = blockDataArr[xCoord, yCoord, zCoord]; blockDataArr[xCoord, yCoord, zCoord] = null; List <MeshFilter> addedMeshesList = new List <MeshFilter>(); if (deletedBlock.DownExists) { blockDataArr[xCoord, yCoord - 1, zCoord].UpExists = false; blockDataArr[xCoord, yCoord - 1, zCoord].visible = true; } if (deletedBlock.UpExists && yCoord > 0) { blockDataArr[xCoord, yCoord + 1, zCoord].DownExists = false; blockDataArr[xCoord, yCoord + 1, zCoord].visible = true; } if (deletedBlock.LeftExists) { //within this chunk if (xCoord > 0) { blockDataArr[xCoord - 1, yCoord, zCoord].RightExists = false; blockDataArr[xCoord - 1, yCoord, zCoord].visible = true; } //outside this else { leftChunk.blockDataArr[15, yCoord, zCoord].RightExists = false; leftChunk.blockDataArr[15, yCoord, zCoord].visible = true; leftChunk.HideInvisible(); } } if (deletedBlock.RightExists) { if (xCoord < 15) { blockDataArr[xCoord + 1, yCoord, zCoord].LeftExists = false; blockDataArr[xCoord + 1, yCoord, zCoord].visible = true; } else { rightChunk.blockDataArr[0, yCoord, zCoord].LeftExists = false; rightChunk.blockDataArr[0, yCoord, zCoord].visible = true; rightChunk.HideInvisible(); } } if (deletedBlock.ForwardExists) { if (zCoord < 15) { blockDataArr[xCoord, yCoord, zCoord + 1].BackExists = false; blockDataArr[xCoord, yCoord, zCoord + 1].visible = true; } else { forwardChunk.blockDataArr[xCoord, yCoord, 0].BackExists = false; forwardChunk.blockDataArr[xCoord, yCoord, 0].visible = true; forwardChunk.HideInvisible(); } } if (deletedBlock.BackExists) { if (zCoord > 0) { blockDataArr[xCoord, yCoord, zCoord - 1].ForwardExists = false; blockDataArr[xCoord, yCoord, zCoord - 1].visible = true; } else { backwardChunk.blockDataArr[xCoord, yCoord, 15].ForwardExists = false; backwardChunk.blockDataArr[xCoord, yCoord, 15].visible = true; backwardChunk.HideInvisible(); } } //Update mesh with what we've learned. HideInvisible(); }
public void GenerateBlocks() { Vector3 beginning = transform.position; for (int x = 0; x < 16; x++) { for (int y = 0; y < 64; y++) { for (int z = 0; z < 16; z++) { if (y < heightMap[x, z] || y == 0 || blockExistsArr[x, y, z]) { if (trees[x, z] == 1 && y + 1 >= heightMap[x, z] && blockExistsArr[x, y, z] == false) { MakeTree(x, y, z); } if (!blockExistsArr[x, y, z]) { blockDataArr[x, y, z] = new BlockDataInternal(); blockExistsArr[x, y, z] = true; blockDataArr[x, y, z].blockType = 0; //natural if (y > 5) { blockDataArr[x, y, z].blockType = 2; } } //Now set all blocks neighbor booleans. //DownCheck: if (y > 0) //prevents oob { if (blockExistsArr[x, y - 1, z]) { //set for current blockDataArr[x, y, z].DownExists = true; //set for below blockDataArr[x, y - 1, z].UpExists = true; if (y > 1 && blockExistsArr[x, y - 2, z] && blockDataArr[x, y, z].blockType == 0) { blockDataArr[x, y - 2, z].blockType = 1; //dirt } } } else { blockDataArr[x, y, z].DownExists = true; } //left check if (x > 0) //prevents oob { if (x == 15 && rightChunk == null) { blockDataArr[x, y, z].RightExists = false; } if (blockExistsArr[x - 1, y, z]) { //set for current blockDataArr[x, y, z].LeftExists = true; //set for left blockDataArr[x - 1, y, z].RightExists = true; } } //Try another chunk! else { if (leftChunk != null) { if (leftChunk.blockExistsArr[15, y, z]) { blockDataArr[x, y, z].LeftExists = true; leftChunk.blockDataArr[15, y, z].RightExists = true; leftChunk.blockDataArr[15, y, z].DetermineVisibility(); } } else { blockDataArr[x, y, z].LeftExists = false; } } //backCheck if (z > 0) //prevents oob { if (z == 15 && forwardChunk == null) { blockDataArr[x, y, z].ForwardExists = false; } if (blockExistsArr[x, y, z - 1]) { //set for current blockDataArr[x, y, z].BackExists = true; //set for back blockDataArr[x, y, z - 1].ForwardExists = true; } } //Check neighbor Chunk! else { if (backwardChunk != null) { if (backwardChunk.blockExistsArr[x, y, 15]) { blockDataArr[x, y, z].BackExists = true; backwardChunk.blockDataArr[x, y, 15].ForwardExists = true; backwardChunk.blockDataArr[x, y, 15].DetermineVisibility(); } } else { blockDataArr[x, y, z].BackExists = false; } } } } } } }
public void MakeTree(int x, int y, int z) { for (int t = 1; t < 6 && t < 58; t++) { blockDataArr[x, y + t, z] = new BlockDataInternal(); blockExistsArr[x, y + t, z] = true; blockDataArr[x, y + t, z].blockType = 1; } blockDataArr[x, y + 6, z] = new BlockDataInternal(); blockExistsArr[x, y + 6, z] = true; blockDataArr[x, y + 6, z].blockType = 3; int first = Random.Range(2, 5); int second = Random.Range(2, 5); int third = Random.Range(2, 5); int fourth = Random.Range(2, 5); if (x > 0) { blockDataArr[x - 1, y + first, z] = new BlockDataInternal(); blockExistsArr[x - 1, y + first, z] = true; blockDataArr[x - 1, y + first, z].blockType = 3; } else if (leftChunk != null) { leftChunk.blockDataArr[15, y + first, z] = new BlockDataInternal(); leftChunk.blockExistsArr[15, y + first, z] = true; leftChunk.blockDataArr[15, y + first, z].blockType = 3; } if (x < 15) { blockDataArr[x + 1, y + second, z] = new BlockDataInternal(); blockExistsArr[x + 1, y + second, z] = true; blockDataArr[x + 1, y + second, z].blockType = 3; } else if (rightChunk != null) { rightChunk.blockDataArr[0, y + second, z] = new BlockDataInternal(); rightChunk.blockExistsArr[0, y + second, z] = true; rightChunk.blockDataArr[0, y + second, z].blockType = 3; } if (z < 15) { blockDataArr[x, y + third, z + 1] = new BlockDataInternal(); blockExistsArr[x, y + third, z + 1] = true; blockDataArr[x, y + third, z + 1].blockType = 3; } else if (forwardChunk != null) { forwardChunk.blockDataArr[x, y + third, 0] = new BlockDataInternal(); forwardChunk.blockExistsArr[x, y + third, 0] = true; forwardChunk.blockDataArr[x, y + third, 0].blockType = 3; } if (z > 0) { blockDataArr[x, y + fourth, z - 1] = new BlockDataInternal(); blockExistsArr[x, y + fourth, z - 1] = true; blockDataArr[x, y + fourth, z - 1].blockType = 3; } else if (backwardChunk != null) { forwardChunk.blockDataArr[x, y + fourth, 15] = new BlockDataInternal(); forwardChunk.blockExistsArr[x, y + fourth, 15] = true; forwardChunk.blockDataArr[x, y + fourth, 15].blockType = 3; } }