Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public TmxTilesetTile(TmxTileset tileset, XElement xTile, TmxList <TmxTerrain> Terrains, string tmxDir = "")
        {
            Tileset = tileset;
            Id      = (int)xTile.Attribute("id");

            var strTerrain = (string)xTile.Attribute("terrain");

            if (strTerrain != null)
            {
                TerrainEdges = new TmxTerrain[4];
                var index = 0;
                foreach (var v in strTerrain.Split(','))
                {
                    var success = int.TryParse(v, out int result);

                    TmxTerrain edge;
                    if (success)
                    {
                        edge = Terrains[result];
                    }
                    else
                    {
                        edge = null;
                    }
                    TerrainEdges[index++] = edge;
                }
            }

            Probability = (double?)xTile.Attribute("probability") ?? 1.0;
            Type        = (string)xTile.Attribute("type");
            var xImage = xTile.Element("image");

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

            ObjectGroups = new TmxList <TmxObjectGroup>();
            foreach (var e in xTile.Elements("objectgroup"))
            {
                ObjectGroups.Add(new TmxObjectGroup(tileset.Map, e));
            }

            AnimationFrames = new List <TmxAnimationFrame>();
            if (xTile.Element("animation") != null)
            {
                foreach (var e in xTile.Element("animation").Elements("frame"))
                {
                    AnimationFrames.Add(new TmxAnimationFrame(e));
                }
            }

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

            if (Properties != null)
            {
                ProcessProperties();
            }
        }
Ejemplo n.º 4
0
        public TmxImageLayer(TmxMap map, XElement xImageLayer, string tmxDir = "")
        {
            this.Map = map;
            Name     = (string)xImageLayer.Attribute("name");

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

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

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

            Properties = PropertyDict.ParsePropertyDict(xImageLayer.Element("properties"));
        }
Ejemplo n.º 5
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));
                }
            }
        }