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); }
public static void Init() { TagNodeCompound player = NBTHelper.GetPlayerData(); int gameMode = player["playerGameType"] as TagNodeInt; _mode = (GameMode)gameMode; }
public override void OnAddBlock(RaycastHit hit) { Vector3Int pos = WireFrameHelper.pos + Vector3Int.RoundToInt(hit.normal); if (CanAddBlock(pos)) { PlayerController.instance.PlayHandAnimation(); byte type = NBTGeneratorManager.id2type[id]; byte data = (byte)InventorySystem.items[ItemSelectPanel.curIndex].damage; if (hit.normal == Vector3.up || hit.normal == Vector3.down) { data |= 0b0000; } else if (hit.normal == Vector3.left || hit.normal == Vector3.right) { data |= 0b0100; } else if (hit.normal == Vector3.back || hit.normal == Vector3.forward) { data |= 0b1000; } NBTHelper.SetBlockData(pos, type, data); InventorySystem.DecrementCurrent(); ItemSelectPanel.instance.RefreshUI(); } }
void OnClickQuit() { NBTHelper.Save(); LocalServer.SaveData(); NetworkManager.Clear(); SceneManager.LoadScene("LoginScene"); }
//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; }
public static void Update() { UnityEngine.Profiling.Profiler.BeginSample("ChunkChecker.Update"); if (isRefreshing) { return; } Vector2Int curChunk = PlayerController.GetCurrentChunkPos(); // if player moved to another chunk or render distance is changed, then try to refresh chunks data. if (lastChunk != curChunk || lastRenderDistance != SettingsPanel.RenderDistance) { isRefreshing = true; tmpChunk = curChunk; lastRenderDistance = SettingsPanel.RenderDistance; // only load chunks in render distance (if render distance is greater than 6, then load chunks in 6) // and unload chunks out of render distance List <Vector2Int> loadChunks = NBTHelper.GetLoadChunks(curChunk); List <Vector2Int> unloadChunks = NBTHelper.GetUnloadChunks(curChunk, SettingsPanel.RenderDistance); if (loadChunks.Count > 0 || unloadChunks.Count > 0) { ChunkManager.ChunksEnterLeaveViewReq(loadChunks, unloadChunks); } else { FinishRefresh(); } } UnityEngine.Profiling.Profiler.EndSample(); }
bool CanAddBlock(Vector3Int pos) { NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(InventorySystem.items[ItemSelectPanel.curIndex].id); // 手上的 if (generator == null) { return(false); } byte type = NBTHelper.GetBlockByte(pos); NBTBlock targetGenerator = NBTGeneratorManager.GetMeshGenerator(type); if (generator is NBTPlant) { if (generator == targetGenerator) { return(false); } byte belowType = NBTHelper.GetBlockByte(pos + Vector3Int.down); //如果手上拿的是植物,则判断下方是否是否是实体 NBTBlock targetBelowGenerator = NBTGeneratorManager.GetMeshGenerator(belowType); return(targetBelowGenerator != null && !(targetBelowGenerator is NBTPlant)); } else { //如果手上拿的不是植物,则判断碰撞盒是否与玩家相交 return(!cc.bounds.Intersects(new Bounds(pos, Vector3.one))); } }
public static void ShowBlock(NBTObject generator, short data) { instance.handMeshRenderer.enabled = false; instance.blockMeshFilter.sharedMesh = generator.GetItemMesh(NBTHelper.GetChunk(GetCurrentBlock()), (byte)data); instance.blockMeshRenderer.GetComponent <MeshRenderer>().sharedMaterial = generator.GetItemMaterial((byte)data); instance.blockMeshFilter.transform.gameObject.SetActive(true); }
void InitTileEntity() { if (hasInitTileEntity) { return; } foreach (Vector3Int pos in tileEntityList) { if (!tileEntityObjs.ContainsKey(pos)) { int sectionIndex = pos.y / 16; TagNodeCompound Section = Sections[sectionIndex] as TagNodeCompound; TagNodeByteArray Blocks = Section["Blocks"] as TagNodeByteArray; TagNodeByteArray Data = Section["Data"] as TagNodeByteArray; int yInSection = pos.y % 16; int blockPos = yInSection * 16 * 16 + pos.z * 16 + pos.x; byte rawType = Blocks.Data[blockPos]; NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(rawType); byte blockData = NBTHelper.GetNibble(Data.Data, blockPos); GameObject obj = generator.GetTileEntityGameObject(this, blockData, pos); tileEntityObjs[pos] = obj; } } hasInitTileEntity = true; }
// Update is called once per frame void Update() { Vector3 pos = PlayerController.instance.transform.position; Vector3Int curBlock = PlayerController.GetCurrentBlock(); int chunkX = Mathf.FloorToInt(curBlock.x / 16f); int chunkY = Mathf.FloorToInt(curBlock.y / 16f); int chunkZ = Mathf.FloorToInt(curBlock.z / 16f); int xInChunk = curBlock.x - chunkX * 16; int yInChunk = curBlock.y - chunkY * 16; int zInChunk = curBlock.z - chunkZ * 16; Vector3Int posInt = pos.ToVector3Int(); NBTHelper.GetLightsByte(posInt.x, posInt.y, posInt.z, out byte skyLight, out byte blockLight); byte maxLight = skyLight > blockLight ? skyLight : blockLight; UnityEngine.Profiling.Profiler.BeginSample("zstring"); using (zstring.Block()) { zstring text = zstring.Format(template, Application.version, GetFPS(), pos.x, pos.y, pos.z, curBlock.x, curBlock.y, curBlock.z); text += zstring.Format(template2, xInChunk, yInChunk, zInChunk, chunkX, chunkY, chunkZ, maxLight, skyLight, blockLight); if (WireFrameHelper.render) { text += zstring.Format(template3, WireFrameHelper.pos.x, WireFrameHelper.pos.y, WireFrameHelper.pos.z, WireFrameHelper.generator.name, WireFrameHelper.data); } label.text = text; label2.text = ChunkRefresher.GetChunkUpdatesCount() + (zstring)" chunk updates"; } UnityEngine.Profiling.Profiler.EndSample(); }
private void OnDestroy() { ChunkPool.Uninit(); InputManager.Destroy(); UISystem.DestroyUIRoot(); PlayerController.Destroy(); NBTHelper.Uninit(); }
public static void PreloadChunks(List <Vector2Int> enterViewChunks) { foreach (Vector2Int chunkPos in enterViewChunks) { NBTChunk chunk = NBTHelper.LoadChunk(chunkPos.x, chunkPos.y); ChunkRefresher.Add(chunk); } ChunkRefresher.ForceRefreshAll(); }
public void GetLightsByte(int xInChunk, int yInChunk, int zInChunk, out byte skyLight, out byte blockLight, bool extends = false) { skyLight = 15; blockLight = 0; if (xInChunk < 0 || xInChunk > 15 || zInChunk < 0 || zInChunk > 15) { if (extends) { int xOffset = 0; int zOffset = 0; if (xInChunk < 0) { xOffset = -1; } else if (xInChunk > 15) { xOffset = 1; } if (zInChunk < 0) { zOffset = -1; } else if (zInChunk > 15) { zOffset = 1; } NBTChunk chunk = NBTHelper.GetChunk(x + xOffset, z + zOffset); if (chunk != null) { chunk.GetLightsByte(xInChunk - xOffset * 16, yInChunk, zInChunk - zOffset * 16, out skyLight, out blockLight); } } return; } int sectionIndex = yInChunk / 16; if (sectionIndex >= Sections.Count || yInChunk < 0 || yInChunk > 255) { return; } int yInSection = yInChunk % 16; int blockPos = yInSection * 16 * 16 + zInChunk * 16 + xInChunk; TagNodeCompound Section = Sections[sectionIndex] as TagNodeCompound; TagNodeByteArray SkyLight = Section["SkyLight"] as TagNodeByteArray; skyLight = NBTHelper.GetNibble(SkyLight.Data, blockPos); TagNodeByteArray BlockLight = Section["BlockLight"] as TagNodeByteArray; blockLight = NBTHelper.GetNibble(BlockLight.Data, blockPos); }
static void Single_OnLoginReq(object obj, Action <object> callback) { CSLoginReq req = obj as CSLoginReq; Vector3 pos = NBTHelper.GetPlayerPos(); Vector3 rot = NBTHelper.GetPlayerRot(); CSLoginRes rsp = new CSLoginRes() { RetCode = 0, PlayerData = new CSPlayer { PlayerID = 0, Name = "Steve", Position = pos.ToCSVector3(), Rotation = rot.ToCSVector3(), SelectIndex = playerData.SelectIndex, } }; for (int i = 0; i < 18; i++) { rsp.PlayerData.BagItems.Add(new CSItem { Type = playerData.BagItems[i].type, Count = playerData.BagItems[i].count }); } for (int i = 0; i < 9; i++) { rsp.PlayerData.SelectItems.Add(new CSItem { Type = playerData.SelectItems[i].type, Count = playerData.SelectItems[i].count }); } foreach (KeyValuePair <Vector3Int, Vector3Int> kvPair in dependenceDict) { rsp.BlockAttrs.Add(new CSBlockAttrs { pos = kvPair.Key.ToCSVector3Int(), depentPos = kvPair.Value.ToCSVector3Int(), }); } foreach (KeyValuePair <Vector3Int, CSBlockOrientation> kvPair in orientationDict) { rsp.BlockAttrs.Add(new CSBlockAttrs { pos = kvPair.Key.ToCSVector3Int(), orient = kvPair.Value, }); } callback(rsp); }
private void Start() { instance = this; TagNodeCompound levelDat = NBTHelper.GetLevelDat(); TagNodeLong dayTimeNode = levelDat["DayTime"] as TagNodeLong; int dayTime = (int)dayTimeNode.Data; tick = dayTime; }
void OnClickSingle() { DataCenter.name = "Steve"; DataCenter.spawnPosition = NBTHelper.GetPlayerPos(); DataCenter.spawnRotation = NBTHelper.GetPlayerRot(); MainMenu.Close(); LoadingUI.Show(); SceneManager.LoadScene("GameScene"); //ChatPanel.AddLine(DataCenter.name + ", welcome!"); }
public override void OnDestroyBlock(Vector3Int globalPos, byte blockData) { bool isUpper = (blockData & 0b1000) > 0; if (isUpper) { NBTHelper.SetBlockByteNoUpdate(globalPos - Vector3Int.up, 0); } else { NBTHelper.SetBlockByteNoUpdate(globalPos + Vector3Int.up, 0); } }
public static void ShowBlock(NBTObject generator, short data) { instance.handMeshRenderer.enabled = false; try { instance.blockMeshFilter.sharedMesh = generator.GetItemMesh(NBTHelper.GetChunk(GetCurrentBlock()), Vector3Int.RoundToInt(instance.position), (byte)data); } catch (System.Exception e) { Debug.LogError("showblock error, generator=" + generator + ",message=\n" + e.Message); } instance.blockMeshRenderer.GetComponent <MeshRenderer>().sharedMaterial = generator.GetItemMaterial((byte)data); instance.blockMeshFilter.transform.gameObject.SetActive(true); }
//input is local position public void GetBlockData(int xInChunk, int worldY, int zInChunk, ref byte blockType, ref byte blockData) { if (xInChunk < 0 || xInChunk > 15 || worldY < 0 || worldY > 255 || zInChunk < 0 || zInChunk > 15) { //if (xInChunk < 0) //{ // NBTChunk chunk = NBTHelper.GetChunk(x - 1, z); // chunk.GetBlockData(xInChunk + 16, worldY, zInChunk, ref blockType, ref blockData); //} //else if (xInChunk > 15) //{ // NBTChunk chunk = NBTHelper.GetChunk(x + 1, z); // chunk.GetBlockData(xInChunk - 16, worldY, zInChunk, ref blockType, ref blockData); //} //else if (zInChunk < 0) //{ // NBTChunk chunk = NBTHelper.GetChunk(x, z - 1); // chunk.GetBlockData(xInChunk, worldY, zInChunk + 16, ref blockType, ref blockData); //} //else if (zInChunk > 15) //{ // NBTChunk chunk = NBTHelper.GetChunk(x, z + 1); // chunk.GetBlockData(xInChunk, worldY, zInChunk - 16, ref blockType, ref blockData); //} //NBTHelper.GetBlockData(xInChunk + 16 * x, worldY, zInChunk + 16 * z, ref blockType, ref 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; }
public virtual void OnAddBlock(RaycastHit hit) { Vector3Int pos = WireFrameHelper.pos + Vector3Int.RoundToInt(hit.normal); if (CanAddBlock(pos)) { PlayerController.instance.PlayHandAnimation(); byte type = NBTGeneratorManager.id2type[id]; byte data = (byte)InventorySystem.items[ItemSelectPanel.curIndex].damage; NBTHelper.SetBlockData(pos, type, data); InventorySystem.DecrementCurrent(); ItemSelectPanel.instance.RefreshUI(); } }
public static void ChunksEnterLeaveViewReq(List <Vector2Int> enterViewChunks, List <Vector2Int> leaveViewChunks = null) { foreach (Vector2Int chunkPos in enterViewChunks) { NBTChunk chunk = NBTHelper.LoadChunk(chunkPos.x, chunkPos.y); ChunkRefresher.Add(chunk); } if (leaveViewChunks != null) { foreach (Vector2Int chunk in leaveViewChunks) { UnloadChunk(chunk.x, chunk.y); } } ChunkChecker.FinishRefresh(); }
protected void InitData() { NBTChunk chunk = NBTHelper.GetChunk(pos); if (chunk != null && chunk.tileEntityDict.ContainsKey(pos)) { Items = (TagNodeList)chunk.tileEntityDict[pos]["Items"]; foreach (TagNodeCompound item in Items) { byte slot = item["Slot"] as TagNodeByte; InventorySystem.items[slot + 46].id = item["id"] as TagNodeString; InventorySystem.items[slot + 46].damage = item["Damage"] as TagNodeShort; InventorySystem.items[slot + 46].count = item["Count"] as TagNodeByte; } } }
public static void RespawnRefreshChunks() { var preloadChunks = Utilities.GetSurroudingChunks(PlayerController.GetCurrentChunkPos(), 1); var unloadChunks = chunkDict.Keys.Except(preloadChunks).ToList(); foreach (Vector2Int chunk in unloadChunks) { NBTHelper.RemoveChunk(chunk.x, chunk.y); } foreach (Vector2Int chunkPos in preloadChunks) { NBTChunk chunk = NBTHelper.LoadChunk(chunkPos.x, chunkPos.y); ChunkRefresher.Add(chunk); } ChunkRefresher.ForceRefreshAll(); }
// Start is called before the first frame update void Start() { shadowTrans = transform.Find("shadow"); shadow = shadowTrans.GetComponent <Renderer>().material; meshTrans = transform.Find("mesh_parent/mesh"); MeshFilter meshFilter = meshTrans.GetComponent <MeshFilter>(); Vector3Int pos = Vector3Int.RoundToInt(transform.position); meshFilter.sharedMesh = generator.GetItemMesh(NBTHelper.GetChunk(PlayerController.GetCurrentBlock()), pos, blockData); meshFilter.transform.localScale = generator.itemSize; meshTrans.GetComponent <MeshRenderer>().sharedMaterial = generator.GetItemMaterial(blockData); RefreshMesh(); }
void BreakBlock(Vector3Int pos) { if (Time.time - lastBreakTime < 0.1f) { return; } lastBreakTime = Time.time; breakingTime = 0; HideBreakingEffect(); //DeleteBlockReq(WireFrameHelper.pos); NBTBlock generator = WireFrameHelper.generator; if (generator.hasDropItem) { try { string id = generator.GetDropItemByData(WireFrameHelper.data); byte data = generator.GetDropItemData(WireFrameHelper.data); Item.CreateBlockDropItem(id, data, pos); } catch (System.Exception e) { Debug.LogError("create item error, id=" + generator.GetDropItemByData(WireFrameHelper.data) + ",generator=" + generator); } } generator.OnDestroyBlock(WireFrameHelper.pos, WireFrameHelper.data); if (generator.isTileEntity) { NBTChunk chunk = NBTHelper.GetChunk(WireFrameHelper.pos); chunk.RemoveTileEntity(WireFrameHelper.pos); NBTHelper.SetBlockByteNoUpdate(WireFrameHelper.pos, 0); } else { NBTHelper.SetBlockByte(WireFrameHelper.pos, 0); } //Item.CreateBlockDropItem(type, WireFrameHelper.pos); BreakBlockEffect.Create(WireFrameHelper.type, WireFrameHelper.data, WireFrameHelper.pos); SoundManager.PlayBreakSound(WireFrameHelper.type, instance.gameObject); }
public void Respawn() { TagNodeCompound player = NBTHelper.GetPlayerData(); int x = player["SpawnX"] as TagNodeInt; int y = player["SpawnY"] as TagNodeInt; int z = player["SpawnZ"] as TagNodeInt; Vector3 spawnVec = new Vector3(x, y, z); transform.position = spawnVec; lastPosOnGround = spawnVec; NBTHelper.RespawnRefreshChunks(); Time.timeScale = 1; Health = 20; }
//input is local position public byte GetBlockByte(int xInChunk, int worldY, int zInChunk) { if (xInChunk < 0 || xInChunk > 15 || zInChunk < 0 || zInChunk > 15) { //if (xInChunk < 0) //{ // NBTChunk chunk = NBTHelper.GetChunk(x - 1, z); // return chunk.GetBlockByte(xInChunk + 16, worldY, zInChunk); //} //else if (xInChunk > 15) //{ // NBTChunk chunk = NBTHelper.GetChunk(x + 1, z); // return chunk.GetBlockByte(xInChunk - 16, worldY, zInChunk); //} //else if (zInChunk < 0) //{ // NBTChunk chunk = NBTHelper.GetChunk(x, z - 1); // return chunk.GetBlockByte(xInChunk, worldY, zInChunk + 16); //} //else if (zInChunk > 15) //{ // NBTChunk chunk = NBTHelper.GetChunk(x, z + 1); // return chunk.GetBlockByte(xInChunk, worldY, zInChunk - 16); //} return(NBTHelper.GetBlockByte(xInChunk + 16 * x, worldY, zInChunk + 16 * z)); } int sectionIndex = Mathf.FloorToInt(worldY / 16f); if (sectionIndex >= 0 && sectionIndex < Sections.Count) { TagNodeCompound section = Sections[sectionIndex] as TagNodeCompound; TagNodeByteArray blocks = section["Blocks"] as TagNodeByteArray; int yInSection = worldY - sectionIndex * 16; int blockPos = yInSection * 16 * 16 + zInChunk * 16 + xInChunk; if (blockPos >= 0 && blockPos < 4096) { return(blocks.Data[blockPos]); } } return(0); }
public static void Init() { TagNodeCompound playerData = NBTHelper.GetPlayerData(); TagNodeList Inventory = playerData["Inventory"] as TagNodeList; for (int i = 0; i < Inventory.Count; i++) { TagNodeCompound item = Inventory[i] as TagNodeCompound; byte slot = item["Slot"] as TagNodeByte; byte count = item["Count"] as TagNodeByte; short damage = item["Damage"] as TagNodeShort; string id = item["id"] as TagNodeString; items[slot].id = id; items[slot].count = count; items[slot].damage = damage; } }
// Start is called before the first frame update void Start() { shadowTrans = transform.Find("shadow"); shadow = shadowTrans.GetComponent <Renderer>().material; meshTrans = transform.Find("mesh_parent/mesh"); MeshFilter meshFilter = meshTrans.GetComponent <MeshFilter>(); meshFilter.sharedMesh = generator.GetItemMesh(NBTHelper.GetChunk(PlayerController.GetCurrentBlock()), blockData); if (generator is NBTBlock && !(generator is NBTPlant)) { meshFilter.transform.localScale = Vector3.one / 2; } meshTrans.GetComponent <MeshRenderer>().sharedMaterial = generator.GetItemMaterial(blockData); RefreshMesh(); }