示例#1
0
        public static TmxImage LoadTmxImage(this TmxImage image, XElement xImage, string tmxDir = "")
        {
            var xSource = xImage.Attribute("source");

            if (xSource != null)
            {
                // Append directory if present
                image.Source = Path.Combine(tmxDir, (string)xSource);

                /*using (var stream = TitleContainer.OpenStream(image.Source))
                 *      image.Texture = Texture2D.FromStream(Core.GraphicsDevice, stream);*/

                image.Source  = image.Source.Substring(Core.Content.RootDirectory.Length + 1);
                image.Source  = image.Source.Remove(image.Source.LastIndexOf('.'));
                image.Texture = (Core.Scene?.Content ?? Core.Content).LoadTexture(image.Source);
            }
            else
            {
                image.Format = (string)xImage.Attribute("format");
                var xData         = xImage.Element("data");
                var decodedStream = new TmxBase64Data(xData);
                image.Data = decodedStream.Data;
                throw new NotSupportedException("Stream Data loading is not yet supported");
            }

            image.Trans  = TiledMapLoader.ParseColor(xImage.Attribute("trans"));
            image.Width  = (int?)xImage.Attribute("width") ?? 0;
            image.Height = (int?)xImage.Attribute("height") ?? 0;

            return(image);
        }
示例#2
0
        public static TmxImage LoadTmxImage(this TmxImage image, XElement xImage, bool headless = false, string tmxDir = "")
        {
            var xSource = xImage.Attribute("source");

            if (xSource != null)
            {
                if (!headless)
                {
                    // Append directory if present
                    image.Source = Path.Combine(tmxDir, (string)xSource);
                    using (var stream = TitleContainer.OpenStream(image.Source))
                        image.Texture = Texture2D.FromStream(Core.GraphicsDevice, stream);
                }
            }
            else
            {
                image.Format = (string)xImage.Attribute("format");
                var xData         = xImage.Element("data");
                var decodedStream = new TmxBase64Data(xData);
                image.Data = decodedStream.Data;
                throw new NotSupportedException("Stream Data loading is not yet supported");
            }

            image.Trans  = TiledMapLoader.ParseColor(xImage.Attribute("trans"));
            image.Width  = (int?)xImage.Attribute("width") ?? 0;
            image.Height = (int?)xImage.Attribute("height") ?? 0;

            return(image);
        }
示例#3
0
文件: TiledCore.cs 项目: wacharlb/Nez
        public TmxImage(XElement xImage, string tmxDir = "")
        {
            var xSource = xImage.Attribute("source");

            if (xSource != null)
            {
                // Append directory if present
                Source = Path.Combine(tmxDir, (string)xSource);

                using (var stream = TitleContainer.OpenStream(Source))
                    Texture = Texture2D.FromStream(Core.GraphicsDevice, stream);
            }
            else
            {
                Format = (string)xImage.Attribute("format");
                var xData         = xImage.Element("data");
                var decodedStream = new TmxBase64Data(xData);
                Data = decodedStream.Data;
                throw new NotSupportedException("Stream Data loading is not yet supported");
            }

            Trans  = TmxColor.ParseColor(xImage.Attribute("trans"));
            Width  = (int?)xImage.Attribute("width") ?? 0;
            Height = (int?)xImage.Attribute("height") ?? 0;
        }
示例#4
0
            static void LoadLayerData(TmxLayer layer, TmxMap map, int width, int height, int startX, int startY, XElement xData, string encoding)
            {
                if (encoding == "base64")
                {
                    var decodedStream = new TmxBase64Data(xData);
                    var stream        = decodedStream.Data;

                    var index = 0;
                    using (var br = new BinaryReader(stream))
                    {
                        for (var j = 0; j < height; j++)
                        {
                            index = ((startY + j) * layer.Width) + startX;
                            for (var i = 0; i < width; i++)
                            {
                                var gid = br.ReadUInt32();
                                layer.Tiles[index++] = gid != 0 ? new TmxLayerTile(map, gid, i + startX, j + startY) : null;
                            }
                        }
                    }
                }
                else if (encoding == "csv")
                {
                    var csvData = xData.Value;
                    int k       = 0;
                    foreach (var s in csvData.Split(CSVSplit, StringSplitOptions.RemoveEmptyEntries))
                    {
                        var gid = uint.Parse(s.Trim());
                        var x   = k % width;
                        var y   = k / width;
                        k++;
                        var index = ((startY + y) * layer.Width) + (startX + x);
                        layer.Tiles[index] = gid != 0 ? new TmxLayerTile(map, gid, x + startX, y + startY) : null;
                    }
                }
                else if (encoding == null)
                {
                    int k = 0;
                    foreach (var e in xData.Elements("tile"))
                    {
                        var gid = (uint?)e.Attribute("gid") ?? 0;

                        var x = k % width;
                        var y = k / width;
                        k++;
                        var index = ((startY + y) * layer.Width) + (startX + x);
                        layer.Tiles[index] = gid != 0 ? new TmxLayerTile(map, gid, x + startX, y + startY) : null;
                    }
                }
                else
                {
                    throw new Exception("TmxLayer: Unknown encoding.");
                }
            }
示例#5
0
        public static TmxLayer LoadTmxLayer(this TmxLayer layer, TmxMap map, XElement xLayer, int width, int height)
        {
            layer.Map             = map;
            layer.Name            = (string)xLayer.Attribute("name");
            layer.Opacity         = (float?)xLayer.Attribute("opacity") ?? 1.0f;
            layer.Visible         = (bool?)xLayer.Attribute("visible") ?? true;
            layer.OffsetX         = (float?)xLayer.Attribute("offsetx") ?? 0.0f;
            layer.OffsetY         = (float?)xLayer.Attribute("offsety") ?? 0.0f;
            layer.ParallaxFactorX = (float?)xLayer.Attribute("parallaxx") ?? 1.0f;
            layer.ParallaxFactorY = (float?)xLayer.Attribute("parallaxy") ?? 1.0f;

            // TODO: does the width/height passed in ever differ from the TMX layer XML?
            layer.Width  = (int)xLayer.Attribute("width");
            layer.Height = (int)xLayer.Attribute("height");

            var xData    = xLayer.Element("data");
            var encoding = (string)xData.Attribute("encoding");

            layer.Tiles = new TmxLayerTile[width * height];
            if (encoding == "base64")
            {
                var decodedStream = new TmxBase64Data(xData);
                var stream        = decodedStream.Data;

                var index = 0;
                using (var br = new BinaryReader(stream))
                {
                    for (var j = 0; j < height; j++)
                    {
                        for (var i = 0; i < width; i++)
                        {
                            var gid = br.ReadUInt32();
                            layer.Tiles[index++] = gid != 0 ? new TmxLayerTile(map, gid, i, j) : null;
                        }
                    }
                }
            }
            else if (encoding == "csv")
            {
                var csvData = xData.Value;
                int k       = 0;
                foreach (var s in csvData.Split(','))
                {
                    var gid = uint.Parse(s.Trim());
                    var x   = k % width;
                    var y   = k / width;

                    layer.Tiles[k++] = gid != 0 ? new TmxLayerTile(map, gid, x, y) : null;
                }
            }
            else if (encoding == null)
            {
                int k = 0;
                foreach (var e in xData.Elements("tile"))
                {
                    var gid = (uint?)e.Attribute("gid") ?? 0;

                    var x = k % width;
                    var y = k / width;

                    layer.Tiles[k++] = gid != 0 ? new TmxLayerTile(map, gid, x, y) : null;
                }
            }
            else
            {
                throw new Exception("TmxLayer: Unknown encoding.");
            }

            layer.Properties = TiledMapLoader.ParsePropertyDict(xLayer.Element("properties"));

            return(layer);
        }