//input is local position public void GetBlockData(int xInChunk, int worldY, int zInChunk, out byte blockType, out byte blockData) { blockType = 0; blockData = 0; if (worldY < 0 || worldY > 255) { return; } if (xInChunk < 0 || xInChunk > 15 || zInChunk < 0 || zInChunk > 15) { NBTHelper.GetBlockData(xInChunk + 16 * x, worldY, zInChunk + 16 * z, out blockType, out blockData); return; } int sectionIndex = Mathf.FloorToInt(worldY / 16f); if (sectionIndex >= 0 && sectionIndex < Sections.Count) { TagNodeCompound section = Sections[sectionIndex] as TagNodeCompound; TagNodeByteArray blocks = section["Blocks"] as TagNodeByteArray; TagNodeByteArray data = section["Data"] as TagNodeByteArray; int yInSection = worldY - sectionIndex * 16; int blockPos = yInSection * 16 * 16 + zInChunk * 16 + xInChunk; if (blockPos >= 0 && blockPos < 4096) { blockType = blocks.Data[blockPos]; blockData = NBTHelper.GetNibble(data.Data, blockPos); return; } } return; }
void DrawWireFrame() { WireFrameHelper.render = false; int cubeLayerIndex = LayerMask.NameToLayer("Chunk"); int otherPlayerLayerIndex = LayerMask.NameToLayer("OtherPlayer"); int plantLayerIndex = LayerMask.NameToLayer("Plant"); if (cubeLayerIndex != -1 && otherPlayerLayerIndex != -1 && plantLayerIndex != -1) { int layerMask = 1 << cubeLayerIndex | 1 << otherPlayerLayerIndex | 1 << plantLayerIndex; if (Physics.Raycast(Camera.main.ScreenPointToRay(center), out hit, 5f, layerMask)) { if (hit.transform.gameObject.layer == cubeLayerIndex || hit.transform.gameObject.layer == plantLayerIndex) { Vector3Int pos = Vector3Int.RoundToInt(hit.point - hit.normal / 100); NBTHelper.GetBlockData(pos.x, pos.y, pos.z, out byte type, out byte data); if (type != 0) { if (pos != WireFrameHelper.pos) { breakingTime = 0; } WireFrameHelper.render = true; WireFrameHelper.pos = pos; WireFrameHelper.hitPos = hit.point; WireFrameHelper.type = type; WireFrameHelper.data = data; WireFrameHelper.generator = NBTGeneratorManager.GetMeshGenerator(type); } } } } }
public override void OnRightClick() { bool isUpper = (WireFrameHelper.data & 0b1000) > 0; int direction = 0; bool isOpen = false; if (isUpper) { NBTHelper.GetBlockData(WireFrameHelper.pos.x, WireFrameHelper.pos.y - 1, WireFrameHelper.pos.z, out byte belowType, out byte belowData); if (belowType == 64) { isOpen = (belowData & 0b0100) > 0; direction = belowData & 0b0011; } byte newData = (byte)(belowData ^ 0b0100); NBTHelper.SetBlockData(WireFrameHelper.pos + Vector3Int.down, WireFrameHelper.type, newData); } else { isOpen = (WireFrameHelper.data & 0b0100) > 0; byte newData = (byte)(WireFrameHelper.data ^ 0b0100); NBTHelper.SetBlockData(WireFrameHelper.pos, WireFrameHelper.type, newData); } SoundManager.Play2DSound(isOpen ? "Player_Door_Close" : "Player_Door_Open"); NBTChunk chunk = NBTHelper.GetChunk(WireFrameHelper.pos); chunk.RebuildMesh(UpdateFlags.Collidable); }
void DrawWireFrame() { WireFrameHelper.render = false; Vector3 center = new Vector3(Screen.width / 2, Screen.height / 2, 0); int cubeLayerIndex = LayerMask.NameToLayer("Chunk"); int otherPlayerLayerIndex = LayerMask.NameToLayer("OtherPlayer"); int plantLayerIndex = LayerMask.NameToLayer("Plant"); if (cubeLayerIndex != -1 && otherPlayerLayerIndex != -1 && plantLayerIndex != -1) { int layerMask = 1 << cubeLayerIndex | 1 << otherPlayerLayerIndex | 1 << plantLayerIndex; if (Physics.Raycast(Camera.main.ScreenPointToRay(center), out hit, 5f, layerMask)) { if (hit.transform.gameObject.layer == cubeLayerIndex || hit.transform.gameObject.layer == plantLayerIndex) { Vector3Int pos = Vector3Int.RoundToInt(hit.point - hit.normal / 10); byte type = 0; byte data = 0; NBTHelper.GetBlockData(pos.x, pos.y, pos.z, ref type, ref data); if (type != 0) { if (pos != WireFrameHelper.pos) { breakTime = 0; } WireFrameHelper.render = true; WireFrameHelper.pos = pos; WireFrameHelper.hitPos = hit.point; WireFrameHelper.type = type; WireFrameHelper.data = data; if (leftMouseDown) { handAnimator.SetBool("isBreaking", true); breakTime += Time.deltaTime; NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(type); float breakNeedTime = generator.breakNeedTime; if (breakNeedTime == 0) { BreakBlock(pos); } else { int curStage = Mathf.FloorToInt(breakTime / (breakNeedTime / 12)); if (stage != curStage) { stage = curStage; UpdateBreakingEffect(generator, pos, stage); } if (stage >= 10) { BreakBlock(pos); } } return; } } } } } handAnimator.SetBool("isBreaking", false); }
public override void RenderWireframe(byte blockData) { bool isUpper = (blockData & 0b1000) > 0; DoorDirection direction = 0; bool isOpen = false; int hinge; if (isUpper) { NBTHelper.GetBlockData(WireFrameHelper.pos.x, WireFrameHelper.pos.y - 1, WireFrameHelper.pos.z, out byte belowType, out byte belowData); if (belowType == 64) { isOpen = (belowData & 0b0100) > 0; direction = (DoorDirection)(belowData & 0b0011); } hinge = (blockData & 0b0001); } else { isOpen = (blockData & 0b0100) > 0; direction = (DoorDirection)(blockData & 0b0011); NBTHelper.GetBlockData(WireFrameHelper.pos.x, WireFrameHelper.pos.y + 1, WireFrameHelper.pos.z, out byte aboveType, out byte aboveData); hinge = (aboveData & 0b0001); } float top, bottom, left, right, front, back; top = 0.501f; bottom = -0.501f; if ((direction == DoorDirection.East && isOpen == false) || (direction == DoorDirection.South && isOpen && hinge == 1) || (direction == DoorDirection.North && isOpen && hinge == 0)) { left = -0.501f; right = -0.3115f; front = 0.501f; back = -0.501f; } else if ((direction == DoorDirection.South && isOpen == false) || (direction == DoorDirection.East && isOpen && hinge == 0) || (direction == DoorDirection.West && isOpen && hinge == 1)) { left = -0.501f; right = 0.501f; front = -0.3115f; back = -0.501f; } else if ((direction == DoorDirection.West && isOpen == false) || (direction == DoorDirection.South && isOpen && hinge == 0) || (direction == DoorDirection.North && isOpen && hinge == 1)) { left = 0.3115f; right = 0.501f; front = 0.501f; back = -0.501f; } else if ((direction == DoorDirection.North && isOpen == false) || (direction == DoorDirection.East && isOpen && hinge == 1) || (direction == DoorDirection.West && isOpen && hinge == 0)) { left = -0.501f; right = 0.501f; front = 0.3115f; back = 0.501f; } else { left = -0.501f; right = 0.501f; front = 0.501f; back = -0.501f; } RenderWireframeByVertex(top, bottom, left, right, front, back); }