示例#1
0
文件: MapEditor.cs 项目: jadmz/HexMap
    protected void sizeChange(int x, int y)
    {
        HexTile[,] map = new HexTile[x, y];
        // Get the size and extent
        Vector2 hexExtent = getHexExtent();
        Vector2 hexSize = getHexSize();

        // For each row, or each y coordinate
        for(int i = 0; i<map.GetLength(1); i++){
            float zpos = StartPosition.y + i*hexExtent.y*1.5F;
            float xOffset = hexExtent.x *i;

            // For each column or x coordinate
            for(int j = 0; j<map.GetLength(0); j++){
                float xPos = j*hexSize.x+xOffset-(i/2)*hexSize.x;
                Vector3 pos = new Vector3(xPos, 0, zpos);
                if(j<_map.GetLength(0) && i<_map.GetLength(1)){
                    map[j, i] = _map[j, i];
                }
                else{
                    GameObject obj = (GameObject)Instantiate(HexPrefab);
                    HexTile tile = obj.GetComponent<HexTile>();
                    map[j,i] = tile;
                    tile.gameObject.transform.position = pos;
                    tile.SetLocation(toMapPosition(j, i));
                    tile.Map = this;
                }
            }
        }

        if(MapSize.x -x >0 || MapSize.y- y >0){
            for(int i = 0; i < _map.GetLength(1); i++){
                for(int j = 0; j< _map.GetLength(0); j ++){
                    if(i>=y || j>= x){
                        Destroy(_map[j,i].gameObject);
                    }
                }
            }
        }
        _map = map;
        MapSize = new Vector2(_map.GetLength(0), _map.GetLength(1));
    }