Exemple #1
0
    // Разместить и раскрасить чанк
    MapChunk BuildChunk(Vector2 position)
    {
        MapChunk chunk = null;

        if (chunksPool.Count > 0)
        {
            chunk = chunksPool[0];
            chunksPool.Remove(chunk);
            chunk.gameObject.SetActive(true);
        }
        else
        {
            GameObject chunkObject = Instantiate(chunkPrefab);
            chunkObject.transform.SetParent(transform);
            chunk = chunkObject.GetComponent <MapChunk>();
            chunk.BuildMesh(chunkSize);
        }
        chunk.name     = "Chunk_" + position.x + "_" + position.y;
        chunk.position = position;
        MDTile[] tiles    = map.GetTilesAt((int)position.x, (int)position.y, chunkSize, chunkSize);
        GDTile[] textures = new GDTile[tiles.Length];
        for (int t = 0; t < tiles.Length; t++)
        {
            textures[t] = tiles[t].texture;
        }
        chunk.SetTextures(textures);
        return(chunk);
    }
Exemple #2
0
    // Use this for initialization
    void Start()
    {
        //TODO: eventually we probably want to not pull in entire map...or maybe we do if its only ints...
        //depends a lot on whether we end up making each tile a full object...if so than loading could be
        //costly in terms of time and/or memory
        //tiles go from left to right horizontally and from the bottom up vertically (0,0) is lower left corner
        gamemap = new HexMap(mapsize_x, mapsize_y);
        ////just setting some tiles for testing
        //gamemap.SetTile(0, 0, 1);
        //gamemap.SetTile(1, 0, 1);
        //gamemap.SetTile(0, 1, 1);
        //gamemap.SetTile(1, 1, 1);
        //gamemap.SetTile(1, 2, 1);
        //gamemap.SetTile(2, 0, 1);
        ////gamemap.SetTile(0, 1, 2);
        ////gamemap.SetTile(1, 1, 3);
        //gamemap.SetTile(5, 5, 3);
        //gamemap.SetTile(5, 10, 2);
        //gamemap.SetTile(16, 8, 2);
        int[,] maptiles = GameController.instance.mapLoader.GetMapTiles();
        gamemap.SetTiles(maptiles);

        //build and fill the tileUV data structure for our texture atlas
        tileuvs = buildTileUVs();

        //meshes can be a max size of 65535 verts
        //so doing a little math 65535/7 ~ 9362 which means chunksize_x * chunksize_y cant be greater than 9362
        //check and throw an error if so
        if (chunksize_x * chunksize_y > 9362)
        {
            throw new System.Exception("chunksize too big");
        }
        //TODO: eventually we probably will want to build out chunks just around where player is viewing
        //dynamically adding and removing meshes as we scroll around...but for now this is sufficient
        //next we need to build out meshes based on the chunk size...so iterate through
        //chunks building meshes for each as we go first get number of chunks
        int numchunks_x = mapsize_x / chunksize_x;
        int numchunks_y = mapsize_y / chunksize_y;

        //allocate array of mapchunks
        gamemapchunks = new MapChunk[numchunks_x, numchunks_y];
        for (int y = 0; y < numchunks_y; y++)
        {
            for (int x = 0; x < numchunks_x; x++)
            {
                MapChunk chunk = Instantiate(chunkprefab);
                chunk.name = "GameMapChunk[" + x + ", " + y + "]";
                chunk.BuildMesh(x, y, chunksize_x, chunksize_y, this);
                gamemapchunks[x, y] = chunk;
                chunk.transform.SetParent(this.transform);
            }
        }
    }