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
        public static TmxGroup LoadTmxGroup(this TmxGroup group, TmxMap map, XElement xGroup, int width, int height, string tmxDirectory)
        {
            group.map             = map;
            group.Name            = (string)xGroup.Attribute("name") ?? string.Empty;
            group.Opacity         = (float?)xGroup.Attribute("opacity") ?? 1.0f;
            group.Visible         = (bool?)xGroup.Attribute("visible") ?? true;
            group.OffsetX         = (float?)xGroup.Attribute("offsetx") ?? 0.0f;
            group.OffsetY         = (float?)xGroup.Attribute("offsety") ?? 0.0f;
            group.ParallaxFactorX = (float?)xGroup.Attribute("parallaxx") ?? 1.0f;
            group.ParallaxFactorY = (float?)xGroup.Attribute("parallaxy") ?? 1.0f;

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

            group.Layers       = new TmxList <ITmxLayer>();
            group.TileLayers   = new TmxList <TmxLayer>();
            group.ObjectGroups = new TmxList <TmxObjectGroup>();
            group.ImageLayers  = new TmxList <TmxImageLayer>();
            group.Groups       = new TmxList <TmxGroup>();

            ParseLayers(group, xGroup, map, width, height, tmxDirectory);

            return(group);
        }
예제 #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;
            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);
        }
예제 #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);
                }
            }
        }
예제 #6
0
        public static TmxMap LoadTmxMap(this TmxMap map, XDocument xDoc)
        {
            var xMap = xDoc.Element("map");

            map.Version      = (string)xMap.Attribute("version");
            map.TiledVersion = (string)xMap.Attribute("tiledversion");

            map.Width         = (int)xMap.Attribute("width");
            map.Height        = (int)xMap.Attribute("height");
            map.TileWidth     = (int)xMap.Attribute("tilewidth");
            map.TileHeight    = (int)xMap.Attribute("tileheight");
            map.HexSideLength = (int?)xMap.Attribute("hexsidelength");

            // enum parsing
            map.Orientation  = ParseOrientationType((string)xMap.Attribute("orientation"));
            map.StaggerAxis  = ParseStaggerAxisType((string)xMap.Attribute("staggeraxis"));
            map.StaggerIndex = ParseStaggerIndexType((string)xMap.Attribute("staggerindex"));
            map.RenderOrder  = ParaseRenderOrderType((string)xMap.Attribute("renderorder"));

            map.NextObjectID    = (int?)xMap.Attribute("nextobjectid");
            map.BackgroundColor = ParseColor(xMap.Attribute("backgroundcolor"));

            map.Properties = ParsePropertyDict(xMap.Element("properties"));

            // we keep a tally of the max tile size for the case of image tilesets with random sizes
            map.MaxTileWidth  = map.TileWidth;
            map.MaxTileHeight = map.TileHeight;

            map.Tilesets = new TmxList <TmxTileset>();
            foreach (var e in xMap.Elements("tileset"))
            {
                var tileset = ParseTmxTileset(map, e, map.TmxDirectory);
                map.Tilesets.Add(tileset);

                // we have to iterate the dictionary because tile.gid (the key) could be any number in any order
                foreach (var kvPair in tileset.Tiles)
                {
                    var tile = kvPair.Value;
                    if (tile.Image != null)
                    {
                        if (tile.Image.Width > map.MaxTileWidth)
                        {
                            map.MaxTileWidth = tile.Image.Width;
                        }
                        if (tile.Image.Height > map.MaxTileHeight)
                        {
                            map.MaxTileHeight = tile.Image.Height;
                        }
                    }
                }
            }

            map.Layers       = new TmxList <ITmxLayer>();
            map.TileLayers   = new TmxList <TmxLayer>();
            map.ObjectGroups = new TmxList <TmxObjectGroup>();
            map.ImageLayers  = new TmxList <TmxImageLayer>();
            map.Groups       = new TmxList <TmxGroup>();

            ParseLayers(map, xMap, map, map.Width, map.Height, map.TmxDirectory);

            return(map);
        }
예제 #7
0
파일: PosUtil.cs 프로젝트: toyboot4e/Rot
        Camera camera;          // world <-> screen

        public PosUtil(TmxMap tiled, Camera camera)
        {
            this.tiled  = tiled;
            this.camera = camera;
        }
예제 #8
0
 public TiledMapMover(TmxLayer collisionLayer)
 {
     Insist.IsNotNull(collisionLayer, nameof(collisionLayer) + " is required");
     CollisionLayer = collisionLayer;
     TiledMap       = collisionLayer.Map;
 }
예제 #9
0
        public TmxTileset(TmxMap map, XElement xTileset, int firstGid, string tmxDir)
        {
            Map      = map;
            FirstGid = firstGid;

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

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

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

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

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

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

            Properties = PropertyDict.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
            TileRegions = new Dictionary <int, RectangleF>();
            if (Image != null)
            {
                var id = firstGid;
                for (var y = Margin; y < Image.Height - Margin; y += TileHeight + Spacing)
                {
                    var column = 0;
                    for (var x = Margin; x < Image.Width - Margin; x += TileWidth + Spacing)
                    {
                        TileRegions.Add(id++, new RectangleF(x, y, TileWidth, TileHeight));

                        if (++column >= Columns)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                // it seems that firstGid is always 0 for image tilesets so we can access them like an array here
                var id = firstGid;
                for (var i = 0; i < Tiles.Count; i++)
                {
                    var tile = Tiles[i];
                    TileRegions.Add(id++, new RectangleF(0, 0, tile.Image.Width, tile.Image.Height));
                }
            }
        }