/// <summary>
        /// Creates a Map Object Layer from node
        /// </summary>
        /// <param name="node">XML node to parse</param>
        /// <param name="tiledMap">MapObjectLayer parent Map</param>
        /// <param name="layerDepth">This Layer's zDepth</param>
        /// <param name="materials">List of Materials containing the TileSet textures</param>
        public MapObjectLayer(NanoXMLNode node, Map tiledMap, int layerDepth)
            : base(node, tiledMap)
        {
            if (node.GetAttribute("color") != null)
            {
                // get the color string, removing the leading #
                string color = node.GetAttribute("color").Value.Substring(1);

                // get the RGB individually
                string r = color.Substring(0, 2);
                string g = color.Substring(2, 2);
                string b = color.Substring(4, 2);

                // convert to the color
                Color = new Color(
                    (byte)int.Parse(r, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(g, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(b, NumberStyles.AllowHexSpecifier));
            }

            Objects = new List<MapObject>();

            foreach (NanoXMLNode objectNode in node.SubNodes)
            {
                if (!objectNode.Name.Equals("object"))
                    continue;

                MapObject mapObjectContent = new MapObject(objectNode, this);

                mapObjectContent = mapObjectContent.ScaleObject(tiledMap.MapRenderParameter) as MapObject;

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

                // if a object already has the same name...
                if (Objects.Find(o => o.Name.Equals(objectName)) != null)
                {
                    // figure out a object name that does work
                    do
                    {
                        objectName = string.Format("{0}{1}", mapObjectContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Objects.Find(o => o.Name.Equals(objectName)) != null);

                    // log a warning for the user to see
                    //Debug.LogWarning("Renaming object \"" + mapObjectContent.Name + "\" to \"" + objectName + "\" in layer \"" + Name + "\" to make a unique name.");

                    // save that name
                    mapObjectContent.Name = objectName;
                }
                //mapObjectContent.CreateTileObject(tiledMap, Name, layerDepth, materials);

                AddObject(mapObjectContent);
            }
        }
        /// <summary>
        /// Creates a map object layer from .tmx
        /// </summary>
        /// <param name="node"></param>
        public MapObjectLayer(XmlNode node, int TileWidth, int TileHeight)
            : base(node)
        {
            if (node.Attributes["color"] != null)
            {
                // get the color string, removing the leading #
                string color = node.Attributes["color"].Value.Substring(1);

                // get the RGB individually
                string r = color.Substring(0, 2);
                string g = color.Substring(2, 2);
                string b = color.Substring(4, 2);

                // convert to the color
                Color = new Color(
                    (byte)int.Parse(r, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(g, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(b, NumberStyles.AllowHexSpecifier));
            }

            Objects = new List<MapObject>();

            foreach (XmlNode objectNode in node.SelectNodes("object"))
            {
                MapObject mapObjectContent = new MapObject(objectNode);
                mapObjectContent.ScaleObject(TileWidth, TileHeight);
                mapObjectContent.Name = this.Name + "_" + mapObjectContent.Name;
                // Object names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string objectName = mapObjectContent.Name;
                int duplicateCount = 2;

                // if a object already has the same name...
                if (Objects.Find(o => o.Name.Equals(objectName)) != null)
                {
                    // figure out a object name that does work
                    do
                    {
                        objectName = string.Format("{0}{1}", mapObjectContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Objects.Find(o => o.Name.Equals(objectName)) != null);

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

                    // save that name
                    mapObjectContent.Name = objectName;
                }

                //Objects.Add(mapObjectContent);
                AddObject(mapObjectContent);
            }
        }
        /// <summary>
        /// Creates a map object layer from .tmx
        /// </summary>
        /// <param name="node"></param>
        public MapObjectLayer(XmlNode node, int TileWidth, int TileHeight)
            : base(node)
        {
            if (node.Attributes["color"] != null)
            {
                // get the color string, removing the leading #
                string color = node.Attributes["color"].Value.Substring(1);

                // get the RGB individually
                string r = color.Substring(0, 2);
                string g = color.Substring(2, 2);
                string b = color.Substring(4, 2);

                // convert to the color
                Color = new Color(
                    (byte)int.Parse(r, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(g, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(b, NumberStyles.AllowHexSpecifier));
            }

            Objects = new List <MapObject>();

            foreach (XmlNode objectNode in node.SelectNodes("object"))
            {
                MapObject mapObjectContent = new MapObject(objectNode);
                mapObjectContent.ScaleObject(TileWidth, TileHeight);
                mapObjectContent.Name = this.Name + "_" + mapObjectContent.Name;
                // Object names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string objectName     = mapObjectContent.Name;
                int    duplicateCount = 2;

                // if a object already has the same name...
                if (Objects.Find(o => o.Name.Equals(objectName)) != null)
                {
                    // figure out a object name that does work
                    do
                    {
                        objectName = string.Format("{0}{1}", mapObjectContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Objects.Find(o => o.Name.Equals(objectName)) != null);

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

                    // save that name
                    mapObjectContent.Name = objectName;
                }

                //Objects.Add(mapObjectContent);
                AddObject(mapObjectContent);
            }
        }
Example #4
0
        /// <summary>
        /// Creates a map object layer from .tmx
        /// </summary>
        /// <param name="node"></param>
        public MapObjectLayer(XElement node, Map tiledMap, int layerDepth, List<Material> materials)
            : base(node)
        {
            if (node.Attribute("color") != null)
            {
                // get the color string, removing the leading #
                string color = node.Attribute("color").Value.Substring(1);

                // get the RGB individually
                string r = color.Substring(0, 2);
                string g = color.Substring(2, 2);
                string b = color.Substring(4, 2);

                // convert to the color
                Color = new Color(
                    (byte)int.Parse(r, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(g, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(b, NumberStyles.AllowHexSpecifier));
            }

            Objects = new List<MapObject>();

            foreach (XElement objectNode in node.Descendants("object"))
            {
                MapObject mapObjectContent = new MapObject(objectNode);
                //if (tiledMap.Orientation == Orientation.Orthogonal)
                //{
                //	mapObjectContent.ScaleObject(tiledMap.TileWidth, tiledMap.TileHeight);
                //}
                //// In Isometric maps, we must consider tile width == height for objects so their size can be correctly calculated
                //else if (tiledMap.Orientation == Orientation.Isometric)
                //{
                //	mapObjectContent.ScaleObject(tiledMap.TileHeight, tiledMap.TileHeight);
                //}
                //// In Staggered maps, we must pre-alter object position, as it comes mixed between staggered and orthogonal properties
                //else if (tiledMap.Orientation == Orientation.Staggered)
                //{
                //	float x = mapObjectContent.Bounds.x / (float)tiledMap.TileWidth;
                //	float y = mapObjectContent.Bounds.y / (float)tiledMap.TileHeight * 2.0f;
                //	float width = mapObjectContent.Bounds.width / (float)tiledMap.TileWidth;
                //	float height = mapObjectContent.Bounds.height / (float)tiledMap.TileWidth;

                //	if (Mathf.FloorToInt(Mathf.Abs(y)) % 2 > 0)
                //		x -= 0.5f;

                //	mapObjectContent.Bounds = new Rect(x, y, width, height);

                //	if (mapObjectContent.Points != null)
                //	{
                //		for (int i = 0; i < mapObjectContent.Points.Count; i++)
                //		{
                //			mapObjectContent.Points[i] = new Vector2(mapObjectContent.Points[i].x / (float)tiledMap.TileWidth, mapObjectContent.Points[i].y / (float)tiledMap.TileHeight * 2.0f);
                //		}
                //	}
                //}
                mapObjectContent.ScaleObject(tiledMap.TileWidth, tiledMap.TileHeight, tiledMap.Orientation);
                mapObjectContent.Name = this.Name + "_" + mapObjectContent.Name;
                // Object names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string objectName = mapObjectContent.Name;
                int duplicateCount = 2;

                // if a object already has the same name...
                if (Objects.Find(o => o.Name.Equals(objectName)) != null)
                {
                    // figure out a object name that does work
                    do
                    {
                        objectName = string.Format("{0}{1}", mapObjectContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Objects.Find(o => o.Name.Equals(objectName)) != null);

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

                    // save that name
                    mapObjectContent.Name = objectName;
                }
                mapObjectContent.CreateTileObject(tiledMap, Name, layerDepth, materials);

                AddObject(mapObjectContent);
            }
        }
Example #5
0
        /// <summary>
        /// Creates a map object layer from .tmx
        /// </summary>
        /// <param name="node"></param>
        public MapObjectLayer(XElement node, Map tiledMap, int layerDepth, List <Material> materials)
            : base(node)
        {
            if (node.Attribute("color") != null)
            {
                // get the color string, removing the leading #
                string color = node.Attribute("color").Value.Substring(1);

                // get the RGB individually
                string r = color.Substring(0, 2);
                string g = color.Substring(2, 2);
                string b = color.Substring(4, 2);

                // convert to the color
                Color = new Color(
                    (byte)int.Parse(r, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(g, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(b, NumberStyles.AllowHexSpecifier));
            }

            Objects = new List <MapObject>();

            foreach (XElement objectNode in node.Descendants("object"))
            {
                MapObject mapObjectContent = new MapObject(objectNode);
                //if (tiledMap.Orientation == Orientation.Orthogonal)
                //{
                //	mapObjectContent.ScaleObject(tiledMap.TileWidth, tiledMap.TileHeight);
                //}
                //// In Isometric maps, we must consider tile width == height for objects so their size can be correctly calculated
                //else if (tiledMap.Orientation == Orientation.Isometric)
                //{
                //	mapObjectContent.ScaleObject(tiledMap.TileHeight, tiledMap.TileHeight);
                //}
                //// In Staggered maps, we must pre-alter object position, as it comes mixed between staggered and orthogonal properties
                //else if (tiledMap.Orientation == Orientation.Staggered)
                //{
                //	float x = mapObjectContent.Bounds.x / (float)tiledMap.TileWidth;
                //	float y = mapObjectContent.Bounds.y / (float)tiledMap.TileHeight * 2.0f;
                //	float width = mapObjectContent.Bounds.width / (float)tiledMap.TileWidth;
                //	float height = mapObjectContent.Bounds.height / (float)tiledMap.TileWidth;

                //	if (Mathf.FloorToInt(Mathf.Abs(y)) % 2 > 0)
                //		x -= 0.5f;

                //	mapObjectContent.Bounds = new Rect(x, y, width, height);

                //	if (mapObjectContent.Points != null)
                //	{
                //		for (int i = 0; i < mapObjectContent.Points.Count; i++)
                //		{
                //			mapObjectContent.Points[i] = new Vector2(mapObjectContent.Points[i].x / (float)tiledMap.TileWidth, mapObjectContent.Points[i].y / (float)tiledMap.TileHeight * 2.0f);
                //		}
                //	}
                //}
                mapObjectContent.ScaleObject(tiledMap.TileWidth, tiledMap.TileHeight, tiledMap.Orientation);
                mapObjectContent.Name = this.Name + "_" + mapObjectContent.Name;
                // Object names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string objectName     = mapObjectContent.Name;
                int    duplicateCount = 2;

                // if a object already has the same name...
                if (Objects.Find(o => o.Name.Equals(objectName)) != null)
                {
                    // figure out a object name that does work
                    do
                    {
                        objectName = string.Format("{0}{1}", mapObjectContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Objects.Find(o => o.Name.Equals(objectName)) != null);

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

                    // save that name
                    mapObjectContent.Name = objectName;
                }
                //mapObjectContent.CreateTileObject(tiledMap, Name, layerDepth, materials);

                AddObject(mapObjectContent);
            }

            foreach (MapObject o in Objects)
            {
                if (o.Type == "NoCollider")
                {
                    //Debug.Log (o.Name + " (" + o.MapObjectType + ")");
                }
            }
        }
Example #6
0
        /// <summary>
        /// Creates a Map Object Layer from node
        /// </summary>
        /// <param name="node">XML node to parse</param>
        /// <param name="tiledMap">MapObjectLayer parent Map</param>
        /// <param name="layerDepth">This Layer's zDepth</param>
        /// <param name="materials">List of Materials containing the TileSet textures</param>
        public MapObjectLayer(NanoXMLNode node, Map tiledMap, int layerDepth, List <Material> materials)
            : base(node)
        {
            if (node.GetAttribute("color") != null)
            {
                // get the color string, removing the leading #
                string color = node.GetAttribute("color").Value.Substring(1);

                // get the RGB individually
                string r = color.Substring(0, 2);
                string g = color.Substring(2, 2);
                string b = color.Substring(4, 2);

                // convert to the color
                Color = new Color(
                    (byte)int.Parse(r, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(g, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(b, NumberStyles.AllowHexSpecifier));
            }

            LayerGameObject.transform.parent        = tiledMap.MapObject.transform;
            LayerGameObject.transform.localPosition = new Vector3(0, 0, this.LayerDepth);
            LayerGameObject.isStatic = true;
            LayerGameObject.SetActive(Visible);

            Objects = new List <MapObject>();

            foreach (NanoXMLNode objectNode in node.SubNodes)
            {
                if (!objectNode.Name.Equals("object"))
                {
                    continue;
                }

                MapObject mapObjectContent = new MapObject(objectNode, this);

                mapObjectContent.ScaleObject(tiledMap.TileWidth, tiledMap.TileHeight, tiledMap.Orientation);
                mapObjectContent.Name = this.Name + "_" + mapObjectContent.Name;
                // Object names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string objectName     = mapObjectContent.Name;
                int    duplicateCount = 2;

                // if a object already has the same name...
                if (Objects.Find(o => o.Name.Equals(objectName)) != null)
                {
                    // figure out a object name that does work
                    do
                    {
                        objectName = string.Format("{0}{1}", mapObjectContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Objects.Find(o => o.Name.Equals(objectName)) != null);

                    // log a warning for the user to see
                    //Debug.LogWarning("Renaming object \"" + mapObjectContent.Name + "\" to \"" + objectName + "\" in layer \"" + Name + "\" to make a unique name.");

                    // save that name
                    mapObjectContent.Name = objectName;
                }
                mapObjectContent.CreateTileObject(tiledMap, Name, layerDepth, materials);

                AddObject(mapObjectContent);
            }
        }