Beispiel #1
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.");
                }
            }
Beispiel #2
0
        public TmxGroup(TmxMap map, XElement xGroup, int width, int height, string tmxDirectory)
        {
            this.map = map;
            Name     = (string)xGroup.Attribute("name") ?? string.Empty;
            Opacity  = (float?)xGroup.Attribute("opacity") ?? 1.0f;
            Visible  = (bool?)xGroup.Attribute("visible") ?? true;
            OffsetX  = (float?)xGroup.Attribute("offsetx") ?? 0.0f;
            OffsetY  = (float?)xGroup.Attribute("offsety") ?? 0.0f;

            Properties = PropertyDict.ParsePropertyDict(xGroup.Element("properties"));

            layers       = new TmxList <ITmxLayer>();
            tileLayers   = new TmxList <TmxLayer>();
            objectGroups = new TmxList <TmxObjectGroup>();
            imageLayers  = new TmxList <TmxImageLayer>();
            groups       = new TmxList <TmxGroup>();
            foreach (var e in xGroup.Elements().Where(x => x.Name == "layer" || x.Name == "objectgroup" || x.Name == "imagelayer" || x.Name == "group"))
            {
                ITmxLayer layer;
                switch (e.Name.LocalName)
                {
                case "layer":
                    var tileLayer = new TmxLayer(map, e, width, height);
                    layer = tileLayer;
                    tileLayers.Add(tileLayer);
                    break;

                case "objectgroup":
                    var objectgroup = new TmxObjectGroup(map, e);
                    layer = objectgroup;
                    objectGroups.Add(objectgroup);
                    break;

                case "imagelayer":
                    var imagelayer = new TmxImageLayer(map, e, tmxDirectory);
                    layer = imagelayer;
                    imageLayers.Add(imagelayer);
                    break;

                case "group":
                    var group = new TmxGroup(map, e, width, height, tmxDirectory);
                    layer = group;
                    groups.Add(group);
                    break;

                default:
                    throw new InvalidOperationException();
                }
                layers.Add(layer);
            }
        }
Beispiel #3
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];


            //xData should just be a string, if not, then its chunked
            if (xData.HasElements)
            {
                foreach (var chunk in xData.Elements("chunk"))
                {
                    var chunkWidth  = (int)chunk.Attribute("width");
                    var chunkHeight = (int)chunk.Attribute("height");
                    var chunkX      = (int)chunk.Attribute("x");
                    var chunkY      = (int)chunk.Attribute("y");
                    LoadLayerData(layer, map, chunkWidth, chunkHeight, chunkX, chunkY, chunk, encoding);
                }
            }
            else
            {
                LoadLayerData(layer, map, width, height, 0, 0, xData, encoding);
            }

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

            return(layer);
Beispiel #4
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);
        }
Beispiel #5
0
        /// <summary>
        /// parses all the layers in xEle putting them in the container
        /// </summary>
        public static void ParseLayers(object container, XElement xEle, TmxMap map, int width, int height, string tmxDirectory)
        {
            foreach (var e in xEle.Elements().Where(x => x.Name == "layer" || x.Name == "objectgroup" || x.Name == "imagelayer" || x.Name == "group"))
            {
                ITmxLayer layer;
                switch (e.Name.LocalName)
                {
                case "layer":
                    var tileLayer = new TmxLayer().LoadTmxLayer(map, e, width, height);
                    layer = tileLayer;

                    if (container is TmxMap m)
                    {
                        m.TileLayers.Add(tileLayer);
                    }
                    else if (container is TmxGroup g)
                    {
                        g.TileLayers.Add(tileLayer);
                    }
                    break;

                case "objectgroup":
                    var objectgroup = new TmxObjectGroup().LoadTmxObjectGroup(map, e);
                    layer = objectgroup;

                    if (container is TmxMap mm)
                    {
                        mm.ObjectGroups.Add(objectgroup);
                    }
                    else if (container is TmxGroup gg)
                    {
                        gg.ObjectGroups.Add(objectgroup);
                    }
                    break;

                case "imagelayer":
                    var imagelayer = new TmxImageLayer().LoadTmxImageLayer(map, e, tmxDirectory);
                    layer = imagelayer;

                    if (container is TmxMap mmm)
                    {
                        mmm.ImageLayers.Add(imagelayer);
                    }
                    else if (container is TmxGroup ggg)
                    {
                        ggg.ImageLayers.Add(imagelayer);
                    }
                    break;

                case "group":
                    var newGroup = new TmxGroup().LoadTmxGroup(map, e, width, height, tmxDirectory);
                    layer = newGroup;

                    if (container is TmxMap mmmm)
                    {
                        mmmm.Groups.Add(newGroup);
                    }
                    else if (container is TmxGroup gggg)
                    {
                        gggg.Groups.Add(newGroup);
                    }
                    break;

                default:
                    throw new InvalidOperationException();
                }

                if (container is TmxMap mmmmm)
                {
                    mmmmm.Layers.Add(layer);
                }
                else if (container is TmxGroup g)
                {
                    g.Layers.Add(layer);
                }
            }
        }
Beispiel #6
0
 public TiledMapMover(TmxLayer collisionLayer)
 {
     Insist.IsNotNull(collisionLayer, nameof(collisionLayer) + " is required");
     CollisionLayer = collisionLayer;
     TiledMap       = collisionLayer.Map;
 }