Exemplo n.º 1
0
        public Tiled.Map Save(Map map, Tiled.Map tiledMap)
        {
            var r = Map(map, tiledMap);

            r.Save(tiledMap.FileName);
            return(r);
        }
Exemplo n.º 2
0
        public void DrawMap(SpriteBatch sb, Tiled.Map m)
        {
            foreach (Tiled.Layer l in m.layers)
            {
                for (int y = 0; y < l.height; y++)
                {
                    for (int x = 0; x < l.width; x++)
                    {
                        // don't render past screen edge
                        var tileDrawPos = new Vector2(x * m.tilewidth, y * m.tileheight);
                        if (tileDrawPos.X > GraphicsDevice.Viewport.Width || tileDrawPos.Y > GraphicsDevice.Viewport.Height)
                        {
                            break;
                        }

                        var tileId      = l.data[x + y * l.width] - m.tilesets.First().firstgid;
                        var tilesPerRow = texLookup["terrain"].Width / tileSize;

                        var tileX = tileId % tilesPerRow * tileSize;
                        var tileY = tileId / tilesPerRow * tileSize;

                        var lookupRect = new Rectangle(tileX, tileY, tileSize, tileSize);
                        sb.Draw(texLookup["terrain"], tileDrawPos, lookupRect, Color.White);

                        DrawDebugString(sb, fontLookup["Calibri"], $"{tileId}", tileDrawPos + new Vector2(1), 1f);
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Finds the tileset file name inside the TMX data.
        /// </summary>
        /// <param name="map">The TMX data.</param>
        /// <returns>The tileset file name founded.</returns>
        private static string findTileset(Tiled.Map map)
        {
            var tilesets = map.Tileset;

            if (tilesets.Length > 0)
            {
                return(tilesets.Where(x => x.Image[0].Width == 512)
                       .Select(x => x.Name).First());
            }
            throw new Exception("No tileset file name inside the TMX file!");
        }
Exemplo n.º 4
0
        /// <summary>
        /// Finds a property inside the TMX data.
        /// </summary>
        /// <param name="map">The TMX data.</param>
        /// <param name="field">The name of the property.</param>
        /// <param name="defaultValue">The default value if not found.</param>
        /// <returns>The property founded.</returns>
        private static string findProperty(Tiled.Map map, string field, string defaultValue)
        {
            var props = map.Properties.Items;

            if (props.Length > 0)
            {
                var value = props.Where(x => x.Name.ToLower() == field.ToLower())
                            .Select(x => x.Value).FirstOrDefault();
                return(string.IsNullOrEmpty(value) ? defaultValue : value);
            }
            return(defaultValue);
        }
Exemplo n.º 5
0
        private Map Map(Tiled.Map src, Map dst = null)
        {
            if (dst == null)
            {
                dst = new Map();
            }
            dst.FileName        = src.FileName;
            dst.Size            = new System.Drawing.Size(src.Width, src.Height);
            dst.TileSize        = new System.Drawing.Size(src.TileWidth, src.TileHeight);
            dst.BackgroundColor = src.BackgroundColor;
            dst.Tilesets        = src.Tilesets.Select(x => Map(x)).ToList();
            dst.Layers          = src.Entries.Select(x => Map(x)).ToList();

            dst.BgmField  = GetPropertyValue(src.Properties, Guid.Empty, nameof(dst.BgmField));
            dst.BgmBattle = GetPropertyValue(src.Properties, Guid.Empty, nameof(dst.BgmBattle));

            Uri Extension = GetPropertyValue(src.Properties, default(Uri), nameof(Extension));

            if (Extension != null && !string.IsNullOrEmpty(Extension.OriginalString))
            {
                string fileName;
                if (Path.IsPathRooted(Extension.OriginalString))
                {
                    fileName = Extension.OriginalString;
                }
                else
                {
                    fileName = Path.Combine(Path.GetDirectoryName(src.FileName), Extension.OriginalString);
                }
                if (File.Exists(fileName))
                {
                    using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            var strContent = reader.ReadToEnd();
                            var ext        = JsonConvert.DeserializeObject <Ext>(strContent);
                            dst.LayersDefinition = ext?.LayersDefinition ?? new List <LayerDefinition>();
                            dst.EventDefinitions = ext?.EventsDefinitions ?? new List <EventDefinition>();
                        }
                    }
                }
            }
            return(dst);
        }
Exemplo n.º 6
0
        private Tiled.Map Map(Map src, Tiled.Map dst = null)
        {
            var basePath = Path.GetDirectoryName(src.FileName);

            if (dst == null)
            {
                dst = new Tiled.Map();
            }
            dst.FileName        = src.FileName;
            dst.Width           = src.Size.Width;
            dst.Height          = src.Size.Height;
            dst.TileWidth       = src.TileSize.Width;
            dst.TileHeight      = src.TileSize.Height;
            dst.BackgroundColor = src.BackgroundColor;
            //dst.Tilesets = src.Tilesets.Select(x => Map(x)).ToList();
            dst.Entries = src.Layers.Select(x => Map(x)).ToList();
            dst.Properties[nameof(src.BgmField)]  = src.BgmField;
            dst.Properties[nameof(src.BgmBattle)] = src.BgmBattle;

            var extFileName = $"{Path.GetFileNameWithoutExtension(src.FileName)}.ext.json";

            SetPropertyValue(dst.Properties, new Uri(extFileName, UriKind.Relative), "Extension");
            var fileName = Path.Combine(Path.GetDirectoryName(src.FileName), extFileName);

            using (var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write(JsonConvert.SerializeObject(new Ext()
                    {
                        LayersDefinition  = src.LayersDefinition,
                        EventsDefinitions = src.EventDefinitions?
                                            .OrderBy(x => x.Index)
                                            .ToList()
                    }, Formatting.Indented));
                }
            }
            return(dst);
        }
Exemplo n.º 7
0
 public Map Open(Tiled.Map tiledMap, IEnumerable <ObjectExtensionDefinition> objExt)
 {
     LoadExtensions(objExt);
     return(Map(tiledMap));
 }