bool RemoveNodeIfIsEmpty(WorldPos bPos) { BlockAir blockAir = GetBlock(bPos.x, bPos.y, bPos.z) as BlockAir; bool isEmpty = true; if (blockAir != null) { foreach (string p in blockAir.pieceNames) { if (p != null && p.Length > 0) { isEmpty = false; break; } } if (isEmpty) { if (nodes.ContainsKey(bPos)) { GameObject.DestroyImmediate(nodes [bPos].pieceRoot); nodes.Remove(bPos); } } } else { isEmpty = false; } return(isEmpty); }
private void PlacePieces() { PaletteItem[] itemArray; #if UNITY_EDITOR itemArray = Resources.LoadAll <PaletteItem> ((EditorApplication.isPlaying && VGlobal.GetSetting().FakeDeco) ? vd.ArtPack : PathCollect.pieces); #else itemArray = Resources.LoadAll <PaletteItem> (VGlobal.GetSetting().FakeDeco ? vd.ArtPack : PathCollect.pieces); #endif foreach (Chunk c in chunks.Values) { for (int b = 0; b < c.cData.blockAirs.Count; b++) { BlockAir ba = c.cData.blockAirs [b]; for (int i = 0; i < ba.pieceNames.Length; i++) { for (int k = 0; k < itemArray.Length; k++) { if (ba.pieceNames [i] == itemArray [k].name) { PlacePiece( new WorldPos( c.cData.ChunkPos.x + ba.BlockPos.x, c.cData.ChunkPos.y + ba.BlockPos.y, c.cData.ChunkPos.z + ba.BlockPos.z), new WorldPos(i % 3, 0, (int)(i / 3)), itemArray [k].gameObject.GetComponent <LevelPiece> ()); } } } } } }
public void BuildVolume(Save _save, VolumeData _VData = null) { if (_VData == null && _useBytes == false) { return; } Reset(); if (_useBytes) //load .bytes { vd = null; Init(_save.chunkX, _save.chunkY, _save.chunkZ); foreach (var blockPair in _save.blocks) { Block block = blockPair.Value; if (block != null) { if (block is BlockAir) { BlockAir bAir = blockPair.Value as BlockAir; if (!RemoveNodeIfIsEmpty(bAir.BlockPos)) { SetBlock(blockPair.Key.x, blockPair.Key.y, blockPair.Key.z, bAir); } else { Debug.Log(bAir.BlockPos); } } else { SetBlock(blockPair.Key.x, blockPair.Key.y, blockPair.Key.z, new Block()); } } } } else //load ScriptableObject { Init(_VData.chunkX, _VData.chunkY, _VData.chunkZ); foreach (Chunk c in chunks.Values) { c.cData = _VData.GetChunk(c.cData.ChunkPos); } } PlacePieces(); UpdateChunks(); }
public void AddChunk(int _x, int _y, int _z, Chunk chunk) { int cx = _x * Chunk.chunkSize; int cy = _y * Chunk.chunkSize; int cz = _z * Chunk.chunkSize; for (int x = 0; x < Chunk.chunkSize; x++) { for (int y = 0; y < Chunk.chunkSize; y++) { for (int z = 0; z < Chunk.chunkSize; z++) { Block block = chunk.GetBlock(x, y, z); bool add = false; if (block == null) { add = false; } else { if (block is BlockAir) { BlockAir bAir = (BlockAir)block; if (bAir.pieceNames != null) { add = (bAir.pieceNames.Length > 0) ? true : false; } } else { add = true; } } if (add) { WorldPos pos = new WorldPos(cx + x, cy + y, cz + z); // Debug.Log ("Save: " + pos.ToString ()); blocks.Add(pos, block); } } } } }
public void PlacePiece(WorldPos bPos, WorldPos gPos, LevelPiece _piece) { Block block = GetBlock(bPos.x, bPos.y, bPos.z); BlockAir blockAir = null; GameObject pObj; if (block != null && !(block is BlockAir || block is BlockItem)) { return; } if (_piece != null) { if (block == null) { SetBlock(bPos.x, bPos.y, bPos.z, new BlockAir()); block = GetBlock(bPos.x, bPos.y, bPos.z); } if (!nodes.ContainsKey(bPos)) { CreateNode(bPos); } pObj = nodes [bPos].pieces [gPos.z * 3 + gPos.x]; if (pObj != null) { PlaceBlockHold(bPos, gPos.z * 3 + gPos.x, pObj.GetComponent <LevelPiece> (), true); GameObject.DestroyImmediate(pObj); } #if UNITY_EDITOR pObj = PrefabUtility.InstantiatePrefab(_piece.gameObject) as GameObject; #else pObj = GameObject.Instantiate(_piece.gameObject); #endif pObj.transform.parent = nodes [bPos].pieceRoot.transform; Vector3 pos = GetPieceOffset(gPos.x, gPos.z); VGlobal vg = VGlobal.GetSetting(); float x = bPos.x * vg.w + pos.x; float y = bPos.y * vg.h + pos.y; float z = bPos.z * vg.d + pos.z; pObj.transform.localPosition = new Vector3(x, y, z); pObj.transform.localRotation = Quaternion.Euler(0, GetPieceAngle(gPos.x, gPos.z), 0); nodes [bPos].pieces [gPos.z * 3 + gPos.x] = pObj; if (block is BlockAir) { blockAir = block as BlockAir; blockAir.SetPiece(bPos, gPos, pObj.GetComponent <LevelPiece> ()); blockAir.SolidCheck(nodes [bPos].pieces); if (_piece.isHold == true) { PlaceBlockHold(bPos, gPos.z * 3 + gPos.x, pObj.GetComponent <LevelPiece> (), false); } } } else { if (block is BlockAir) { blockAir = block as BlockAir; blockAir.SetPiece(bPos, gPos, null); blockAir.SolidCheck(nodes [bPos].pieces); } if (nodes.ContainsKey(bPos)) { pObj = nodes [bPos].pieces [gPos.z * 3 + gPos.x]; if (pObj != null) { PlaceBlockHold(bPos, gPos.z * 3 + gPos.x, pObj.GetComponent <LevelPiece> (), true); GameObject.DestroyImmediate(pObj); } } if (RemoveNodeIfIsEmpty(bPos)) { SetBlock(bPos.x, bPos.y, bPos.z, null); } } }
public void UpdateChunk() { foreach (Block block in cData.blocks) { if (!BlockDict.ContainsKey(block.BlockPos)) { setBlockDict(block.BlockPos, block); } } foreach (BlockHold bHold in cData.blockHolds) { if (!BlockDict.ContainsKey(bHold.BlockPos)) { setBlockDict(bHold.BlockPos, bHold); } } for (int i = 0; i < cData.blockAirs.Count; i++) { BlockAir bAir = cData.blockAirs [i]; bool isEmpty = true; foreach (string p in bAir.pieceNames) { if (p != "") { isEmpty = false; break; } } if (isEmpty) { cData.blockAirs.Remove(bAir); if (BlockDict.ContainsKey(bAir.BlockPos)) { BlockDict.Remove(bAir.BlockPos); } } else { if (!BlockDict.ContainsKey(bAir.BlockPos)) { setBlockDict(bAir.BlockPos, bAir); } WorldPos volumePos = new WorldPos( cData.ChunkPos.x + bAir.BlockPos.x, cData.ChunkPos.y + bAir.BlockPos.y, cData.ChunkPos.z + bAir.BlockPos.z); if (volume != null) { if (volume.GetNode(volumePos) != null) { #if UNITY_EDITOR if (!UnityEditor.EditorApplication.isPlaying && volume.cuter && bAir.BlockPos.y + cData.ChunkPos.y > volume.cutY) { volume.GetNode(volumePos).SetActive(false); } else { volume.GetNode(volumePos).SetActive(true); } #else volume.GetNode(volumePos).SetActive(true); #endif } } } } UpdateMeshFilter(); UpdateMeshCollider(); }