void Update() { ChunkPos temp = new ChunkPos(Camera.main.transform.position / chunkSize); // Did player move from their chunk? if (ChunkPos.CubeDistance(temp, _playerPos) > 0) { _playerPos = temp; // Set new pos GenerateChunks(); // Generate new chunks PingChunks(); // Ping old chunks for deletion } }
/// <summary> /// Checks whether chunks are still in view range or not, and destroys them if need be. /// </summary> private void PingChunks() { List <ChunkPos> temp = new List <ChunkPos>(); // Collect all chunks that need to be deleted foreach (KeyValuePair <ChunkPos, DataChunk> pair in _chunks) { if (ChunkPos.CubeDistance(pair.Key, _playerPos) > _viewRangeHorizontal + 1) { temp.Add(pair.Key); } else { /* * // Get chunk * Chunk chunkScript = pair.Value.GetChunk(); * * // Make only hidden chunks render! * if (chunkScript.GetRender()) * { * // Should chunk render yet? * chunkScript.SetRender(CubeDistance(_playerPos, pair.Key) <= _viewRangeHorizontal); * * // Queue chunk for generation * _loadQueue.Enqueue(chunkScript, 0); * } */ } } // Are there chunks that need generation? if (!_rendering && _loadQueue.Count > 0) { _rendering = true; new Thread(RenderThread).Start(); } // Destroy chunk foreach (ChunkPos key in temp) { DestroyChunk(key); } }