Esempio n. 1
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();
            }
        }
Esempio n. 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);
            }
        }
Esempio n. 3
0
        public static TmxTilesetTile LoadTmxTilesetTile(this TmxTilesetTile tile, TmxTileset tileset, XElement xTile, TmxList <TmxTerrain> Terrains, bool headless = false, string tmxDir = "")
        {
            tile.Tileset = tileset;
            tile.Id      = (int)xTile.Attribute("id");

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

            if (strTerrain != null)
            {
                tile.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;
                    }
                    tile.TerrainEdges[index++] = edge;
                }
            }

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

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

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

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

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

            if (tile.Properties != null)
            {
                tile.ProcessProperties();
            }

            return(tile);
        }
Esempio n. 4
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));
                }
            }
        }