示例#1
0
        /// <summary>
        /// Creates a new Tile object.
        /// </summary>
        /// <param name="texture">The texture that contains the tile image.</param>
        /// <param name="source">The source rectangle of the tile.</param>
        /// <param name="properties">The initial property collection or null to create an empty property collection.</param>
        public Tile(TileSet tileSet, Rect source, int GID, PropertyCollection properties)
        {
            if (tileSet == null)
                throw new ArgumentNullException("tileSet");

            this.GID = GID;
            TileSet = tileSet;
            Source = source;
            Properties = properties ?? new PropertyCollection();
            Color = Color.white;
            //SpriteEffects = SpriteEffects.None;
        }
示例#2
0
        /// <summary>
        /// Creates a new Tile object.
        /// </summary>
        /// <param name="texture">The texture that contains the tile image.</param>
        /// <param name="source">The source rectangle of the tile.</param>
        /// <param name="properties">The initial property collection or null to create an empty property collection.</param>
        public Tile(TileSet tileSet, Rect source, int GID, PropertyCollection properties)
        {
            if (tileSet == null)
                throw new ArgumentNullException("tileSet");

            this.GID = GID;
            TileSet = tileSet;
            Source = source;
            Properties = properties ?? new PropertyCollection();
            Color = Color.white;
            //SpriteEffects = SpriteEffects.None;
            // Create Sprite
            TileSprite = Sprite.Create(TileSet.Texture, Source, Vector2.zero, TileSet.TileWidth, (uint)(TileSet.Spacing * 2));
            TileSprite.name = GID.ToString();
        }
示例#3
0
        /// <summary>
        /// Creates a new Tile object.
        /// </summary>
        /// <param name="tileSet">The TileSet that contains the tile image.</param>
        /// <param name="source">The source rectangle of the tile.</param>
        /// <param name="OriginalID">This Tile's ID</param>
        /// <param name="properties">The initial property collection or null to create an empty property collection.</param>
        /// <param name="pivot">The Tile's Sprite Pivot Point</param>
        /// <param name="mapTileWidth">The Map's TileWidth this tile is inside, used to calculate sprite's pixelsToUnits</param>
        public Tile(TileSet tileSet, Rect source, int OriginalID, PropertyCollection properties, Vector2 pivot, int mapTileWidth = 0)
        {
            if (tileSet == null)
                throw new ArgumentNullException("tileSet");

            this.OriginalID = OriginalID;
            CurrentID = OriginalID;
            TileSet = tileSet;
            Source = source;
            Properties = properties ?? new PropertyCollection();
            Color = Color.white;
            SpriteEffects = new X_UniTMX.SpriteEffects();

            MapTileWidth = mapTileWidth;
            if (mapTileWidth <= 0)
                MapTileWidth = TileSet.TileWidth;
            CreateSprite(pivot);
        }
示例#4
0
 /// <summary>
 /// Creates a new Tile without creating the Sprite
 /// </summary>
 /// <param name="tileSet">The TileSet that contains the tile image.</param>
 /// <param name="source">The source rectangle of the tile.</param>
 /// <param name="OriginalID">This Tile's ID</param>
 /// <param name="properties">The initial property collection or null to create an empty property collection.</param>
 internal Tile(TileSet tileSet, Rect source, int OriginalID, PropertyCollection properties, int mapTileWidth = 0)
 {
     this.OriginalID = OriginalID;
     CurrentID = OriginalID;
     TileSet = tileSet;
     Source = source;
     Properties = properties ?? new PropertyCollection();
     Color = Color.white;
     SpriteEffects = new X_UniTMX.SpriteEffects();
     if (mapTileWidth <= 0)
         MapTileWidth = TileSet.TileWidth;
 }
示例#5
0
 /// <summary>
 /// Creates a new Tile object.
 /// </summary>
 /// <param name="tileSet">The TileSet that contains the tile image.</param>
 /// <param name="source">The source rectangle of the tile.</param>
 /// <param name="OriginalID">This Tile's ID</param>
 public Tile(TileSet tileSet, Rect source, int OriginalID)
     : this(tileSet, source, OriginalID, new PropertyCollection(), Vector2.zero)
 {
 }
示例#6
0
        Vector3 GetTileWorldPosition(int tileX, int tileY, TileSet tileSet)
        {
            Vector3 pos = Vector3.zero;
            // Set Tile's position according to map orientation
            // Can't use Map.TiledPositionToWorldPoint as sprites' anchors doesn't follow tile anchor point
            if (BaseMap.Orientation == Orientation.Orthogonal)
            {
                //pos = new Vector3(
                //	tileX * (BaseMap.TileWidth / (float)tileSet.TileWidth),
                //	(-tileY - 1) * (BaseMap.TileHeight / (float)tileSet.TileHeight) * ((float)tileSet.TileHeight / (float)tileSet.TileWidth),
                //	0);
                float ratio = tileSet.TileHeight / (float)tileSet.TileWidth;
                float mapRatio = BaseMap.TileHeight / (float)BaseMap.TileWidth;
                pos = new Vector3(tileX, 1, 0);
                if(ratio != 1)
                    pos.y = (-tileY - 1) * (BaseMap.TileHeight / (float)tileSet.TileHeight) * ratio;
                else
                    pos.y = (-tileY - 1) * mapRatio;

            }
            else if (BaseMap.Orientation == Orientation.Isometric)
            {
                pos = new Vector3(
                    (BaseMap.TileWidth / 2.0f * (BaseMap.Width - tileY + tileX) - tileSet.TileWidth / 2.0f) / (float)BaseMap.TileWidth,
                    -BaseMap.Height + BaseMap.TileHeight * (BaseMap.Height - ((tileX + tileY) / (BaseMap.TileWidth / (float)BaseMap.TileHeight)) / 2.0f) / (float)BaseMap.TileHeight - (BaseMap.TileHeight / (float)BaseMap.TileWidth),
                    0);
            }
            else if (BaseMap.Orientation == Orientation.Staggered)
            {
                // In Staggered maps, odd rows and even rows are handled differently
                if (tileY % 2 < 1)
                {
                    // Even row
                    pos.x = tileX * (BaseMap.TileWidth / (float)tileSet.TileWidth);
                    pos.y = (-tileY - 2) * (BaseMap.TileHeight / 2.0f / (float)tileSet.TileHeight) * ((float)tileSet.TileHeight / (float)tileSet.TileWidth);
                }
                else
                {
                    // Odd row
                    pos.x = tileX * (BaseMap.TileWidth / (float)tileSet.TileWidth) + (BaseMap.TileWidth / (float)tileSet.TileWidth) / 2.0f;
                    pos.y = (-tileY - 2) * (BaseMap.TileHeight / 2.0f / (float)tileSet.TileHeight) * ((float)tileSet.TileHeight / (float)tileSet.TileWidth);
                }
            }

            // Add TileSet Tile Offset
            pos.x += tileSet.TileOffsetX / (float)BaseMap.TileWidth;
            pos.y += tileSet.TileOffsetY / (float)BaseMap.TileWidth;

            return pos;
        }
示例#7
0
 /// <summary>
 /// Sets a Tile in position x and y to be Tile with ID equals newTileID (a Local Tile ID from tileSet)
 /// This does not works if the layer is not made of Unique Tiles.
 /// </summary>
 /// <param name="x">Tile X index</param>
 /// <param name="y">Tile Y index</param>
 /// <param name="newTileID">Local Tile ID to change existing tile to. If -1 is passed, erase current Tile</param>
 /// <param name="tileSet">TileSet to read newTileID from</param>
 /// <returns>true if newTileID inside tileSet was found and change succeded, false otherwise</returns>
 public bool SetTile(float x, float y, int newTileID, TileSet tileSet)
 {
     return SetTile(Mathf.FloorToInt(x), Mathf.FloorToInt(y), newTileID, tileSet);
 }
示例#8
0
 /// <summary>
 /// Creates a new Tile object.
 /// </summary>
 /// <param name="tileSet">The TileSet that contains the tile image.</param>
 /// <param name="source">The source rectangle of the tile.</param>
 /// <param name="OriginalID">This Tile's ID</param>
 public Tile(TileSet tileSet, Rect source, int OriginalID) : this(tileSet, source, OriginalID, new PropertyCollection(), Vector2.zero)
 {
 }
示例#9
0
        void OnFinishedLoadingTileSet(TileSet tileSet)
        {
            TileSets.Add(tileSet);
            foreach (KeyValuePair<int, Tile> item in tileSet.Tiles)
            {
                this.Tiles.Add(item.Key, item.Value);
            }
            _tileSetsToLoaded++;

            if (_tileSetsToLoaded >= _numberOfTileSetsToLoad)
                ContinueLoadingTiledMapAfterTileSetsLoaded();
        }
示例#10
0
 /// <summary>
 /// Creates a new Tile object.
 /// </summary>
 /// <param name="texture">The texture that contains the tile image.</param>
 /// <param name="source">The source rectangle of the tile.</param>
 public Tile(TileSet tileSet, Rect source, int GID) : this(tileSet, source, GID, new PropertyCollection())
 {
 }
        Vector3 GetTileWorldPosition(int tileX, int tileY, TileSet tileSet)
        {
            Vector3 pos = Vector3.zero;
            MapRenderParameters renderParam = BaseMap.MapRenderParameter;
            // Set Tile's position according to map orientation
            // Can't use Map.TiledPositionToWorldPoint as sprites' anchors doesn't follow tile anchor point
            if (renderParam.Orientation == Orientation.Orthogonal)
            {
                //pos = new Vector3(
                //	tileX * (BaseMap.TileWidth / (float)tileSet.TileWidth),
                //	(-tileY - 1) * (BaseMap.TileHeight / (float)tileSet.TileHeight) * ((float)tileSet.TileHeight / (float)tileSet.TileWidth),
                //	0);
                float ratio = tileSet.TileHeight / (float)tileSet.TileWidth;
                float mapRatio = renderParam.TileHeight / (float)renderParam.TileWidth;
                pos.x = tileX;
                if(ratio != 1)
                    pos.y = (-tileY - 1) * (renderParam.TileHeight / (float)tileSet.TileHeight) * ratio;
                else
                    pos.y = (-tileY - 1) * mapRatio;

            }
            else if (renderParam.Orientation == Orientation.Isometric)
            {
                pos.x = (renderParam.TileWidth / 2.0f * (renderParam.Width - tileY + tileX) - tileSet.TileWidth / 2.0f) / (float)renderParam.TileWidth;
                pos.y = -renderParam.Height + renderParam.TileHeight * (renderParam.Height - ((tileX + tileY) / (renderParam.TileWidth / (float)renderParam.TileHeight)) / 2.0f) / (float)renderParam.TileHeight - (renderParam.TileHeight / (float)renderParam.TileWidth);
            }
            else if (renderParam.Orientation == Orientation.Staggered)
            {
                // In Staggered maps, odd rows and even rows are handled differently
                if (renderParam.MapStaggerAxis.Equals(StaggerAxis.Y))
                {
                    if ((renderParam.MapStaggerIndex.Equals(StaggerIndex.Odd) && tileY % 2 < 1) ||
                        (renderParam.MapStaggerIndex.Equals(StaggerIndex.Even) && tileY % 2 > 0))
                    {
                        // Even row in Odd Staggered Maps
                        // Odd row in Even Staggered Maps
                        pos.x = tileX * (renderParam.TileWidth / (float)tileSet.TileWidth);
                        pos.y = (-tileY - 2) * (renderParam.TileHeight / 2.0f / (float)tileSet.TileHeight) * ((float)tileSet.TileHeight / (float)tileSet.TileWidth);
                    }
                    else
                    {
                        // Odd row in Odd Staggered Maps
                        // Even row in Even Staggered Maps
                        pos.x = tileX * (renderParam.TileWidth / (float)tileSet.TileWidth) + (renderParam.TileWidth / (float)tileSet.TileWidth) / 2.0f;
                        pos.y = (-tileY - 2) * (renderParam.TileHeight / 2.0f / (float)tileSet.TileHeight) * ((float)tileSet.TileHeight / (float)tileSet.TileWidth);
                    }

                }
                else
                {
                    if ((renderParam.MapStaggerIndex.Equals(StaggerIndex.Odd) && tileX % 2 < 1) ||
                        (renderParam.MapStaggerIndex.Equals(StaggerIndex.Even) && tileX % 2 > 0))
                    {
                        // Even row in Odd Staggered Maps
                        // Odd row in Even Staggered Maps
                        pos.x = tileX * (renderParam.TileWidth / 2.0f / (float)tileSet.TileWidth);
                        pos.y = (-tileY - 1) * (renderParam.TileHeight / (float)tileSet.TileHeight) * ((float)tileSet.TileHeight / (float)tileSet.TileWidth);
                    }
                    else
                    {
                        // Odd row in Odd Staggered Maps
                        // Even row in Even Staggered Maps
                        pos.x = tileX * (renderParam.TileWidth / 2.0f / (float)tileSet.TileWidth);
                        pos.y = (-tileY - 1) * (renderParam.TileHeight / (float)tileSet.TileHeight) * ((float)tileSet.TileHeight / (float)tileSet.TileWidth) - (BaseMap.MapRenderParameter.TileHeight / (float)tileSet.TileHeight) / 2.0f * ((float)tileSet.TileHeight / (float)tileSet.TileWidth);
                    }
                }
            }
            else if (renderParam.Orientation == Orientation.Hexagonal)
            {
                // In Staggered maps, odd rows and even rows are handled differently
                if (renderParam.MapStaggerAxis.Equals(StaggerAxis.Y))
                {
                    float halfGap = (renderParam.TileHeight - renderParam.HexSideLength) / 2.0f;
                    float tileDisplacement = halfGap + renderParam.HexSideLength;
                    if ((renderParam.MapStaggerIndex.Equals(StaggerIndex.Odd) && tileY % 2 < 1) ||
                        (renderParam.MapStaggerIndex.Equals(StaggerIndex.Even) && tileY % 2 > 0))
                    {
                        // Even row in Odd Staggered Maps
                        // Odd row in Even Staggered Maps
                        pos.x = tileX * (renderParam.TileWidth / (float)tileSet.TileWidth);
                        pos.y = (-tileY - 1) * (tileDisplacement / (float)tileSet.TileHeight) - halfGap / (float)tileSet.TileHeight;
                    }
                    else
                    {
                        // Odd row in Odd Staggered Maps
                        // Even row in Even Staggered Maps
                        pos.x = tileX * (renderParam.TileWidth / (float)tileSet.TileWidth) + (renderParam.TileWidth / (float)tileSet.TileWidth) / 2.0f;
                        pos.y = (-tileY - 1) * (tileDisplacement / (float)tileSet.TileHeight) - halfGap / (float)tileSet.TileHeight;
                    }

                }
                else
                {
                    float halfGap = (renderParam.TileWidth - renderParam.HexSideLength) / 2.0f;
                    float tileDisplacement = halfGap + renderParam.HexSideLength;
                    if ((renderParam.MapStaggerIndex.Equals(StaggerIndex.Odd) && tileX % 2 < 1) ||
                        (renderParam.MapStaggerIndex.Equals(StaggerIndex.Even) && tileX % 2 > 0))
                    {
                        // Even row in Odd Staggered Maps
                        // Odd row in Even Staggered Maps
                        pos.x = tileX * (tileDisplacement / (float)tileSet.TileWidth);
                        pos.y = (-tileY - 1) * (renderParam.TileHeight / (float)tileSet.TileHeight) * ((float)tileSet.TileHeight / (float)tileSet.TileWidth);
                    }
                    else
                    {
                        // Odd row in Odd Staggered Maps
                        // Even row in Even Staggered Maps
                        pos.x = tileX * (tileDisplacement / (float)tileSet.TileWidth);
                        pos.y = (-tileY - 1) * (renderParam.TileHeight / (float)tileSet.TileHeight) * ((float)tileSet.TileHeight / (float)tileSet.TileWidth) - (BaseMap.MapRenderParameter.TileHeight / (float)tileSet.TileHeight) / 2.0f * ((float)tileSet.TileHeight / (float)tileSet.TileWidth);
                    }
                }
            }

            // Add TileSet Tile Offset
            pos.x += tileSet.TileOffsetX / (float)renderParam.TileWidth;
            pos.y += tileSet.TileOffsetY / (float)renderParam.TileHeight;

            return pos;
        }
示例#12
0
        void OnFinishedLoadingTileSet(TileSet tileSet)
        {
            TileSets.Add(tileSet);

            _tileSetsToLoaded++;

            if (_tileSetsToLoaded >= _numberOfTileSetsToLoad)
                ContinueLoadingTiledMapAfterTileSetsLoaded();
        }
示例#13
0
        private void onFinishedGeneratingTileSet(TileSet tileSet)
        {
            foreach (KeyValuePair<int, Tile> item in tileSet.Tiles)
            {
                this.Tiles.Add(item.Key, item.Value);
            }
            _tileSetsToLoaded++;

            if (_tileSetsToLoaded >= _numberOfTileSetsToLoad)
                GenerateTileSetsMaterials();
        }
示例#14
0
        void Initialize(XmlDocument document, bool makeUnique, string fullPath, string mapPath)
        {
            XmlNode mapNode = document["map"];

            Version     = mapNode.Attributes["version"].Value;
            Orientation = (Orientation)Enum.Parse(typeof(Orientation), mapNode.Attributes["orientation"].Value, true);
            Width       = int.Parse(mapNode.Attributes["width"].Value, CultureInfo.InvariantCulture);
            Height      = int.Parse(mapNode.Attributes["height"].Value, CultureInfo.InvariantCulture);
            TileWidth   = int.Parse(mapNode.Attributes["tilewidth"].Value, CultureInfo.InvariantCulture);
            TileHeight  = int.Parse(mapNode.Attributes["tileheight"].Value, CultureInfo.InvariantCulture);

            XmlNode propertiesNode = document.SelectSingleNode("map/properties");

            if (propertiesNode != null)
            {
                Properties = new PropertyCollection(propertiesNode);                //Property.ReadProperties(propertiesNode);
            }

            TileSets = new List <TileSet>();
            Tiles    = new Dictionary <int, Tile>();
            foreach (XmlNode tileSet in document.SelectNodes("map/tileset"))
            {
                if (tileSet.Attributes["source"] != null)
                {
                    //TileSets.Add(new ExternalTileSetContent(tileSet, context));
                    XmlDocument externalTileSet = new XmlDocument();

                    TextAsset externalTileSetTextAsset = (TextAsset)Resources.Load(mapPath + Path.GetFileNameWithoutExtension(tileSet.Attributes["source"].Value));

                    //externalTileSet.Load(fullPath + "/" + tileSet.Attributes["source"].Value);
                    externalTileSet.LoadXml(externalTileSetTextAsset.text);
                    XmlNode externalTileSetNode = externalTileSet["tileset"];
                    //Debug.Log(externalTileSet.Value);
                    TileSet t = new TileSet(externalTileSetNode, mapPath);
                    TileSets.Add(t);
                    foreach (KeyValuePair <int, Tile> item in t.Tiles)
                    {
                        this.Tiles.Add(item.Key, item.Value);
                    }
                    //this.Tiles.AddRange(t.Tiles);
                }
                else
                {
                    TileSet t = new TileSet(tileSet, mapPath);
                    TileSets.Add(t);
                    foreach (KeyValuePair <int, Tile> item in t.Tiles)
                    {
                        this.Tiles.Add(item.Key, item.Value);
                    }
                }
            }
            // Generate Materials for Map batching
            List <Material> materials = new List <Material>();
            // Generate Materials
            int i = 0;

            for (i = 0; i < TileSets.Count; i++)
            {
                Material layerMat = new Material(Shader.Find("Unlit/Transparent"));
                layerMat.mainTexture = TileSets[i].Texture;
                materials.Add(layerMat);
            }

            Layers = new List <Layer>();
            i      = 0;
            foreach (XmlNode layerNode in document.SelectNodes("map/layer|map/objectgroup"))
            {
                Layer layerContent;

                float layerDepth = 1f - (LayerDepthSpacing * i);

                if (layerNode.Name == "layer")
                {
                    layerContent = new TileLayer(layerNode, this, layerDepth, makeUnique, materials);
                    //((TileLayer)layerContent).GenerateLayerMesh(MeshRendererPrefab);
                }
                else if (layerNode.Name == "objectgroup")
                {
                    layerContent = new MapObjectLayer(layerNode, TileWidth, TileHeight);
                }
                else
                {
                    throw new Exception("Unknown layer name: " + layerNode.Name);
                }

                // Layer names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string layerName      = layerContent.Name;
                int    duplicateCount = 2;

                // if a layer already has the same name...
                if (Layers.Find(l => l.Name == layerName) != null)
                {
                    // figure out a layer name that does work
                    do
                    {
                        layerName = string.Format("{0}{1}", layerContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Layers.Find(l => l.Name == layerName) != null);

                    // log a warning for the user to see
                    Debug.Log("Renaming layer \"" + layerContent.Name + "\" to \"" + layerName + "\" to make a unique name.");

                    // save that name
                    layerContent.Name = layerName;
                }
                layerContent.LayerDepth = layerDepth;
                Layers.Add(layerContent);
                namedLayers.Add(layerName, layerContent);
                i++;
            }
        }
示例#15
0
 /// <summary>
 /// Creates a new Tile object.
 /// </summary>
 /// <param name="texture">The texture that contains the tile image.</param>
 /// <param name="source">The source rectangle of the tile.</param>
 public Tile(TileSet tileSet, Rect source, int GID)
     : this(tileSet, source, GID, new PropertyCollection())
 {
 }
示例#16
0
文件: Map.cs 项目: Tavrox/Lavapools
        void Initialize(XmlDocument document, bool makeUnique, string fullPath, string mapPath)
        {
            XmlNode mapNode = document["map"];
            Version = mapNode.Attributes["version"].Value;
            Orientation = (Orientation)Enum.Parse(typeof(Orientation), mapNode.Attributes["orientation"].Value, true);
            Width = int.Parse(mapNode.Attributes["width"].Value, CultureInfo.InvariantCulture);
            Height = int.Parse(mapNode.Attributes["height"].Value, CultureInfo.InvariantCulture);
            TileWidth = int.Parse(mapNode.Attributes["tilewidth"].Value, CultureInfo.InvariantCulture);
            TileHeight = int.Parse(mapNode.Attributes["tileheight"].Value, CultureInfo.InvariantCulture);

            XmlNode propertiesNode = document.SelectSingleNode("map/properties");
            if (propertiesNode != null)
            {
                Properties = new PropertyCollection(propertiesNode);//Property.ReadProperties(propertiesNode);
            }

            TileSets = new List<TileSet>();
            Tiles = new Dictionary<int, Tile>();
            foreach (XmlNode tileSet in document.SelectNodes("map/tileset"))
            {
                if (tileSet.Attributes["source"] != null)
                {
                    //TileSets.Add(new ExternalTileSetContent(tileSet, context));
                    XmlDocument externalTileSet = new XmlDocument();

                    TextAsset externalTileSetTextAsset = (TextAsset)Resources.Load(mapPath + Path.GetFileNameWithoutExtension(tileSet.Attributes["source"].Value));

                    //externalTileSet.Load(fullPath + "/" + tileSet.Attributes["source"].Value);
                    externalTileSet.LoadXml(externalTileSetTextAsset.text);
                    XmlNode externalTileSetNode = externalTileSet["tileset"];
                    //Debug.Log(externalTileSet.Value);
                    TileSet t = new TileSet(externalTileSetNode, mapPath);
                    TileSets.Add(t);
                    foreach (KeyValuePair<int, Tile> item in t.Tiles)
                    {
                        this.Tiles.Add(item.Key, item.Value);
                    }
                    //this.Tiles.AddRange(t.Tiles);
                }
                else
                {
                    TileSet t = new TileSet(tileSet, mapPath);
                    TileSets.Add(t);
                    foreach (KeyValuePair<int, Tile> item in t.Tiles)
                    {
                        this.Tiles.Add(item.Key, item.Value);
                    }
                }
            }
            // Generate Materials for Map batching
            List<Material> materials = new List<Material>();
            // Generate Materials
            int i = 0;
            for (i = 0; i < TileSets.Count; i++)
            {
                Material layerMat = new Material(Shader.Find("Unlit/Transparent"));
                layerMat.mainTexture = TileSets[i].Texture;
                materials.Add(layerMat);
            }

            Layers = new List<Layer>();
            i = 0;
            foreach (XmlNode layerNode in document.SelectNodes("map/layer|map/objectgroup"))
            {
                Layer layerContent;

                float layerDepth = 1f - (LayerDepthSpacing * i);

                if (layerNode.Name == "layer")
                {
                    layerContent = new TileLayer(layerNode, this, layerDepth, makeUnique, materials);
                    //((TileLayer)layerContent).GenerateLayerMesh(MeshRendererPrefab);
                }
                else if (layerNode.Name == "objectgroup")
                {
                    layerContent = new MapObjectLayer(layerNode, TileWidth, TileHeight);
                }
                else
                {
                    throw new Exception("Unknown layer name: " + layerNode.Name);
                }

                // Layer names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string layerName = layerContent.Name;
                int duplicateCount = 2;

                // if a layer already has the same name...
                if (Layers.Find(l => l.Name == layerName) != null)
                {
                    // figure out a layer name that does work
                    do
                    {
                        layerName = string.Format("{0}{1}", layerContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Layers.Find(l => l.Name == layerName) != null);

                    // log a warning for the user to see
                    Debug.Log("Renaming layer \"" + layerContent.Name + "\" to \"" + layerName + "\" to make a unique name.");

                    // save that name
                    layerContent.Name = layerName;
                }
                layerContent.LayerDepth = layerDepth;
                Layers.Add(layerContent);
                namedLayers.Add(layerName, layerContent);
                i++;
            }
        }
示例#17
0
        /// <summary>
        /// Sets a Tile in position x and y to be Tile with ID equals newTileID (a Local Tile ID from tileSet)
        /// This does not works if the layer is not made of Unique Tiles.
        /// </summary>
        /// <param name="x">Tile X index</param>
        /// <param name="y">Tile Y index</param>
        /// <param name="newTileID">Local Tile ID to change existing tile to. If -1 is passed, erase current Tile</param>
        /// <param name="tileSet">TileSet to read newTileID from</param>
        /// <returns>true if newTileID inside tileSet was found and change succeded, false otherwise</returns>
        public bool SetTile(int x, int y, int newTileID, TileSet tileSet)
        {
            if (x < 0 || x >= Width || y < 0 || y >= Height || !MakeUniqueTiles)
                return false;
            if (newTileID < 0)
            {
                if (Tiles[x, y] != null)
                    GameObject.Destroy(Tiles[x, y].TileGameObject);
                Tiles[x, y] = null;
                return true;
            }
            Tile t = null;
            if (tileSet.Tiles.TryGetValue(newTileID, out t))
            {
                if (Tiles[x, y] != null)
                {
                    Tiles[x, y].TileSprite = t.TileSprite;
                    Tiles[x, y].CurrentID = t.OriginalID;
                    (Tiles[x, y].TileGameObject.GetComponent<Renderer>() as SpriteRenderer).sprite = t.TileSprite;
                }
                else
                {
                    Tile newTile = t.Clone();
                    CreateTileGameObject(newTile, x, y);
                    Tiles[x, y] = newTile;
                }
                return true;
            }

            return false;
        }
示例#18
0
        private void Initialize(XDocument document, bool makeUnique, string fullPath, string mapPath, Material baseTileMaterial, int sortingOrder)
        {
            XElement mapNode = document.Root;
            Version = mapNode.Attribute("version").Value;
            Orientation = (Orientation)Enum.Parse(typeof(Orientation), mapNode.Attribute("orientation").Value, true);
            Width = int.Parse(mapNode.Attribute("width").Value, CultureInfo.InvariantCulture);
            Height = int.Parse(mapNode.Attribute("height").Value, CultureInfo.InvariantCulture);
            TileWidth = int.Parse(mapNode.Attribute("tilewidth").Value, CultureInfo.InvariantCulture);
            TileHeight = int.Parse(mapNode.Attribute("tileheight").Value, CultureInfo.InvariantCulture);

            if (_mapName == null)
                _mapName = "Map";

            if (!mapPath.EndsWith("/"))
                mapPath = mapPath + "/";

            MapObject = new GameObject(_mapName);
            MapObject.transform.parent = Parent.transform;
            MapObject.transform.localPosition = Vector3.zero;

            DefaultSortingOrder = sortingOrder;

            XElement propertiesElement = mapNode.Element("properties");
            if (propertiesElement != null)
                Properties = new PropertyCollection(propertiesElement);

            TileSets = new List<TileSet>();
            Tiles = new Dictionary<int, Tile>();
            foreach (XElement tileSet in mapNode.Descendants("tileset"))
            {
                if (tileSet.Attribute("source") != null)
                {
                    TextAsset externalTileSetTextAsset = (TextAsset)Resources.Load(mapPath + Path.GetFileNameWithoutExtension(tileSet.Attribute("source").Value));

                    XDocument externalTileSet = XDocument.Parse(externalTileSetTextAsset.text);

                    XElement externalTileSetNode = externalTileSet.Element("tileset");

                    TileSet t = new TileSet(externalTileSetNode, mapPath);
                    TileSets.Add(t);
                    foreach (KeyValuePair<int, Tile> item in t.Tiles)
                    {
                        this.Tiles.Add(item.Key, item.Value);
                    }
                }
                else
                {
                    TileSet t = new TileSet(tileSet, mapPath);
                    TileSets.Add(t);
                    foreach (KeyValuePair<int, Tile> item in t.Tiles)
                    {
                        this.Tiles.Add(item.Key, item.Value);
                    }
                }
            }
            // Generate Materials for Map batching
            List<Material> materials = new List<Material>();
            // Generate Materials
            int i = 0;
            for (i = 0; i < TileSets.Count; i++)
            {
                Material layerMat = new Material(baseTileMaterial);
                layerMat.mainTexture = TileSets[i].Texture;
                materials.Add(layerMat);
            }

            Layers = new List<Layer>();
            i = 0;

            foreach (XElement layerNode in
                mapNode.Elements("layer").Concat(
                mapNode.Elements("objectgroup").Concat(
                mapNode.Elements("imagelayer")))
                )
            {
                Layer layerContent;

                int layerDepth = 1 - (LayerDepthSpacing * i);

                if (layerNode.Name == "layer")
                {
                    layerContent = new TileLayer(layerNode, this, layerDepth, makeUnique, materials);
                }
                else if (layerNode.Name == "objectgroup")
                {
                    layerContent = new MapObjectLayer(layerNode, this, layerDepth, materials);
                }
                else if (layerNode.Name == "imagelayer")
                {
                    layerContent = new ImageLayer(layerNode, this, mapPath, baseTileMaterial);
                }
                else
                {
                    throw new Exception("Unknown layer name: " + layerNode.Name);
                }

                // Layer names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string layerName = layerContent.Name;
                int duplicateCount = 2;

                // if a layer already has the same name...
                if (Layers.Find(l => l.Name == layerName) != null)
                {
                    // figure out a layer name that does work
                    do
                    {
                        layerName = string.Format("{0}{1}", layerContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Layers.Find(l => l.Name == layerName) != null);

                    // log a warning for the user to see
                    Debug.Log("Renaming layer \"" + layerContent.Name + "\" to \"" + layerName + "\" to make a unique name.");

                    // save that name
                    layerContent.Name = layerName;
                }
                layerContent.LayerDepth = layerDepth;
                Layers.Add(layerContent);
                namedLayers.Add(layerName, layerContent);
                i++;
            }
        }
示例#19
0
 /// <summary>
 /// Sets a Tile in position x and y to be Tile with ID equals newTileID (a Local Tile ID from tileSet)
 /// This does not works if the layer is not made of Unique Tiles.
 /// </summary>
 /// <param name="x">Tile X index</param>
 /// <param name="y">Tile Y index</param>
 /// <param name="newTileID">Local Tile ID to change existing tile to. If -1 is passed, erase current Tile</param>
 /// <param name="tileSet">TileSet to read newTileID from</param>
 /// <returns>true if newTileID inside tileSet was found and change succeded, false otherwise</returns>
 public bool SetTile(float x, float y, int newTileID, TileSet tileSet)
 {
     return(SetTile(Mathf.FloorToInt(x), Mathf.FloorToInt(y), newTileID, tileSet));
 }