コード例 #1
0
        public static TmxImageLayer LoadTmxImageLayer(this TmxImageLayer layer, TmxMap map, XElement xImageLayer, string tmxDir = "")
        {
            layer.Map  = map;
            layer.Name = (string)xImageLayer.Attribute("name");

            layer.Width   = (int?)xImageLayer.Attribute("width");
            layer.Height  = (int?)xImageLayer.Attribute("height");
            layer.Visible = (bool?)xImageLayer.Attribute("visible") ?? true;
            layer.Opacity = (float?)xImageLayer.Attribute("opacity") ?? 1.0f;
            layer.OffsetX = (float?)xImageLayer.Attribute("offsetx") ?? 0.0f;
            layer.OffsetY = (float?)xImageLayer.Attribute("offsety") ?? 0.0f;

            var xImage = xImageLayer.Element("image");

            if (xImage != null)
            {
                layer.Image = new TmxImage().LoadTmxImage(xImage, tmxDir);
            }

            layer.Properties = ParsePropertyDict(xImageLayer.Element("properties"));

            return(layer);
        }
コード例 #2
0
ファイル: TiledMapMover.cs プロジェクト: JonSnowbd/Ash
 public TiledMapMover(TmxLayer collisionLayer)
 {
     Insist.IsNotNull(collisionLayer, nameof(collisionLayer) + " is required");
     CollisionLayer = collisionLayer;
     TiledMap       = collisionLayer.Map;
 }
コード例 #3
0
        public static TmxTileset LoadTmxTileset(this TmxTileset tileset, TmxMap map, XElement xTileset, int firstGid, string tsxDir)
        {
            tileset.Map      = map;
            tileset.FirstGid = firstGid;

            tileset.Name       = (string)xTileset.Attribute("name");
            tileset.TileWidth  = (int)xTileset.Attribute("tilewidth");
            tileset.TileHeight = (int)xTileset.Attribute("tileheight");
            tileset.Spacing    = (int?)xTileset.Attribute("spacing") ?? 0;
            tileset.Margin     = (int?)xTileset.Attribute("margin") ?? 0;
            tileset.Columns    = (int?)xTileset.Attribute("columns");
            tileset.TileCount  = (int?)xTileset.Attribute("tilecount");
            tileset.TileOffset = ParseTmxTileOffset(xTileset.Element("tileoffset"));

            var xImage = xTileset.Element("image");

            if (xImage != null)
            {
                tileset.Image = new TmxImage().LoadTmxImage(xImage, tsxDir);
            }

            var xTerrainType = xTileset.Element("terraintypes");

            if (xTerrainType != null)
            {
                tileset.Terrains = new TmxList <TmxTerrain>();
                foreach (var e in xTerrainType.Elements("terrain"))
                {
                    tileset.Terrains.Add(ParseTmxTerrain(e));
                }
            }

            tileset.Tiles = new Dictionary <int, TmxTilesetTile>();
            foreach (var xTile in xTileset.Elements("tile"))
            {
                var tile = new TmxTilesetTile().LoadTmxTilesetTile(tileset, xTile, tileset.Terrains, tsxDir);
                tileset.Tiles[tile.Id] = tile;
            }

            tileset.Properties = ParsePropertyDict(xTileset.Element("properties"));

            // cache our source rects for each tile so we dont have to calculate them every time we render. If we have
            // an image this is a normal tileset, else its an image tileset
            tileset.TileRegions = new Dictionary <int, RectangleF>();
            if (tileset.Image != null)
            {
                var id = firstGid;
                for (var y = tileset.Margin; y < tileset.Image.Height - tileset.Margin; y += tileset.TileHeight + tileset.Spacing)
                {
                    var column = 0;
                    for (var x = tileset.Margin; x < tileset.Image.Width - tileset.Margin; x += tileset.TileWidth + tileset.Spacing)
                    {
                        tileset.TileRegions.Add(id++, new RectangleF(x, y, tileset.TileWidth, tileset.TileHeight));

                        if (++column >= tileset.Columns)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                foreach (var tile in tileset.Tiles.Values)
                {
                    tileset.TileRegions.Add(firstGid + tile.Id, new RectangleF(0, 0, tile.Image.Width, tile.Image.Height));
                }
            }

            return(tileset);
        }
コード例 #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;
            // 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);
        }
コード例 #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);
                }
            }
        }