public void DoAction( AutoTileMap _autoTileMap ) { int tileMinX = _autoTileMap.MapTileWidth-1; int tileMinY = _autoTileMap.MapTileHeight-1; int tileMaxX = 0; int tileMaxY = 0; for( int i = 0; i < aTileData.Count; ++i ) { TileData tileData = aTileData[i]; // save prev tile type for undo action tileData.Tile_type_prev = _autoTileMap.GetAutoTile( tileData.Tile_x, tileData.Tile_y, tileData.Tile_layer ).Idx; _autoTileMap.SetAutoTile( tileData.Tile_x, tileData.Tile_y, tileData.Tile_type, tileData.Tile_layer ); tileMinX = Mathf.Min( tileMinX, tileData.Tile_x ); tileMinY = Mathf.Min( tileMinY, tileData.Tile_y ); tileMaxX = Mathf.Max( tileMaxX, tileData.Tile_x ); tileMaxY = Mathf.Max( tileMaxY, tileData.Tile_y ); } if (_autoTileMap.BrushGizmo.IsRefreshMinimapEnabled) { _autoTileMap.RefreshMinimapTexture(tileMinX, tileMinY, (tileMaxX - tileMinX) + 1, (tileMaxY - tileMinY) + 1); } _autoTileMap.UpdateChunks(); }
/// <summary> /// Save the map configuration /// </summary> /// <param name="_autoTileMap"></param> /// <returns></returns> public bool SaveData( AutoTileMap _autoTileMap ) { // avoid clear map data when auto tile map is not initialized if( !_autoTileMap.IsInitialized ) { //Debug.LogError(" Error saving data. Autotilemap is not initialized! Map will not be saved. "); return false; } TileMapWidth = _autoTileMap.MapTileWidth; TileMapHeight = _autoTileMap.MapTileHeight; TileData.Clear(); for( int iLayer = 0; iLayer < (int)AutoTileMap.eTileLayer._SIZE; ++iLayer ) { List<int> tileData = new List<int>(TileMapWidth*TileMapHeight); int iTileRepetition = 0; int savedType = 0; for( int tile_y = 0; tile_y < TileMapHeight; ++tile_y ) { for( int tile_x = 0; tile_x < TileMapWidth; ++tile_x ) { int iType = _autoTileMap.GetAutoTile( tile_x, tile_y, iLayer ).Idx; if( iTileRepetition == 0 ) { savedType = iType; iTileRepetition = 1; } else { // compression data. All tiles of the same type are store with number of repetitions ( negative number ) and type // ex: 5|5|5|5 --> |-4|5| (4 times 5) ex: -1|-1|-1 --> |-3|-1| ( 3 times -1 ) if( iType == savedType ) ++iTileRepetition; else { if( iTileRepetition > 1 ) { tileData.Add( -iTileRepetition ); } tileData.Add( savedType ); savedType = iType; iTileRepetition = 1; } } } } // save last tile type found if( iTileRepetition > 1 ) { tileData.Add( -iTileRepetition ); } tileData.Add( savedType ); // TileData.Add( new TileLayer(){ Tiles = tileData } ); } return true; }
/// <summary> /// Save the map configuration /// </summary> /// <param name="_autoTileMap"></param> /// <returns></returns> public bool SaveData( AutoTileMap _autoTileMap, int width = -1, int height = -1 ) { if (width < 0) width = TileMapWidth; if (height < 0) height = TileMapHeight; // avoid clear map data when auto tile map is not initialized if( !_autoTileMap.IsInitialized ) { //Debug.LogError(" Error saving data. Autotilemap is not initialized! Map will not be saved. "); return false; } Metadata.version = k_version; TileData.Clear(); for( int iLayer = 0; iLayer < _autoTileMap.GetLayerCount(); ++iLayer ) { AutoTileMap.MapLayer mapLayer = _autoTileMap.MapLayers[iLayer]; List<int> tileData = new List<int>(width * height); int iTileRepetition = 0; int savedTileId = 0; for (int tile_y = 0; tile_y < height; ++tile_y) { for (int tile_x = 0; tile_x < width; ++tile_x) { int iType = _autoTileMap.GetAutoTile( tile_x, tile_y, iLayer ).Id; //+++fix: FogOfWar tiles could be < -1, and this is not good for compress system, excepting ids >= -1 if (mapLayer.LayerType == AutoTileMap.eLayerType.FogOfWar) { iType = ((iType >> 1) & 0x7FFFFFFF); // remove the last bit of the last byte. Will be << 1 later when loading } //--- if( iTileRepetition == 0 ) { savedTileId = iType; iTileRepetition = 1; } else { // compression data. All tiles of the same type are store with number of repetitions ( negative number ) and type // ex: 5|5|5|5 --> |-4|5| (4 times 5) ex: -1|-1|-1 --> |-3|-1| ( 3 times -1 ) if( iType == savedTileId ) ++iTileRepetition; else { if( iTileRepetition > 1 ) { tileData.Add( -iTileRepetition ); // save number of repetition with negative sign } if( savedTileId < -1 ) { Debug.LogError(" Wrong tile id found when compressing the tile layer " + mapLayer.Name); savedTileId = -1; } tileData.Add( savedTileId ); savedTileId = iType; iTileRepetition = 1; } } } } // save last tile type found if( iTileRepetition > 1 ) { tileData.Add( -iTileRepetition ); } tileData.Add( savedTileId ); // TileData.Add(new TileLayer() { Tiles = tileData, Depth = mapLayer.Depth, LayerType = mapLayer.LayerType, SortingLayer = mapLayer.SortingLayer, SortingOrder = mapLayer.SortingOrder, Name = mapLayer.Name, Visible = mapLayer.Visible }); } TileMapWidth = width; TileMapHeight = height; return true; }