public void GetNeighbors() { int x = ChunkIndex.x; int y = ChunkIndex.y; int z = ChunkIndex.z; if (NeighborChunks[0] == null) { NeighborChunks[0] = ChunkManager.GetChunkComponent(x, y + 1, z); } if (NeighborChunks[1] == null) { NeighborChunks[1] = ChunkManager.GetChunkComponent(x, y - 1, z); } if (NeighborChunks[2] == null) { NeighborChunks[2] = ChunkManager.GetChunkComponent(x + 1, y, z); } if (NeighborChunks[3] == null) { NeighborChunks[3] = ChunkManager.GetChunkComponent(x - 1, y, z); } if (NeighborChunks[4] == null) { NeighborChunks[4] = ChunkManager.GetChunkComponent(x, y, z + 1); } if (NeighborChunks[5] == null) { NeighborChunks[5] = ChunkManager.GetChunkComponent(x, y, z - 1); } }
public static GameObject GetChunk(Index index) { //咱也不知道写这个后面要不要用,先写了吧,不嫌代码多 Chunk chunk = ChunkManager.GetChunkComponent(index); if (chunk == null) { return(null); } else { return(chunk.gameObject); } }
//有借鉴参考 private IEnumerator SpawnMissingChunks(int originX, int originY, int originZ, int range) { int heightRange = Engine.HeightRange; //清空更新队列updatequeue ChunkUpdateQueue = new List <Chunk>(); //flag chunk不在范围内 ChunksToDestroy = new List <Chunk>(); foreach (Chunk chunk in Chunks.Values) { if (Vector2.Distance(new Vector2(chunk.ChunkIndex.x, chunk.ChunkIndex.z), new Vector2(originX, originZ)) > range + Engine.ChunkDespawnDistance) { ChunksToDestroy.Add(chunk); } else if (Mathf.Abs(chunk.ChunkIndex.y - originY) > range + Engine.ChunkDespawnDistance) { ChunksToDestroy.Add(chunk); } } // main loop==看不懂了,原注释保留 for (int currentLoop = 0; currentLoop <= range; currentLoop++) { for (var x = originX - currentLoop; x <= originX + currentLoop; x++) { // iterate through all potential chunk indexes within range for (var y = originY - currentLoop; y <= originY + currentLoop; y++) { for (var z = originZ - currentLoop; z <= originZ + currentLoop; z++) { if (Mathf.Abs(y) <= heightRange) { // skip chunks outside of height range if (Mathf.Abs(originX - x) + Mathf.Abs(originZ - z) < range * 1.3f) { // skip corners // pause loop while the queue is not empty while (ChunkUpdateQueue.Count > 0) { ProcessChunkQueue(); if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration) { yield return(new WaitForEndOfFrame()); } } Chunk currentChunk = ChunkManager.GetChunkComponent(x, y, z); // chunks that already exist but haven't had their mesh built yet should be added to the update queue if (currentChunk != null) { // chunks without meshes spawned by server should be changed to regular chunks if (currentChunk.DisableMesh || currentChunk.EnableTimeout) { currentChunk.DisableMesh = false; currentChunk.EnableTimeout = false; currentChunk.Fresh = true; } if (currentChunk.Fresh) { // spawn neighbor chunks for (int d = 0; d < 6; d++) { Index neighborIndex = currentChunk.ChunkIndex.GetAdjacentIndex((Direction)d); GameObject neighborChunk = GetChunk(neighborIndex); if (neighborChunk == null) { neighborChunk = Instantiate(ChunkObject, neighborIndex.ToVector3(), transform.rotation) as GameObject; } currentChunk.NeighborChunks[d] = neighborChunk.GetComponent <Chunk>(); // always add the neighbor to NeighborChunks, in case it's not there already // continue loop in next frame if the current frame time is exceeded if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration) { yield return(new WaitForEndOfFrame()); } if (StopSpawning) { EndSequence(); yield break; } } if (currentChunk != null) { currentChunk.AddToQueueWhenReady(); } } } else { // if chunk doesn't exist, create new chunk (it adds itself to the update queue when its data is ready) // spawn chunk GameObject newChunk = Instantiate(ChunkObject, new Vector3(x, y, z), transform.rotation) as GameObject; // Spawn a new chunk. currentChunk = newChunk.GetComponent <Chunk>(); // spawn neighbor chunks if they're not spawned yet for (int d = 0; d < 6; d++) { Index neighborIndex = currentChunk.ChunkIndex.GetAdjacentIndex((Direction)d); GameObject neighborChunk = GetChunk(neighborIndex); if (neighborChunk == null) { neighborChunk = Instantiate(ChunkObject, neighborIndex.ToVector3(), transform.rotation) as GameObject; } currentChunk.NeighborChunks[d] = neighborChunk.GetComponent <Chunk>(); // always add the neighbor to NeighborChunks, in case it's not there already // continue loop in next frame if the current frame time is exceeded if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration) { yield return(new WaitForEndOfFrame()); } if (StopSpawning) { EndSequence(); yield break; } } if (currentChunk != null) { currentChunk.AddToQueueWhenReady(); } } } } // continue loop in next frame if the current frame time is exceeded if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration) { yield return(new WaitForEndOfFrame()); } if (StopSpawning) { EndSequence(); yield break; } } } } } yield return(new WaitForEndOfFrame()); EndSequence(); }