public static void CreateChunk(int x, int y, int z) { // Get a chunk, either from the pool or brand new Chunk newChunk = InstantiateChunk(); // Set the position of the chunk World3 pos = new World3(x, y, z); newChunk.transform.position = pos.ToVector3(); newChunk.transparentChunk.transform.position = pos.ToVector3(); newChunk.pos = pos; // Add the chunk to the world chunks World.Chunks.Add(pos.GetHashCode(), newChunk); World.ChunkList.Add(newChunk); // Look for the rest of the chunks in this column of chunks bool columnBuilt = true; for (int i = 1 - Config.WorldHeight; i < 1; i++) { World3 chunkPos = new World3(pos.x, i * Chunk.Size, pos.z); Chunk chunk; if (Chunks.TryGetValue(chunkPos.GetHashCode(), out chunk)) { columnChunks[i + Config.WorldHeight - 1] = chunk; } else { columnBuilt = false; break; } } // If they exist we are ready to generate the column if (columnBuilt) { // Does this column already exist? World3 columnLocation = new World3(columnChunks[0].pos.x, 0, columnChunks[0].pos.z); if (!Columns.ContainsKey(columnLocation.GetHashCode())) { // Initialize the generator if it isn't if (!World._instance.generator.initialized) { World._instance.generator.Initialize(); } // Kick off generation. We get a region back that encompasses these chunks. Region region = Generator.Generate(columnChunks); Column column = new Column(region, columnChunks); Columns.Add(columnLocation.GetHashCode(), column); } } }
void ExecuteSpawn() { if (!Game.Active) { return; } if (World.Columns.Count > 0) { for (int i = 0; i < ChunkData.SpawnOrder.Count(); i++) { //translate the player position and array position into chunk position World3 spawnPosition = new World3 ( ChunkData.SpawnOrder[i].x * Chunk.Size + playerChunkPos.x, 0, ChunkData.SpawnOrder[i].z * Chunk.Size + playerChunkPos.z ); Column column; if (World.Columns.TryGetValue(spawnPosition.GetHashCode(), out column)) { if (!column.spawned && column.rendered) { // Only spawn if the player is still around World3 currentPosition = World.GetChunkPosition(new Vector3 ( Game.Player.transform.position.x, 0, Game.Player.transform.position.z )); World3 columnPosition = World.GetChunkPosition(new Vector3 ( column.region.min.x, 0, column.region.min.z )); float distance = Vector3.Distance(columnPosition.ToVector3(), currentPosition.ToVector3()); if (distance <= Config.SpawnRadius * Chunk.Size) { column.SpawnColumn(spawnPosition, World.Spawn); } } } } } }