/// <summary> /// Generates up to the passed number of chunks, or infinite if -1. Then returns the total number generated. /// </summary> private int generateChunksFromInstructions(int max) { int builtChunks = 0; while (this.buildQueue.Count > 0 && (builtChunks < max || max == -1)) { NewChunkInstructions instructions = this.buildQueue.Dequeue(); Chunk chunk; if (this.cachedUnusedChunks.Count > 0) { // Pull out and reset an old chunk. chunk = this.cachedUnusedChunks.Dequeue(); chunk.gameObject.SetActive(true); //chunk.filter.mesh.Clear(); // TODO Large performance hit? } else { // The cache queue is empty, create a new chunk from scratch. chunk = GameObject.Instantiate(References.list.chunkPrefab, this.map.holderChunk).GetComponent <Chunk>(); } chunk.transform.position = instructions.chunkPos.toWorldSpaceVector(); this.map.loadChunk(chunk, instructions); builtChunks++; } return(builtChunks); }
/// <summary> /// Loads a new chunk, reading it from disk if it exists, otherwise a new one it generated. The Chunk is then returned. /// </summary> public Chunk loadChunk(Chunk chunk, NewChunkInstructions instructions) { chunk.initChunk(this, instructions); this.loadedChunks.Add(chunk.chunkPos, chunk); if (!this.mapSaver.readChunkFromDisk(chunk)) { // Generate chunk and compute lighting. this.mapGenerator.generateChunk(chunk); Chunk adjacentChunk; for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { for (int z = -1; z <= 1; z++) { if (!(x == 0 && y == 0 && z == 0)) // Not the middle chunk. { adjacentChunk = this.getChunk(new ChunkPos(chunk.chunkPos.x + x, chunk.chunkPos.z + z)); //if (adjacentChunk != null && !adjacentChunk.hasDoneGen2) { // this.tryPhase2OnChunk(adjacentChunk); //} } } } } // It's possible that this chunk itself needs phase 2. //this.tryPhase2OnChunk(chunk); } return(chunk); }
/// <summary> /// Acts like a constructor of a chunk. /// </summary> public void initChunk(World w, NewChunkInstructions instructions) { this.world = w; this.worldPos = instructions.chunkPos.toBlockPos(); this.chunkPos = instructions.chunkPos; this.isReadOnly = instructions.isReadOnly; float radius = 7f; this.chunkBounds = new Bounds(new Vector3(radius + this.worldPos.x, radius + this.worldPos.y, radius + this.worldPos.z), new Vector3(Chunk.SIZE, Chunk.SIZE, Chunk.SIZE)); this.name = "Chunk" + this.chunkPos.ToString(); }
/// <summary> /// Acts like a constructor of a chunk. /// </summary> public void initChunk(Map map, NewChunkInstructions instructions) { this.map = map; this.chunkPos = instructions.chunkPos; this.transform.position = this.chunkPos.toWorldSpaceVector(); //this.isReadOnly = instructions.isReadOnly; float radius = 7f; Vector3 worldPos = this.chunkPos.toWorldSpaceVector(); this.chunkBounds = new Bounds(new Vector3(radius + worldPos.x, radius + worldPos.y, radius + worldPos.z), new Vector3(Chunk.SIZE, Chunk.SIZE, Chunk.SIZE)); #if UNITY_EDITOR this.name = "Chunk" + this.chunkPos.ToString(); #endif }
protected void loadNewChunk(ChunkPos occupiedChunkPos, int x, int z, bool isReadOnly) { NewChunkInstructions instructions = new NewChunkInstructions(x + occupiedChunkPos.x, z + occupiedChunkPos.z, isReadOnly); Chunk chunk = this.map.getChunk(instructions.chunkPos); if (chunk == null) { if (!this.buildQueue.Contains(instructions)) { this.buildQueue.Enqueue(instructions); } } else { //chunk.isReadOnly = isReadOnly; } }