private void TickBlocks() { int count = m_BlocksToTickQueue.Count; if (count > MaxTickBlockCountPerFrame) { count = MaxTickBlockCountPerFrame; // 防止卡死 } while (count-- > 0) { Vector3Int blockPos = m_BlocksToTickQueue.Dequeue(); int x = blockPos.x; int y = blockPos.y; int z = blockPos.z; RWAccessor.GetBlock(x, y, z)?.Tick(this, x, y, z); } }
private void LightBlocks(Stack <Vector3Int> queue, ref int limit, ModificationSource source) { while (limit-- > 0 && queue.Count > 0) { Vector3Int blockPos = queue.Pop(); if (blockPos.y < 0 || blockPos.y >= ChunkHeight) { continue; } if (!ChunkManager.GetChunk(ChunkPos.GetFromAny(blockPos.x, blockPos.z), false, out _)) { // 我不想管这个了,如果有人有好的算法请告诉我! // m_BlocksToLightQueue.Push(blockPos); // break; continue; } int x = blockPos.x; int y = blockPos.y; int z = blockPos.z; BlockData block = RWAccessor.GetBlock(x, y, z); int opacity = Mathf.Max(block.LightOpacity, 1); int finalLight = 0; if (opacity < MaxLight || block.LightValue > 0) // 不然就是0 { int max = RWAccessor.GetAmbientLight(x + 1, y, z); int temp; if ((temp = RWAccessor.GetAmbientLight(x - 1, y, z)) > max) { max = temp; } if ((temp = RWAccessor.GetAmbientLight(x, y + 1, z)) > max) { max = temp; } if ((temp = RWAccessor.GetAmbientLight(x, y - 1, z)) > max) { max = temp; } if ((temp = RWAccessor.GetAmbientLight(x, y, z + 1)) > max) { max = temp; } if ((temp = RWAccessor.GetAmbientLight(x, y, z - 1)) > max) { max = temp; } finalLight = max - opacity; if (block.LightValue > finalLight) { finalLight = block.LightValue; // 假设这个值一定是合法的(不过确实应该是合法的) } else if (finalLight < 0) { finalLight = 0; } //else if (finalLight > MaxLight) //{ // finalLight = MaxLight; //} } if (RWAccessor.SetAmbientLightLevel(x, y, z, finalLight, source)) { queue.Push(new Vector3Int(x - 1, y, z)); queue.Push(new Vector3Int(x, y - 1, z)); queue.Push(new Vector3Int(x, y, z - 1)); queue.Push(new Vector3Int(x + 1, y, z)); queue.Push(new Vector3Int(x, y + 1, z)); queue.Push(new Vector3Int(x, y, z + 1)); } } }