Esempio n. 1
0
 public void UpdateLayersData(bool isPlayMode)
 {
     for (int i = 0; i < TileChunkLayers.Count; ++i)
     {
         AutoTileMap.MapLayer mapLayer       = m_autoTileMap.MapLayers[i];
         TileChunkLayer       tileChunkLayer = TileChunkLayers[i];
         tileChunkLayer.ObjNode.SetActive(mapLayer.Visible);
         if (isPlayMode)
         {
             tileChunkLayer.ObjNode.transform.localPosition = Vector3.zero;
         }
         else
         {
             if (mapLayer.TileLayerIdx == 0)
             {
                 tileChunkLayer.ObjNode.transform.localPosition = Vector3.zero;
             }
             else
             {
                 tileChunkLayer.ObjNode.transform.localPosition = Vector3.zero + new Vector3(0f, 0f, -5 - mapLayer.TileLayerIdx);
             }
             tileChunkLayer.SortingLayer = mapLayer.SortingLayer;
             tileChunkLayer.SortingOrder = mapLayer.SortingOrder;
         }
     }
 }
Esempio n. 2
0
 public void UpdateLayersData( )
 {
     for (int i = 0; i < TileChunkLayers.Count; ++i)
     {
         AutoTileMap.MapLayer mapLayer       = m_autoTileMap.MapLayers[i];
         TileChunkLayer       tileChunkLayer = TileChunkLayers[i];
         tileChunkLayer.ObjNode.SetActive(mapLayer.Visible);
         tileChunkLayer.ObjNode.transform.localPosition = Vector3.zero + new Vector3(0f, 0f, mapLayer.Depth);
         tileChunkLayer.SortingLayer = mapLayer.SortingLayer;
         tileChunkLayer.SortingOrder = mapLayer.SortingOrder;
     }
 }
Esempio n. 3
0
        /// <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;

                int mapWidth  = _autoTileMap.MapTileWidth;
                int mapHeight = _autoTileMap.MapTileHeight;
                for (int tile_y = 0; tile_y < height; ++tile_y)
                {
                    for (int tile_x = 0; tile_x < width; ++tile_x)
                    {
                        int iType = -1;
                        if (tile_x < mapWidth && tile_y < mapHeight)
                        {
                            AutoTile autoTile = _autoTileMap.TileLayers[_autoTileMap.MapLayers[iLayer].TileLayerIdx][tile_x + tile_y * mapWidth];
                            iType = autoTile != null? autoTile.Id : -1;
                        }

                        //+++fix: FogOfWar tiles could be < -1, and this is not good for compress system, excepting ids >= -1
                        if (mapLayer.LayerType == 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);
        }
        /// <summary>
        /// Save the map configuration
        /// </summary>
        /// <param name="_autoTileMap"></param>
        /// <returns></returns>
        public bool SaveData(AutoTileMap _autoTileMap)
        {
            int width  = TileMapWidth;
            int 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 = MetadataChunk.k_version;

            TileData.Clear();
            // TriggerLink.Clear();
            // WarpsData.Clear();
            // SignpostsData.Clear();
            // ScriptData.Clear();

            bool isEnableCompression = true;

            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;
                int mapWidth    = _autoTileMap.MapTileWidth;
                int mapHeight   = _autoTileMap.MapTileHeight;
                for (int tile_y = 0; tile_y < height; ++tile_y)
                {
                    for (int tile_x = 0; tile_x < width; ++tile_x)
                    {
                        int iType = -1;
                        if (tile_x < mapWidth && tile_y < mapHeight)
                        {
                            // AutoTile autoTile = _autoTileMap.TileLayers[_autoTileMap.MapLayers[iLayer].TileLayerIdx][tile_x + tile_y * mapWidth];
                            AutoTile autoTile = _autoTileMap.TileLayers[iLayer][tile_x, tile_y];
                            iType = autoTile != null? autoTile.Id : -1;
                        }

                        if (isEnableCompression)
                        {
                            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;
                                }
                            }
                        }
                        else
                        {
                            tileData.Add(iType);   // save number of repetition with negative sign
                        }
                    }
                }
                if (isEnableCompression)
                {
                    // 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;

            //Compression Data
            OverlayLink_C   = CreateCompressionArray(OverlayLink);
            TriggerLink_C   = CreateCompressionArray(TriggerLink);
            High_C          = CreateCompressionArray(High);
            OverlayRotate_C = CreateCompressionArray(OverlayRotate);
            // RawFlagMap.Data = FlagMap;
            RawFlagAction = new List <FlagAction.SerializableFlagAction>(ListFlagAction.Count);
            for (int i = 0; i < ListFlagAction.Count; i++)
            {
                var r = new FlagAction.SerializableFlagAction();
                r.FlagAction = ListFlagAction[i];
                RawFlagAction.Add(r);
            }
            Debug.Log("Save with Compression");
            return(true);
        }