private void zdir(float dirz) { GameObject placeHolder; if (dirz >= 1) { for (int iter = 0; iter < dirz; iter++) { for (int x = 0; x < arraySize.x; x++) { for (int y = 0; y < arraySize.y; y++) { placeHolder = boxColliderGOs[x, y, 0]; for (int z = 0; z < arraySize.z - 1; z++) { boxColliderGOs[x, y, z] = boxColliderGOs[x, y, z + 1]; } placeHolder.transform.position = new Vector3( placeHolder.transform.position.x, placeHolder.transform.position.y, placeHolder.transform.position.z + arraySize.z); int xa = Mathf.CeilToInt(placeHolder.transform.position.x) - 1; int ya = Mathf.CeilToInt(placeHolder.transform.position.y) - 1; int za = Mathf.CeilToInt(placeHolder.transform.position.z) - 1; //Vector3 chunkcoords = new Vector3(0, 0, 0); int3 chunkcoords = new int3(0, 0, 0); int3 blockcoords = new int3(0, 0, 0); int3 offset = InfiniteWorld.ChunkWorldPositionOffset; /* * chunkcoords.x = Mathf.CeilToInt(((float)xa / (float)chunkSize.x - (float)offset.x)) - 1; * chunkcoords.y = Mathf.CeilToInt(((float)ya / (float)chunkSize.y - (float)offset.y)) - 1; * chunkcoords.z = Mathf.CeilToInt(((float)za / (float)chunkSize.z - (float)offset.z)) - 1; */ chunkcoords.x = xa / chunkSize.x - offset.x; chunkcoords.y = ya / chunkSize.y - offset.y; chunkcoords.z = za / chunkSize.z - offset.z; //Debug.Log(xa + " " + chunkcoords.x + " " + chunkSize.x + " " + offset.x); if (WorldInitializer.chunkArrayContainsKey(chunkcoords)) { blockcoords.x = xa % chunkSize.x; blockcoords.y = ya % chunkSize.y; blockcoords.z = za % chunkSize.z; if (blockcoords.x < 0) { blockcoords.x = chunkSize.x - Math.Abs(blockcoords.x); chunkcoords.x -= 1; } if (blockcoords.y < 0) { blockcoords.y = chunkSize.y - Math.Abs(blockcoords.y); chunkcoords.y -= 1; } if (blockcoords.z < 0) { blockcoords.z = chunkSize.z - Math.Abs(blockcoords.z); chunkcoords.z -= 1; } if (chunkcoords.x < 0 || chunkcoords.y < 0 || chunkcoords.z < 0) { placeHolder.GetComponent <BoxCollider>().enabled = false; } else { blockTypes[x, y, arraySize.z - 1] = WorldInitializer.chunkArray[chunkcoords.x, chunkcoords.y, chunkcoords.z]. blocks[blockcoords.x, blockcoords.y, blockcoords.z].blockType; if (blockTypes[x, y, arraySize.z - 1] != BlockType.Air) { placeHolder.GetComponent <BoxCollider>().enabled = true; } else { placeHolder.GetComponent <BoxCollider>().enabled = false; } } } else { placeHolder.GetComponent <BoxCollider>().enabled = false; } boxColliderGOs[x, y, arraySize.z - 1] = placeHolder; } } } } if (dirz <= -1) { for (int iter = 0; iter > dirz; iter--) { for (int y = 0; y < arraySize.y; y++) { for (int x = 0; x < arraySize.x; x++) { placeHolder = boxColliderGOs[x, y, arraySize.z - 1]; for (int z = arraySize.z - 1; z > 0; z--) { boxColliderGOs[x, y, z] = boxColliderGOs[x, y, z - 1]; } placeHolder.transform.position = new Vector3( placeHolder.transform.position.x, placeHolder.transform.position.y, placeHolder.transform.position.z - arraySize.z); int xa = Mathf.CeilToInt(placeHolder.transform.position.x) - 1; int ya = Mathf.CeilToInt(placeHolder.transform.position.y) - 1; int za = Mathf.CeilToInt(placeHolder.transform.position.z) - 1; int3 chunkcoords = new int3(0, 0, 0); int3 blockcoords = new int3(0, 0, 0); int3 offset = InfiniteWorld.ChunkWorldPositionOffset; /* * chunkcoords.x = Mathf.CeilToInt(((float)xa / (float)chunkSize.x - (float)offset.x)) - 1; * chunkcoords.y = Mathf.CeilToInt(((float)ya / (float)chunkSize.y - (float)offset.y)) - 1; * chunkcoords.z = Mathf.CeilToInt(((float)za / (float)chunkSize.z - (float)offset.z)) - 1; */ chunkcoords.x = xa / chunkSize.x - offset.x; chunkcoords.y = ya / chunkSize.y - offset.y; chunkcoords.z = za / chunkSize.z - offset.z; if (WorldInitializer.chunkArrayContainsKey(chunkcoords)) { blockcoords.x = xa % chunkSize.x; blockcoords.y = ya % chunkSize.y; blockcoords.z = za % chunkSize.z; if (blockcoords.x < 0) { blockcoords.x = chunkSize.x - Math.Abs(blockcoords.x); chunkcoords.x -= 1; } if (blockcoords.y < 0) { blockcoords.y = chunkSize.y - Math.Abs(blockcoords.y); chunkcoords.y -= 1; } if (blockcoords.z < 0) { blockcoords.z = chunkSize.z - Math.Abs(blockcoords.z); chunkcoords.z -= 1; } if (chunkcoords.x < 0 || chunkcoords.y < 0 || chunkcoords.z < 0) { placeHolder.GetComponent <BoxCollider>().enabled = false; } else { blockTypes[x, y, 0] = WorldInitializer.chunkArray[chunkcoords.x, chunkcoords.y, chunkcoords.z]. blocks[blockcoords.x, blockcoords.y, blockcoords.z].blockType; //Debug.Log(chunkcoords.x + " " + chunkcoords.y + " " + chunkcoords.z); //Debug.Log(blockcoords.x + " " + blockcoords.y + " " + blockcoords.z); if (blockTypes[x, y, 0] != BlockType.Air) { placeHolder.GetComponent <BoxCollider>().enabled = true; } else { placeHolder.GetComponent <BoxCollider>().enabled = false; } } } boxColliderGOs[x, y, 0] = placeHolder; } } } } }
private BlockType[] getNeighbourBlocks(int x, int y, int z) { int3 offset = InfiniteWorld.ChunkWorldPositionOffset; int3 arrayBasedChunkPos = new int3(chunkPosWorld.x - offset.x, chunkPosWorld.y - offset.y, chunkPosWorld.z - offset.z); BlockType x_minus; BlockType x_plus; BlockType y_minus; BlockType y_plus; BlockType z_minus; BlockType z_plus; int3 key; //Xminus if (x - 1 >= 0) { x_minus = blocks[x - 1, y, z].blockType; } else { key = new int3(arrayBasedChunkPos.x - 1, arrayBasedChunkPos.y, arrayBasedChunkPos.z); if (WorldInitializer.chunkArrayContainsKey(key)) { x_minus = WorldInitializer.chunkArray[key.x, key.y, key.z].blocks[CONST.chunkSize.x - 1, y, z].blockType; } else { x_minus = BlockType.Air; } } //Xplus if (x + 1 <= CONST.chunkSize.x - 1) { x_plus = blocks[x + 1, y, z].blockType; } else { key = new int3(arrayBasedChunkPos.x + 1, arrayBasedChunkPos.y, arrayBasedChunkPos.z); if (WorldInitializer.chunkArrayContainsKey(key)) { x_plus = WorldInitializer.chunkArray[key.x, key.y, key.z].blocks[0, y, z].blockType; } else { x_plus = BlockType.Air; } } //Yminus if (y - 1 >= 0) { y_minus = blocks[x, y - 1, z].blockType; } else { key = new int3(arrayBasedChunkPos.x, arrayBasedChunkPos.y - 1, arrayBasedChunkPos.z); if (WorldInitializer.chunkArrayContainsKey(key)) { y_minus = WorldInitializer.chunkArray[key.x, key.y, key.z].blocks[x, CONST.chunkSize.y - 1, z].blockType; } else { y_minus = BlockType.Air; } } //Yplus if (y + 1 <= CONST.chunkSize.y - 1) { y_plus = blocks[x, y + 1, z].blockType; } else { key = new int3(arrayBasedChunkPos.x, arrayBasedChunkPos.y + 1, arrayBasedChunkPos.z); if (WorldInitializer.chunkArrayContainsKey(key)) { y_plus = WorldInitializer.chunkArray[key.x, key.y, key.z].blocks[x, 0, z].blockType; } else { y_plus = BlockType.Air; } } //Zminus if (z - 1 >= 0) { z_minus = blocks[x, y, z - 1].blockType; } else { key = new int3(arrayBasedChunkPos.x, arrayBasedChunkPos.y, arrayBasedChunkPos.z - 1); if (WorldInitializer.chunkArrayContainsKey(key)) { z_minus = WorldInitializer.chunkArray[key.x, key.y, key.z].blocks[x, y, CONST.chunkSize.z - 1].blockType; } else { z_minus = BlockType.Air; } } //Zplus if (z + 1 <= CONST.chunkSize.z - 1) { z_plus = blocks[x, y, z + 1].blockType; } else { key = new int3(arrayBasedChunkPos.x, arrayBasedChunkPos.y, arrayBasedChunkPos.z + 1); if (WorldInitializer.chunkArrayContainsKey(key)) { z_plus = WorldInitializer.chunkArray[key.x, key.y, key.z].blocks[x, y, 0].blockType; } else { z_plus = BlockType.Air; } } BlockType[] neighbours = new BlockType[6]; neighbours[0] = x_minus; neighbours[1] = x_plus; neighbours[2] = y_minus; neighbours[3] = y_plus; neighbours[4] = z_minus; neighbours[5] = z_plus; return(neighbours); }
void Start() { int x = Mathf.CeilToInt(transform.position.x) - 1; int y = Mathf.CeilToInt(transform.position.y) - 1; int z = Mathf.CeilToInt(transform.position.z) - 1; //Debug.Log(x); playerLastFrameBlock = new Vector3(x, y, z); for (int xa = 0; xa < arraySize.x; xa++) { for (int ya = 0; ya < arraySize.y; ya++) { for (int za = 0; za < arraySize.z; za++) { blockPositions[xa, ya, za] = new Vector3( xa + loopStartPos.x + x - 0.5f, ya + loopStartPos.y + y + 0.5f, za + loopStartPos.z + z - 0.5f); int ccx = xa + loopStartPos.x + x; int ccy = ya + loopStartPos.y + y; int ccz = za + loopStartPos.z + z; int3 chunkcoords = new int3(0, 0, 0); int3 blockcoords = new int3(0, 0, 0); int3 offset = InfiniteWorld.ChunkWorldPositionOffset; chunkcoords.x = ccx / chunkSize.x - offset.x; chunkcoords.y = ccy / chunkSize.y - offset.y; chunkcoords.z = ccz / chunkSize.z - offset.z; boxColliderGOs[xa, ya, za] = new GameObject(); boxColliderGOs[xa, ya, za].AddComponent <BoxCollider>(); BoxCollider boxCollider = boxColliderGOs[xa, ya, za].GetComponent <BoxCollider>(); boxCollider.enabled = false; boxColliderGOs[xa, ya, za].transform.position = blockPositions[xa, ya, za]; if (WorldInitializer.chunkArrayContainsKey(chunkcoords)) { blockcoords.x = ccx % chunkSize.x; blockcoords.y = ccy % chunkSize.y; blockcoords.z = ccz % chunkSize.z; blockTypes[xa, ya, za] = WorldInitializer.chunkArray[chunkcoords.x, chunkcoords.y, chunkcoords.z]. blocks[blockcoords.x, blockcoords.y, blockcoords.z].blockType; if (blockTypes[xa, ya, za] != BlockType.Air) { boxCollider.enabled = true; } } else { blockTypes[xa, ya, za] = BlockType.Air; } } } } }