Example #1
0
        // TMX tileset element constructor
        public TmxTileset(XElement xTileset, string tmxDir = "")
        {
            var xFirstGid = xTileset.Attribute("firstgid");
            var source = (string) xTileset.Attribute("source");

            if (source != null)
            {
                // Prepend the parent TMX directory if necessary
                source = Path.Combine(tmxDir, source);

                // source is always preceded by firstgid
                FirstGid = (int) xFirstGid;

                // Everything else is in the TSX file
                var xDocTileset = ReadXml(source);
                var ts = new TmxTileset(xDocTileset, TmxDirectory);
                Name = ts.Name;
                TileWidth = ts.TileWidth;
                TileHeight = ts.TileHeight;
                Spacing = ts.Spacing;
                Margin = ts.Margin;
                TileCount = ts.TileCount;
                TileOffset = ts.TileOffset;
                Image = ts.Image;
                Terrains = ts.Terrains;
                Tiles = ts.Tiles;
                Properties = ts.Properties;
            }
            else
            {
                // firstgid is always in TMX, but not TSX
                if (xFirstGid != null)
                    FirstGid = (int) xFirstGid;

                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;
                TileCount = (int?) xTileset.Attribute("tilecount");
                TileOffset = new TmxTileOffset(xTileset.Element("tileoffset"));
                Image = new TmxImage(xTileset.Element("image"), tmxDir);

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

                Tiles = new Collection<TmxTilesetTile>();
                foreach (var xTile in xTileset.Elements("tile"))
                {
                    var tile = new TmxTilesetTile(xTile, Terrains, tmxDir);
                    Tiles.Add(tile);
                }

                Properties = new PropertyDict(xTileset.Element("properties"));
            }
        }
Example #2
0
        public TmxObjectGroup(XElement xObjectGroup)
        {
            Name = (string) xObjectGroup.Attribute("name") ?? String.Empty;
            Color = new TmxColor(xObjectGroup.Attribute("color"));
            Opacity = (double?) xObjectGroup.Attribute("opacity") ?? 1.0;
            Visible = (bool?) xObjectGroup.Attribute("visible") ?? true;
            OffsetX = (double?) xObjectGroup.Attribute("offsetx") ?? 0.0;
            OffsetY = (double?) xObjectGroup.Attribute("offsety") ?? 0.0;

            var drawOrderDict = new Dictionary<string, DrawOrderType> {
                {"unknown", DrawOrderType.UnknownOrder},
                {"topdown", DrawOrderType.IndexOrder},
                {"index", DrawOrderType.TopDown}
            };

            var drawOrderValue = (string) xObjectGroup.Attribute("draworder");
            if (drawOrderValue != null)
                DrawOrder = drawOrderDict[drawOrderValue];

            Objects = new TmxList<TmxObject>();
            foreach (var e in xObjectGroup.Elements("object"))
                Objects.Add(new TmxObject(e));

            Properties = new PropertyDict(xObjectGroup.Element("properties"));
        }
Example #3
0
        //returns the layers of the map in the order they are supposed to be drawn in
        //TODO: support more layers and figure out depth
        public TmxList <TmxLayer> GetLayers()
        {
            var layers = new TmxList <TmxLayer>();

            layers.Add(map.Layers[BASE_LAYER_NAME]);
            return(layers);
        }
Example #4
0
        public TmxMap(string filename)
        {
            XDocument xDoc = ReadXml(filename);
            var xMap = xDoc.Element("map");

            Version = (string)xMap.Attribute("version");
            Orientation = (OrientationType) Enum.Parse(
                                    typeof(OrientationType),
                                    xMap.Attribute("orientation").Value,
                                    true);
            Width = (int)xMap.Attribute("width");
            Height = (int)xMap.Attribute("height");
            TileWidth = (int)xMap.Attribute("tilewidth");
            TileHeight = (int)xMap.Attribute("tileheight");
            BackgroundColor = new TmxColor(xMap.Attribute("backgroundcolor"));

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

            Layers = new TmxList<TmxLayer>();
            foreach (var e in xMap.Elements("layer"))
                Layers.Add(new TmxLayer(e, Width, Height));

            ObjectGroups = new TmxList<TmxObjectGroup>();
            foreach (var e in xMap.Elements("objectgroup"))
                ObjectGroups.Add(new TmxObjectGroup(e));

            ImageLayers = new TmxList<TmxImageLayer>();
            foreach (var e in xMap.Elements("imagelayer"))
                ImageLayers.Add(new TmxImageLayer(e, TmxDirectory));

            Properties = new PropertyDict(xMap.Element("properties"));
        }
Example #5
0
        public TmxMap(Stream stream)
        {
            XDocument xDoc = ReadXml(stream);
            XElement xMap = xDoc.Element("map");

            Version = (string) xMap.Attribute("version");
            Orientation = (OrientationType) Enum.Parse(
                typeof (OrientationType),
                xMap.Attribute("orientation").Value,
                true);
            Width = (int) xMap.Attribute("width");
            Height = (int) xMap.Attribute("height");
            TileWidth = (int) xMap.Attribute("tilewidth");
            TileHeight = (int) xMap.Attribute("tileheight");
            BackgroundColor = new TmxColor(xMap.Attribute("backgroundcolor"));

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

            Layers = new TmxList<TmxLayer>();
            foreach (XElement e in xMap.Elements("layer"))
                Layers.Add(new TmxLayer(e, Width, Height));

            ObjectGroups = new TmxList<TmxObjectGroup>();
            foreach (XElement e in xMap.Elements("objectgroup"))
                ObjectGroups.Add(new TmxObjectGroup(e));

            if (xMap.Elements("imagelayer").Any())
                throw new NotSupportedException(
                    "Image layers are not supported in the current implementation. You can disable this warning at your own risk.");

            Properties = new PropertyDict(xMap.Element("properties"));
        }
 public TmxObjectLayer(XMLReader xObjectGroup) : base(xObjectGroup, 0, 0)
 {
     Color     = TmxHelpers.ParseTmxColor(xObjectGroup.Attribute("color"));
     DrawOrder = xObjectGroup.AttributeEnum <DrawOrder>("draworder");
     Objects   = new TmxList <TmxObject>();
     foreach (XMLReader e in xObjectGroup.Elements("object"))
     {
         Objects.Add(new TmxObject(e));
     }
 }
        public TmxObjectGroup(XElement xObjectGroup)
        {
            Name = (string)xObjectGroup.Attribute("name");
            Color = new TmxColor(xObjectGroup.Attribute("color"));
            Opacity = (double?)xObjectGroup.Attribute("opacity") ?? 1.0;
            Visible = (bool?)xObjectGroup.Attribute("visible") ?? true;

            Objects = new TmxList<TmxObject>();
            foreach (var e in xObjectGroup.Elements("object"))
                Objects.Add(new TmxObject(e));

            Properties = new PropertyDict(xObjectGroup.Element("properties"));
        }
        public TmxGroupedLayers(XMLReader xGroup, int width, int height) : base(xGroup, width, height)
        {
            Layers       = new TmxList <TmxLayer>();
            TileLayers   = new TmxList <TmxLayer>();
            ObjectGroups = new TmxList <TmxObjectLayer>();
            ImageLayers  = new TmxList <TmxImageLayer>();
            Groups       = new TmxList <TmxGroupedLayers>();
            foreach (XMLReader e in xGroup.Elements().Where(x => x.Name == "layer" || x.Name == "objectgroup" || x.Name == "imagelayer" || x.Name == "group"))
            {
                TmxLayer layer;
                switch (e.Name)
                {
                case "layer":
                    var tileLayer = new TmxLayer(e, width, height);
                    layer = tileLayer;
                    TileLayers.Add(tileLayer);
                    break;

                case "objectgroup":
                    var objectgroup = new TmxObjectLayer(e);
                    layer = objectgroup;
                    ObjectGroups.Add(objectgroup);
                    break;

                case "imagelayer":
                    var imagelayer = new TmxImageLayer(e);
                    layer = imagelayer;
                    ImageLayers.Add(imagelayer);
                    break;

                case "group":
                    var group = new TmxGroupedLayers(e, width, height);
                    layer = group;
                    Groups.Add(group);
                    break;

                default:
                    Engine.Log.Warning($"Unknown TMX layer type {e.Name}.", MessageSource.TMX);
                    continue;
                }

                Layers.Add(layer);
            }
        }
Example #9
0
        public TmxTilesetTile(XElement xTile, TmxList<TmxTerrain> Terrains,
                       string tmxDir = "")
        {
            Id = (int)xTile.Attribute("id");

            TerrainEdges = new Collection<TmxTerrain>();

            int result;
            TmxTerrain edge;

            var strTerrain = (string)xTile.Attribute("terrain") ?? ",,,";
            foreach (var v in strTerrain.Split(',')) {
                var success = int.TryParse(v, out result);
                if (success)
                    edge = Terrains[result];
                else
                    edge = null;
                TerrainEdges.Add(edge);

                // TODO: Assert that TerrainEdges length is 4
            }

            Probability = (double?)xTile.Attribute("probability") ?? 1.0;
            Image = new TmxImage(xTile.Element("image"), tmxDir);

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

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

            Properties = new PropertyDict(xTile.Element("properties"));
        }
Example #10
0
        public TmxMap(string filename)
        {
            XDocument xDoc = ReadXml(filename);
            var xMap = xDoc.Element("map");

            Version = (string) xMap.Attribute("version");

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

            // Map orientation type
            var orientDict = new Dictionary<string, OrientationType> {
                {"unknown", OrientationType.Unknown},
                {"orthogonal", OrientationType.Orthogonal},
                {"isometric", OrientationType.Isometric},
                {"staggered", OrientationType.Staggered},
                {"hexagonal", OrientationType.Hexagonal},
            };

            var orientValue = (string) xMap.Attribute("orientation");
            if (orientValue != null)
                Orientation = orientDict[orientValue];

            // Hexagonal stagger axis
            var staggerAxisDict = new Dictionary<string, StaggerAxisType> {
                {"x", StaggerAxisType.X},
                {"y", StaggerAxisType.Y},
            };

            var staggerAxisValue = (string) xMap.Attribute("staggeraxis");
            if (staggerAxisValue != null)
                StaggerAxis = staggerAxisDict[staggerAxisValue];

            // Hexagonal stagger index
            var staggerIndexDict = new Dictionary<string, StaggerIndexType> {
                {"odd", StaggerIndexType.Odd},
                {"even", StaggerIndexType.Even},
            };

            var staggerIndexValue = (string) xMap.Attribute("staggerindex");
            if (staggerIndexValue != null)
                StaggerIndex = staggerIndexDict[staggerIndexValue];

            // Tile render order
            var renderDict = new Dictionary<string, RenderOrderType> {
                {"right-down", RenderOrderType.RightDown},
                {"right-up", RenderOrderType.RightUp},
                {"left-down", RenderOrderType.LeftDown},
                {"left-up", RenderOrderType.LeftUp}
            };

            var renderValue = (string) xMap.Attribute("renderorder");
            if (renderValue != null)
                RenderOrder = renderDict[renderValue];

            NextObjectID = (int?)xMap.Attribute("nextobjectid");
            BackgroundColor = new TmxColor(xMap.Attribute("backgroundcolor"));

            Properties = new PropertyDict(xMap.Element("properties"));

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

            Layers = new TmxList<TmxLayer>();
            foreach (var e in xMap.Elements("layer"))
                Layers.Add(new TmxLayer(e, Width, Height));

            ObjectGroups = new TmxList<TmxObjectGroup>();
            foreach (var e in xMap.Elements("objectgroup"))
                ObjectGroups.Add(new TmxObjectGroup(e));

            ImageLayers = new TmxList<TmxImageLayer>();
            foreach (var e in xMap.Elements("imagelayer"))
                ImageLayers.Add(new TmxImageLayer(e, TmxDirectory));
        }
Example #11
0
        // TMX tileset element constructor
        public TmxTileset(XElement xTileset, string tmxDir = "")
        {
            XAttribute xFirstGid = xTileset.Attribute("firstgid");
            var source = (string) xTileset.Attribute("source");

            if (source != null)
            {
                throw new NotSupportedException(
                    "External tilesets are not yet supported. Please move the tileset or implement this.");

                // This won't work just yet; we'll need to implement a seperate loader probably

                // Prepend the parent TMX directory if necessary
                source = Path.Combine(tmxDir, source);

                // source is always preceded by firstgid
                FirstGid = (int) xFirstGid;

                // Everything else is in the TSX file
                XDocument xDocTileset = ReadXml(new FileStream(source, FileMode.Open));
                var ts = new TmxTileset(xDocTileset, TmxDirectory);

                Name = ts.Name;
                TileWidth = ts.TileWidth;
                TileHeight = ts.TileHeight;
                Spacing = ts.Spacing;
                Margin = ts.Margin;
                TileOffset = ts.TileOffset;
                Image = ts.Image;
                Terrains = ts.Terrains;
                Tiles = ts.Tiles;
                Properties = ts.Properties;
            }
            else
            {
                // firstgid is always in TMX, but not TSX
                if (xFirstGid != null)
                    FirstGid = (int) xFirstGid;

                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;

                TileOffset = new TmxTileOffset(xTileset.Element("tileoffset"));
                Image = new TmxImage(xTileset.Element("image"), tmxDir);

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

                Tiles = new List<TmxTilesetTile>();
                foreach (XElement xTile in xTileset.Elements("tile"))
                {
                    var tile = new TmxTilesetTile(xTile, Terrains, tmxDir);
                    Tiles.Add(tile);
                }

                Properties = new PropertyDict(xTileset.Element("properties"));
            }
        }