示例#1
0
        public TmxTileset(XMLReader xTileset)
        {
            FirstGid   = xTileset.AttributeInt("firstgid");
            Name       = xTileset.Attribute("name");
            TileWidth  = xTileset.AttributeInt("tilewidth");
            TileHeight = xTileset.AttributeInt("tileheight");
            Spacing    = xTileset.AttributeIntN("spacing") ?? 0;
            Margin     = xTileset.AttributeInt("margin");
            Columns    = xTileset.AttributeIntN("columns");
            TileCount  = xTileset.AttributeIntN("tilecount");
            TileOffset = TmxHelpers.GetVector2(xTileset.Element("tileoffset"));

            XMLReader image = xTileset.Element("image");

            if (image != null)
            {
                Source = image.Attribute("source");
            }

            Terrains = new TmxList <TmxTerrain>();
            XMLReader xTerrainType = xTileset.Element("terraintypes");

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

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

            Properties = TmxHelpers.GetPropertyDict(xTileset.Element("properties"));
        }
示例#2
0
文件: TmxMap.cs 项目: Cryru/Emotion
        public TmxMap(XMLReader reader, string?filePath = null)
        {
            XMLReader?xMap = reader.Element("map");

            if (xMap == null)
            {
                return;
            }

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

            Width           = xMap.AttributeInt("width");
            Height          = xMap.AttributeInt("height");
            TileWidth       = xMap.AttributeInt("tilewidth");
            TileHeight      = xMap.AttributeInt("tileheight");
            HexSideLength   = xMap.AttributeIntN("hexsidelength");
            Orientation     = xMap.AttributeEnum <Orientation>("orientation");
            StaggerAxis     = xMap.AttributeEnum <StaggerAxis>("staggeraxis");
            StaggerIndex    = xMap.AttributeEnum <StaggerIndex>("staggerindex");
            RenderOrder     = xMap.AttributeEnum <RenderOrder>("renderorder");
            NextObjectId    = xMap.AttributeIntN("nextobjectid");
            BackgroundColor = TmxHelpers.ParseTmxColor(xMap.Attribute("backgroundcolor"));

            Properties = TmxHelpers.GetPropertyDict(xMap.Element("properties"));

            foreach (XMLReader e in xMap.Elements("tileset"))
            {
                string?fileSource = e.Attribute("source");
                int    firstGid   = e.AttributeInt("firstgid");

                // Check if external file.
                if (!string.IsNullOrEmpty(fileSource))
                {
                    if (filePath != null)
                    {
                        string directory = AssetLoader.GetDirectoryName(filePath);
                        fileSource = AssetLoader.GetNonRelativePath(directory, fileSource);
                    }
                    var textAsset = Engine.AssetLoader.Get <TextAsset>(fileSource);
                    if (textAsset?.Content == null)
                    {
                        Engine.Log.Warning("Couldn't load external tileset.", MessageSource.TMX);
                        continue;
                    }

                    var       externalReader = new XMLReader(textAsset.Content);
                    XMLReader?tileSetElement = externalReader.Element("tileset");
                    if (tileSetElement == null)
                    {
                        continue;
                    }
                    Tilesets.Add(new TmxTileset(firstGid, tileSetElement));
                    continue;
                }

                Tilesets.Add(new TmxTileset(firstGid, e));
            }

            foreach (XMLReader e in xMap.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 objectLayer = new TmxObjectLayer(e);
                    layer = objectLayer;
                    ObjectLayers.Add(objectLayer);
                    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);
            }
        }
 public TmxTerrain(XMLReader xTerrain)
 {
     Name       = xTerrain.Attribute("name");
     Tile       = xTerrain.AttributeInt("tile");
     Properties = TmxHelpers.GetPropertyDict(xTerrain.Element("properties"));
 }
示例#4
0
        public TmxMap(XMLReader reader)
        {
            XMLReader xMap = reader.Element("map");

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

            Width           = xMap.AttributeInt("width");
            Height          = xMap.AttributeInt("height");
            TileWidth       = xMap.AttributeInt("tilewidth");
            TileHeight      = xMap.AttributeInt("tileheight");
            HexSideLength   = xMap.AttributeIntN("hexsidelength");
            Orientation     = xMap.AttributeEnum <Orientation>("orientation");
            StaggerAxis     = xMap.AttributeEnum <StaggerAxis>("staggeraxis");
            StaggerIndex    = xMap.AttributeEnum <StaggerIndex>("staggerindex");
            RenderOrder     = xMap.AttributeEnum <RenderOrder>("renderorder");
            NextObjectId    = xMap.AttributeIntN("nextobjectid");
            BackgroundColor = TmxHelpers.ParseTmxColor(xMap.Attribute("backgroundcolor"));

            Properties = TmxHelpers.GetPropertyDict(xMap.Element("properties"));

            Tilesets = new TmxList <TmxTileset>();
            foreach (XMLReader e in xMap.Elements("tileset"))
            {
                Tilesets.Add(new TmxTileset(e));
            }

            Layers       = new TmxList <TmxLayer>();
            TileLayers   = new TmxList <TmxLayer>();
            ObjectLayers = new TmxList <TmxObjectLayer>();
            ImageLayers  = new TmxList <TmxImageLayer>();
            Groups       = new TmxList <TmxGroupedLayers>();
            foreach (XMLReader e in xMap.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 objectLayer = new TmxObjectLayer(e);
                    layer = objectLayer;
                    ObjectLayers.Add(objectLayer);
                    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);
            }
        }