public bool Generate(GenerationTemplate template) { Stopwatch sw = Stopwatch.StartNew(); // Completely fill the world with this type of terrain FillWorldWith(ETile.Grass); // Now, add patches of this type of terrain // First argument, how much to fill (ex: 10% of the world) // Second argument, how tight are the patch (1.0 = 1 patch, 0 = as many patches as possible). // These are just hints foreach (var patch in template.patchTemplate) { AddPatches(patch.tile, patch.percent, patch.tightness); } sw.Stop(); Debug.Log(string.Format("ChunkInfo::Generate took {0}ms", sw.ElapsedMilliseconds)); string fullDump = ""; for (int j = 0; j < Width; ++j) { for (int i = 0; i < Width; ++i) { fullDump += m_data[i, j].Tile.ToString().Substring(0, 1); } fullDump += "\n"; } Debug.Log(fullDump); return(true); }
private void InitializeGenerationTemplates() { // TODO: Could be data driven GenerationTemplate plainTemplate = new GenerationTemplate(); //plainTemplate.patchTemplate.Add(new PatchTemplate(ETile.Forest, 0.02f, 0.0f)); //plainTemplate.patchTemplate.Add(new PatchTemplate(ETile.Water, 0.02f, 0.0f)); m_biomeToGeneration[EBiome.Plain] = plainTemplate; GenerationTemplate oceanTemplate = new GenerationTemplate(); oceanTemplate.patchTemplate.Add(new PatchTemplate(ETile.Water, 1.0f, 1.0f)); m_biomeToGeneration[EBiome.Ocean] = oceanTemplate; GenerationTemplate forestTemplate = new GenerationTemplate(); forestTemplate.patchTemplate.Add(new PatchTemplate(ETile.Forest, 1.0f, 1.0f)); m_biomeToGeneration[EBiome.Forest] = forestTemplate; GenerationTemplate desertTemplate = new GenerationTemplate(); desertTemplate.patchTemplate.Add(new PatchTemplate(ETile.Desert, 1.0f, 1.0f)); m_biomeToGeneration[EBiome.Desert] = desertTemplate; GenerationTemplate mountainTemplate = new GenerationTemplate(); mountainTemplate.patchTemplate.Add(new PatchTemplate(ETile.Mountain, 1.0f, 1.0f)); m_biomeToGeneration[EBiome.Mountain] = mountainTemplate; }
private void SpawnChunkAt(Vector2 chunkPos) { Vector3 spawnPos = m_worldAnchorRoot.transform.position; spawnPos.x += chunkPos.x * 64; spawnPos.y += chunkPos.y * 64; Quaternion spawnRot = m_worldAnchorRoot.transform.rotation; Stopwatch sw = Stopwatch.StartNew(); int x = (int)chunkPos.x; int y = (int)chunkPos.y; GameObject chunkObj = (GameObject)Instantiate(m_worldMapChunkPrefab, spawnPos, spawnRot); chunkObj.GetComponent <Renderer>().material.mainTexture = m_tileTextureMap; chunkObj.name = string.Format("Chunk({0},{1})", x, y); sw.Stop(); UnityEngine.Debug.Log(string.Format("Mesh Instanciation in {0}ms", sw.ElapsedMilliseconds)); ChunkInfo chunkInfo = new ChunkInfo(chunkPos, chunkObj); // update all mapping m_chunks.AddLast(chunkInfo); m_posToChunks[new Vector2(chunkPos.x, chunkPos.y)] = chunkInfo; chunkInfo.PostInitialize(); TileMap anotherTileMap = chunkObj.GetComponent <TileMap>(); anotherTileMap.SourceChunk = chunkInfo; #if SIMPLE_BIOME for (int j = 0; j < ChunkInfo.Height; j++) { for (int i = 0; i < ChunkInfo.Width; i++) { Vector2 worldPos = Chunk2World(chunkInfo, i, j); int biomeX = (int)worldPos.x + m_biomeManager.Width / 2; int biomeY = (int)worldPos.y + m_biomeManager.Height / 2; Debug.Assert(biomeX >= 0 && biomeX < m_biomeManager.Width); Debug.Assert(biomeY >= 0 && biomeY < m_biomeManager.Height); EBiome biome = m_biomeManager.Map[biomeX, biomeY]; ETile tile = BiomeManager.Biome2Tile(biome); chunkInfo.WriteSlotValue(i, j, tile); } } // Special case for starting spot if (x == 0 && y == 0) { chunkInfo.GenerateSquare(ETile.Grass, 8, 8); } #else // Chunk goes from -Inf to + Inf // But biomeMap goes from 0 to Width / 2 // So, this won't work for boundary, but temporary remap by adding half-size // TODO: fix me int biomeX = x + m_biomeManager.Width / 2; int biomeY = y + m_biomeManager.Height / 2; EBiome biome = m_biomeManager.Map[biomeX, biomeY]; GenerationTemplate template = m_biomeManager.GetTemplateFromBiome(biome); chunkInfo.Generate(template); #endif }