public void SetPiece(WorldPos bPos, WorldPos gPos, LevelPiece piece) { if (pieceNames == null) { pieceNames = new string[9]; } GameObject pObj = (piece != null) ? piece.gameObject : null; int id = gPos.z * 3 + gPos.x; if (pObj != null) { pieceNames [id] = pObj.GetComponent <PaletteItem> ().name; } else { pieceNames [id] = null; } }
Chunk CreateChunk(int x, int y, int z) { VGlobal vg = VGlobal.GetSetting(); WorldPos chunkPos = new WorldPos(x, y, z); GameObject newChunkObject = Instantiate( chunkPrefab, new Vector3(x * vg.w, y * vg.h, z * vg.d), Quaternion.Euler(Vector3.zero) ) as GameObject; newChunkObject.name = "Chunk(" + x + "," + y + "," + z + ")"; newChunkObject.transform.parent = transform; newChunkObject.transform.localPosition = new Vector3(x * vg.w, y * vg.h, z * vg.d); newChunkObject.transform.localRotation = Quaternion.Euler(Vector3.zero); #if UNITY_EDITOR if (vertexMaterial != null && EditorApplication.isPlaying) { newChunkObject.GetComponent <Renderer> ().material = vg.FakeDeco ? vertexMaterial : Resources.Load(PathCollect.pieces + "/Materials/Mat_Voxel", typeof(Material)) as Material; } newChunkObject.layer = LayerMask.NameToLayer((EditorApplication.isPlaying) ? "Floor" : "Editor"); #else if (vertexMaterial != null) { newChunkObject.GetComponent <Renderer> ().material = vg.FakeDeco?vertexMaterial:Resources.Load(PathCollect.pieces + "/Materials/Mat_Voxel", typeof(Material)) as Material; } newChunkObject.layer = LayerMask.NameToLayer("Floor"); #endif Chunk newChunk = newChunkObject.GetComponent <Chunk> (); newChunk.cData.ChunkPos = chunkPos; newChunk.volume = this; chunks.Add(chunkPos, newChunk); return(newChunk); }
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 SetBlock(int x, int y, int z, Block _block) { Chunk chunk = GetChunk(x, y, z); Block oldBlock = GetBlock(x, y, z); if (chunk != null) { WorldPos chunkBlockPos = new WorldPos(x - chunk.cData.ChunkPos.x, y - chunk.cData.ChunkPos.y, z - chunk.cData.ChunkPos.z); if (_block != null) { _block.BlockPos = chunkBlockPos; Predicate <BlockAir> sameBlockAir = delegate(BlockAir b) { return(b.BlockPos.Compare(chunkBlockPos)); }; switch (_block.GetType().ToString()) { case "CreVox.BlockAir": if (!chunk.cData.blockAirs.Exists(sameBlockAir)) { chunk.cData.blockAirs.Add(_block as BlockAir); chunk.setBlockDict(chunkBlockPos, _block as BlockAir); } break; case "CreVox.BlockItem": Predicate <BlockItem> sameBlockItem = delegate(BlockItem b) { return(b.BlockPos.Compare(chunkBlockPos)); }; if (!chunk.cData.blockItems.Exists(sameBlockItem)) { chunk.cData.blockItems.Add(_block as BlockItem); chunk.setBlockDict(chunkBlockPos, _block as BlockItem); } break; case "CreVox.BlockHold": Predicate <BlockHold> sameBlockHold = delegate(BlockHold b) { return(b.BlockPos.Compare(chunkBlockPos)); }; if (!chunk.cData.blockHolds.Exists(sameBlockHold)) { chunk.cData.blockHolds.Add(_block as BlockHold); chunk.setBlockDict(chunkBlockPos, _block as BlockHold); } break; case "CreVox.Block": Predicate <Block> sameBlock = delegate(Block b) { return(b.BlockPos.Compare(chunkBlockPos)); }; if (chunk.cData.blockAirs.Exists(sameBlockAir)) { chunk.cData.blockAirs.Remove(oldBlock as BlockAir); WorldPos bPos = new WorldPos(x, y, z); if (nodes.ContainsKey(bPos)) { GameObject.DestroyImmediate(nodes [bPos].pieceRoot); nodes.Remove(bPos); } } if (!chunk.cData.blocks.Exists(sameBlock)) { chunk.cData.blocks.Add(_block); chunk.setBlockDict(chunkBlockPos, _block); } break; } } else if (oldBlock != null) { switch (oldBlock.GetType().ToString()) { case "CreVox.BlockAir": chunk.cData.blockAirs.Remove(oldBlock as BlockAir); chunk.setBlockDict(chunkBlockPos, null); break; case "CreVox.Block": Debug.LogWarning("chunk.cData.blocks.Contains([" + chunkBlockPos + "]:" + chunk.cData.blocks.Contains(oldBlock)); chunk.cData.blocks.Remove(oldBlock); chunk.setBlockDict(chunkBlockPos, null); break; } } } RemoveNodeIfIsEmpty(new WorldPos(x, y, z)); }
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(); }