Esempio n. 1
0
        /// <summary>
        /// Check to see if you can paint something at a position.
        /// Check to make sure there is nothing on the paintLayer Currently
        /// Check to see if there is anything on the terrainLayer that would obstruct painting
        /// </summary>
        /// <param name="pos">Position of the tile</param>
        /// <param name="paintLayer">The tilemap that you will be painting on.</param>
        /// <param name="terrainLayer">The tilemap that is the terrain you would be painting over</param>
        /// <returns></returns>
        public static bool CheckPaintability(Vector3Int pos, Tilemap paintLayer, Tilemap terrainLayer)
        {
            bool result = true;

            //is there anything at the paintlayer to obstruct us from painting?
            if (TileTools.GetCollider(new Vector3Int(pos.x, pos.y, pos.z), paintLayer) != Tile.ColliderType.None ||
                TileTools.GetCollider(new Vector3Int(pos.x, pos.y, pos.z), terrainLayer) != Tile.ColliderType.None)
            {
                return(false);
            }

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Pass in a tileMap to restore your data to
        /// Along with a Path to a Saved data object and load your data!
        /// MUST have sprites in a SpriteAtlas to work
        /// TODO support raw sprites, but spritesheets must always be in a SpriteAtlas
        /// </summary>
        /// <param name="tileMapGO">GameObject with a TileMap Attached</param>
        /// <param name="myPath">Path to the data object saved with SaveTileMap</param>
        /// <param name="dataFormat">The format the data is stored</param>
        /// <param name="spriteAtlas">The spriteAtlas where your sprites are stored</param>
        public static void LoadTileMap(GameObject tileMapGO, string myPath, DataFormat dataFormat, SpriteAtlas spriteAtlas
                                       )
        {
            var bytes = File.ReadAllBytes(myPath);
            var data  = SerializationUtility.DeserializeValue <WorldTile[, ]>(bytes, dataFormat);

            Tilemap tileMap = tileMapGO.GetComponent <Tilemap>();

            //Loop over the deserialized data, and paint the tilemap
            for (int x = 0; x <= data.GetUpperBound(0); x++)
            {
                for (int y = 0; y <= data.GetUpperBound(1); y++)
                {
                    //Skip if there is no data for a position
                    if (data[x, y] == null)
                    {
                        continue;
                    }

                    ///The tile we're building has no concept of it's position
                    /// We NEED to know it's position for when we call back to SetTile to pain the f****r
                    var myTile = ScriptableObject.CreateInstance <Tile>();

                    //Position where the tile will live in the tilemap
                    var pos = new Vector3Int(x, y, 0);
                    tileMap.SetTileFlags(pos, TileFlags.None);

                    // TODO support non-spriteatlas based sprites
                    // If you don't have a sprite don't error
                    if (data[x, y].SpriteName != null)
                    {
                        myTile.sprite = spriteAtlas.GetSprite(data[x, y].SpriteName);
                    }

                    TileTools.SetTile(pos, tileMap, myTile);
                    TileTools.SetColor(pos, tileMap, data[x, y].Color);
                }
            }

            //Refresh the tilemaps
            tileMap.RefreshAllTiles();
            Debug.Log(String.Format("GameData:LoadMap data name {0}", data.Length));
        }