コード例 #1
0
    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
    }