/// <summary> /// Called by the player to update the chunk loader. /// </summary> public void updateChunkLoader() { ChunkPos playerPos = this.getOccupiedChunkPos(this.player.transform.position); if (!(playerPos.Equals(this.previousOccupiedChunkPos))) { this.loadChunks(playerPos); } this.previousOccupiedChunkPos = playerPos; this.unloadChunks(playerPos); this.generateChunksFromInstructions(this.maxBuildPerLoop); }
private void Update() { playerChunkPos = GetChunkCoordFromPosition(player.position); if (!playerChunkPos.Equals(playerLastChunkPos)) { CheckViewDistance(); } if (chunksToDraw.Count > 0) { chunksToDraw.Dequeue().CreateMesh(); } if (!settings.enableMultiThreading) { if (!modsApplying) { ApplyModifications(); } if (chunksToUpdate.Count > 0) { UpdateChunks(); } } if (Input.GetKeyDown(KeyCode.F3)) { debug.SetActive(!debug.activeSelf); } if (Input.GetKeyDown(KeyCode.F1)) { WorldSaveSystem.SaveWorld(worldData); } }
void LoadChunks() { var pp = player.position; if (player.GetComponent <PlayerBehavior>().Enabled) { persistor.PlayerPosition = pp; } ChunkPos playerChunk = GetChunkPosition(pp); if (!curChunk.Equals(playerChunk)) { curChunk.x = playerChunk.x; curChunk.z = playerChunk.z; List <ChunkPos> toGenerate = new List <ChunkPos>(); for (int i = playerChunk.x - TerrainChunk.CHUNK_SIZE * chunkRenderDist; i <= playerChunk.x + TerrainChunk.CHUNK_SIZE * chunkRenderDist; i += TerrainChunk.CHUNK_SIZE) { for (int j = playerChunk.z - TerrainChunk.CHUNK_SIZE * chunkRenderDist; j <= playerChunk.z + TerrainChunk.CHUNK_SIZE * chunkRenderDist; j += TerrainChunk.CHUNK_SIZE) { ChunkPos cp = new ChunkPos(i, j); if (!chunks.ContainsKey(cp)) { toGenerate.Add(cp); } } } List <ChunkPos> toDestroy = new List <ChunkPos>(); foreach (ChunkPos cp in chunks.Keys) { if (Mathf.Abs(playerChunk.x - cp.x) > TerrainChunk.CHUNK_SIZE * (chunkRenderDist + chunkKeepDist) || Mathf.Abs(playerChunk.z - cp.z) > TerrainChunk.CHUNK_SIZE * (chunkRenderDist + chunkKeepDist) ) { toDestroy.Add(cp); } } toGenerate.RemoveAll(cp => Mathf.Abs(playerChunk.x - cp.x) > TerrainChunk.CHUNK_SIZE * (chunkRenderDist + 1) || Mathf.Abs(playerChunk.z - cp.z) > TerrainChunk.CHUNK_SIZE * (chunkRenderDist + 1)); foreach (ChunkPos cp in toDestroy) { chunks[cp].gameObject.SetActive(false); chunkPool.Enqueue(chunks[cp]); chunks.Remove(cp); } toGenerate.ForEach(BuildChunk); } if (!buildChunkQueue.IsEmpty) { StartCoroutine(DelayBuildChunks()); } }