void Awake()
        {
            //hideFlags = HideFlags.HideInInspector;
            MyAutoTileMap = GetComponent<AutoTileMap>();

            OnRenderObject();
        }
			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;
		}
예제 #4
0
        /// <summary>
        /// Configure this chunk
        /// </summary>
        /// <param name="autoTileMap"></param>
        /// <param name="layer"></param>
        /// <param name="startTileX"></param>
        /// <param name="startTileY"></param>
        /// <param name="tileChunkWidth"></param>
        /// <param name="tileChunkHeight"></param>
        public void Configure(AutoTileMap autoTileMap, int layer, int startTileX, int startTileY, int tileChunkWidth, int tileChunkHeight)
        {
            MyAutoTileMap = autoTileMap;
            TileWidth = tileChunkWidth;
            TileHeight = tileChunkHeight;
            MapLayerIdx = layer;
            StartTileX = startTileX;
            StartTileY = startTileY;

            transform.gameObject.name = "TileChunk"+startTileX+""+startTileY;
            Vector3 vPosition = new Vector3();
            vPosition.x = startTileX * MyAutoTileMap.Tileset.TileWorldWidth;
            vPosition.y = -startTileY * MyAutoTileMap.Tileset.TileWorldHeight;
            transform.localPosition = vPosition;

            MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
            if( meshRenderer == null )
            {
                meshRenderer = transform.gameObject.AddComponent<MeshRenderer>();
            }

            if (autoTileMap.MapLayers[layer].LayerType == AutoTileMap.eLayerType.FogOfWar)
            {
                if( s_fogOfWarMaterial == null)
                {
                    s_fogOfWarMaterial = new Material(Shader.Find("Sprites/Default"));
                    Texture2D fogOfWarTexture = new Texture2D(1, 1);
                    fogOfWarTexture.SetPixel(0, 0, Color.gray);
                    fogOfWarTexture.Apply();
                    s_fogOfWarMaterial.SetTexture(0, fogOfWarTexture);
                }
                meshRenderer.sharedMaterial = s_fogOfWarMaterial;
            }
            else
            {
                meshRenderer.sharedMaterial = MyAutoTileMap.Tileset.AtlasMaterial;
            }

            #if UNITY_5_0 || UNITY_5_1
            meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
            #else
            meshRenderer.castShadows = false;
            #endif
            meshRenderer.receiveShadows = false;

            m_meshFilter = GetComponent<MeshFilter>();
            if (m_meshFilter == null)
            {
                m_meshFilter = transform.gameObject.AddComponent<MeshFilter>();
            }
        }
		// Use this for initialization
		public void Init() 
		{
			m_autoTileMap = GetComponent<AutoTileMap>();

			if( m_autoTileMap != null && m_autoTileMap.IsInitialized )
			{
				m_isInitialized = true;

                m_tileGroupNames = new string[m_autoTileMap.Tileset.SubTilesets.Count];
                for (int i = 0; i < m_autoTileMap.Tileset.SubTilesets.Count; ++i)
                {
                    m_tileGroupNames[i] = m_autoTileMap.Tileset.SubTilesets[i].Name;
                }

				if( m_autoTileMap.ViewCamera == null )
				{
					Debug.LogWarning( "AutoTileMap has no ViewCamera set. Camera.main will be set as ViewCamera" );
					m_autoTileMap.ViewCamera = Camera.main;
				}
				m_camera2D = m_autoTileMap.ViewCamera.GetComponent<Camera2DController>();

				if( m_camera2D == null )
				{
					m_camera2D = m_autoTileMap.ViewCamera.gameObject.AddComponent<Camera2DController>();
				}
				
				m_camera2DFollowBehaviour = m_camera2D.transform.GetComponent<FollowObjectBehaviour>();

				// Generate thumbnail textures
				m_thumbnailTextures = new Texture2D[ m_autoTileMap.Tileset.SubTilesets.Count ];
                for (int i = 0; i < m_thumbnailTextures.Length; ++i )
                {
                    m_thumbnailTextures[i] = UtilsAutoTileMap.GenerateTilesetTexture(m_autoTileMap.Tileset, m_autoTileMap.Tileset.SubTilesets[i]);               
                }

				#region Collision Layer
				m_spriteCollLayer = new GameObject();
				m_spriteCollLayer.name = "CollisionLayer";
				m_spriteCollLayer.transform.parent = transform;
				SpriteRenderer sprRender = m_spriteCollLayer.AddComponent<SpriteRenderer>();
				sprRender.sortingOrder = 50; //TODO: +50 temporal? see for a const number later
				_GenerateCollisionTexture();
				#endregion

			}
		}
예제 #6
0
 public void Initialize(AutoTileMap autoTileMap)
 {
     hideFlags = HideFlags.NotEditable;
     m_autoTileMap = autoTileMap;
     foreach( TileChunkLayer tileChunkLayer in TileChunkLayers )
     {
         if( tileChunkLayer.ObjNode != null )
         {
         #if UNITY_EDITOR
             DestroyImmediate(tileChunkLayer.ObjNode);
         #else
             Destroy(tileChunkLayer.ObjNode);
         #endif
         }
     }
     TileChunkLayers.Clear();
     m_tileChunkToBeUpdated.Clear();
 }
        void OnRenderObject()
        {
            if( MyAutoTileMap == null )
            {
                MyAutoTileMap = GetComponent<AutoTileMap>();
            }

            if( MyAutoTileMap != null )
            {
                if( !MyAutoTileMap.IsInitialized )
                {
                    if( MyAutoTileMap.CanBeInitialized() )
                    {
                        MyAutoTileMap.LoadMap();
                    }
                }
            }
        }
			public void Push( AutoTileMap _autoTileMap, int tile_x, int tile_y, int tile_type, int tile_layer )
			{
				if( tile_type <= -2 )
				{
					; // do nothing, skip tile
				}
				else
				{
					if( tile_type >= 0 && _autoTileMap.Tileset.AutotileCollType[ tile_type ] == AutoTileMap.eTileCollisionType.OVERLAY )
					{
						tile_layer = (int)AutoTileMap.eTileLayer.OVERLAY;
					}
					
					TileData tileData = new TileData()
					{  
						Tile_x = tile_x,
						Tile_y = tile_y,
						Tile_type = tile_type,
						Tile_layer = tile_layer,
					};
					aTileData.Add( tileData );
				}
			}
		public TilesetComponent( AutoTileMap _autoTileMap )
		{
			m_autoTileMap = _autoTileMap;
		}
예제 #10
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;
        }
			public void CopyRelative( AutoTileMap _autoTileMap, TileAction _action, int tile_x, int tile_y )
			{
				foreach( TileData tileData in _action.aTileData )
				{
					Push( _autoTileMap, tileData.Tile_x + tile_x, tileData.Tile_y + tile_y, tileData.Tile_type, tileData.Tile_layer );
				}
			}
예제 #12
0
 public void Push( AutoTileMap _autoTileMap, int tile_x, int tile_y, int tileId, int tile_layer )
 {
     if(
         tileId <= -2 || tile_layer >= _autoTileMap.MapLayers.Count ||  !_autoTileMap.MapLayers[tile_layer].Visible ||
         _autoTileMap.MapLayers[tile_layer].LayerType == AutoTileMap.eLayerType.FogOfWar ||
         _autoTileMap.MapLayers[tile_layer].LayerType == AutoTileMap.eLayerType.Objects
     )
     {
         ; // do nothing, skip tile
     }
     else
     {
         TileData tileData = new TileData()
         {
             Tile_x = tile_x,
             Tile_y = tile_y,
             Tile_type = tileId,
             Tile_layer = tile_layer,
         };
         aTileData.Add( tileData );
     }
 }
예제 #13
0
 private void _applyFixVersionBelow124(AutoTileMap _autoTileMap)
 {
     for (int iLayer = 0; iLayer < TileData.Count; ++iLayer)
     {
         TileLayer tileData = TileData[iLayer];
         tileData.Visible = true;
         switch (iLayer)
         {
             case 0:
                 tileData.Name = "Ground";
                 tileData.LayerType = eLayerType.Ground;
                 tileData.Depth = 1f;
                 break;
             case 1:
                 tileData.Name = "Ground Overlay";
                 tileData.LayerType = eLayerType.Ground;
                 tileData.Depth = 0.5f;
                 break;
             case 2:
                 tileData.Name = "Overlay";
                 tileData.LayerType = eLayerType.Overlay;
                 tileData.Depth = -1f;
                 break;
         }
     }
 }
 //float now;
 float GetDistToTarget( AutoTileMap.AutoTile targetTile )
 {
     Vector3 target = RpgMapHelper.GetTileCenterPosition(targetTile.TileX, targetTile.TileY);
     target.z = transform.position.z;
     return (transform.position - target).magnitude;
 }
예제 #15
0
        /// <summary>
        /// Load map serialized data into a map
        /// </summary>
        /// <param name="_autoTileMap"></param>
        public IEnumerator LoadToMap(AutoTileMap _autoTileMap)
        {
            _autoTileMap.Initialize();
            _autoTileMap.ClearMap();

            if( !Metadata.IsVersionAboveOrEqual("1.2.4") )
            {
                _applyFixVersionBelow124(_autoTileMap);
            }

            int totalMapTiles = TileMapWidth * TileMapHeight;
            for( int iLayer = 0; iLayer < TileData.Count; ++iLayer )
            {
                TileLayer tileData = TileData[iLayer];
                _autoTileMap.MapLayers.Add(
                    new AutoTileMap.MapLayer()
                    {
                        Name = tileData.Name,
                        Visible = tileData.Visible,
                        LayerType = tileData.LayerType,
                        SortingOrder = tileData.SortingOrder,
                        SortingLayer = tileData.SortingLayer,
                        Depth = tileData.Depth,
                        TileLayerIdx = iLayer,
                    });
                _autoTileMap.TileLayers.Add( new AutoTile[TileMapWidth * TileMapHeight] );
                int iTileRepetition = 1;
                int iTileIdx = 0;
                for (int i = 0; i < tileData.Tiles.Count; ++i )
                {
                    int iType = tileData.Tiles[i];
                    //see compression notes in CreateFromTilemap
                    if (iType < -1)
                    {
                        iTileRepetition = -iType;
                    }
                    else
                    {
                        //+++fix: FogOfWar tiles could be < -1, and this is not good for compress system, excepting ids >= -1
                        if (tileData.LayerType == eLayerType.FogOfWar)
                        {
                            iType = (iType << 1); //restore value so the lost bit was the less significant of last byte
                        }
                        //---
                        if (iTileRepetition > totalMapTiles)
                        {
                            Debug.LogError("Error uncompressing layer " + tileData.Name + ". The repetition of a tile was higher than map tiles " + iTileRepetition + " > " + totalMapTiles);
                            iTileRepetition = 0;
                        }
                        for (; iTileRepetition > 0; --iTileRepetition, ++iTileIdx)
                        {
                            if (iTileIdx % 10000 == 0)
                            {
                                //float loadingPercent = ((float)(iTileIdx + iLayer * TileMapWidth * TileMapHeight)) / (TileMapWidth * TileMapHeight * TileData.Count);
                                //Debug.Log(" Loading " + (int)(loadingPercent * 100) + "%");
                                yield return null;
                            }
                            int tile_x = iTileIdx % TileMapWidth;
                            int tile_y = iTileIdx / TileMapWidth;
                            if (iType >= 0 || tileData.LayerType == eLayerType.FogOfWar)
                            {
                                _autoTileMap.SetAutoTile(tile_x, tile_y, iType, iLayer, false);
                            }
                        }
                        iTileRepetition = 1;
                    }
                }
            }
            _autoTileMap.RefreshAllTiles();
            _autoTileMap.UpdateChunkLayersData();
            _autoTileMap.RefreshMinimapTexture();
        }
예제 #16
0
        /// <summary>
        /// Configure this chunk
        /// </summary>
        /// <param name="autoTileMap"></param>
        /// <param name="layer"></param>
        /// <param name="startTileX"></param>
        /// <param name="startTileY"></param>
        /// <param name="tileChunkWidth"></param>
        /// <param name="tileChunkHeight"></param>
		public void Configure (AutoTileMap autoTileMap, int layer, int startTileX, int startTileY, int tileChunkWidth, int tileChunkHeight)
		{
			MyAutoTileMap = autoTileMap;
			TileWidth = tileChunkWidth;
			TileHeight = tileChunkHeight;
			TileLayer = layer;
			StartTileX = startTileX;
			StartTileY = startTileY;

			transform.gameObject.name = "TileChunk"+startTileX+""+startTileY;
			Vector3 vPosition = new Vector3();
            vPosition.x = startTileX * MyAutoTileMap.Tileset.TileWorldWidth;
            vPosition.y = -startTileY * MyAutoTileMap.Tileset.TileWorldHeight;
			transform.localPosition = vPosition;

			MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
			if( meshRenderer == null )
			{
				meshRenderer = transform.gameObject.AddComponent<MeshRenderer>();
			}
			meshRenderer.sharedMaterial = MyAutoTileMap.Tileset.AtlasMaterial;
#if UNITY_5_0
            meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
#else
			meshRenderer.castShadows = false;
#endif
			meshRenderer.receiveShadows = false;

            m_meshFilter = GetComponent<MeshFilter>();
            if (m_meshFilter == null)
            {
                m_meshFilter = transform.gameObject.AddComponent<MeshFilter>();
            }
		}
        /// <summary>
        /// Load map serialized data into a map
        /// </summary>
        /// <param name="_autoTileMap"></param>
		public void LoadToMap( AutoTileMap _autoTileMap )
		{
			_autoTileMap.Initialize();
			_autoTileMap.ClearMap();
			for( int iLayer = 0; iLayer < TileData.Count; ++iLayer )
			{
				int iTileRepetition = 1;
				int iTileIdx = 0;
				foreach( int iType in TileData[iLayer].Tiles )
				{
					//see compression notes in CreateFromTilemap
					if( iType < -1 )
					{
						iTileRepetition = -iType;
					}
					else
					{
						for( ;iTileRepetition > 0; --iTileRepetition, ++iTileIdx )
						{
							int tile_x = iTileIdx % TileMapWidth;
							int tile_y = iTileIdx / TileMapWidth;
							if( iType >= 0 )
							{
								_autoTileMap.SetAutoTile( tile_x, tile_y, iType, iLayer, false);
							}
						}
						iTileRepetition = 1;
					}
				}
			}
            _autoTileMap.RefreshAllTiles();
			_autoTileMap.RefreshMinimapTexture();
		}
예제 #18
0
 void OnMapLoaded(AutoTileMap autoTileMap)
 {
     m_OverlayLayerZ = autoTileMap.FindFirstLayer(eLayerType.Overlay).Depth;
     m_GroundOverlayLayerZ = autoTileMap.FindLastLayer(eLayerType.Ground).Depth;
 }
예제 #19
0
        // Use this for initialization
        public void Init()
        {
            m_autoTileMap = GetComponent<AutoTileMap>();

            if( m_autoTileMap != null && m_autoTileMap.IsInitialized )
            {
                m_isInitialized = true;

                m_tileGroupNames = new string[m_autoTileMap.Tileset.SubTilesets.Count];
                for (int i = 0; i < m_autoTileMap.Tileset.SubTilesets.Count; ++i)
                {
                    m_tileGroupNames[i] = m_autoTileMap.Tileset.SubTilesets[i].Name;
                }

                if( m_autoTileMap.ViewCamera == null )
                {
                    Debug.LogWarning( "AutoTileMap has no ViewCamera set. Camera.main will be set as ViewCamera" );
                    m_autoTileMap.ViewCamera = Camera.main;
                }
                m_camera2D = m_autoTileMap.ViewCamera.GetComponent<Camera2DController>();

                if( m_camera2D == null )
                {
                    m_camera2D = m_autoTileMap.ViewCamera.gameObject.AddComponent<Camera2DController>();
                }

                m_camera2DFollowBehaviour = m_camera2D.transform.GetComponent<FollowObjectBehaviour>();

                // Generate thumbnail textures
                m_thumbnailTextures = new Texture2D[ m_autoTileMap.Tileset.SubTilesets.Count ];
                for (int i = 0; i < m_thumbnailTextures.Length; ++i )
                {
                    m_thumbnailTextures[i] = UtilsAutoTileMap.GenerateTilesetTexture(m_autoTileMap.Tileset, m_autoTileMap.Tileset.SubTilesets[i]);
                }

                #region Collision Layer
                m_spriteCollLayer = new GameObject();
                m_spriteCollLayer.name = "CollisionLayer";
                m_spriteCollLayer.transform.parent = transform;
                SpriteRenderer sprRender = m_spriteCollLayer.AddComponent<SpriteRenderer>();
                sprRender.sortingOrder = 50; //TODO: +50 temporal? see for a const number later
                _GenerateCollisionTexture();
                #endregion

                #region Layers Combobox
                string[] toolBarNames = m_autoTileMap.MapLayers.Select( x => x.Name ).ToArray();
                comboBoxList = new GUIContent[toolBarNames.Length];
                for (int i = 0; i < toolBarNames.Length; ++i)
                {
                    comboBoxList[i] = new GUIContent("Layer: " + toolBarNames[i]);
                }

                listStyle.normal.textColor = Color.white;
                listStyle.onHover.background =
                listStyle.hover.background = new Texture2D(2, 2);
                listStyle.padding.left =
                listStyle.padding.right =
                listStyle.padding.top =
                listStyle.padding.bottom = 4;

                comboBoxControl = new ComboBox(new Rect(0, 0, 150, 20), comboBoxList[0], comboBoxList, "button", "box", listStyle);
                comboBoxControl.SelectedItemIndex = m_autoTileMap.BrushGizmo.SelectedLayer;
                #endregion

            }
        }