From 7407edccb3c32bc39eeae0ef4aa749d1f030ae06 Mon Sep 17 00:00:00 2001 From: krnlexception Date: Tue, 10 Sep 2024 00:56:24 +0200 Subject: [PATCH 1/8] Example project --- src/DotTiled.Example/DotTiled.Example.csproj | 29 +++++++ src/DotTiled.Example/Program.cs | 78 +++++++++++++++++++ src/DotTiled.Example/tilemap.tmx | 12 +++ src/DotTiled.Example/tileset.png | Bin 0 -> 149 bytes src/DotTiled.Example/tileset.tsx | 4 + src/DotTiled.sln | 6 ++ 6 files changed, 129 insertions(+) create mode 100644 src/DotTiled.Example/DotTiled.Example.csproj create mode 100644 src/DotTiled.Example/Program.cs create mode 100644 src/DotTiled.Example/tilemap.tmx create mode 100644 src/DotTiled.Example/tileset.png create mode 100644 src/DotTiled.Example/tileset.tsx diff --git a/src/DotTiled.Example/DotTiled.Example.csproj b/src/DotTiled.Example/DotTiled.Example.csproj new file mode 100644 index 0000000..89e9d09 --- /dev/null +++ b/src/DotTiled.Example/DotTiled.Example.csproj @@ -0,0 +1,29 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + Always + + + Always + + + Always + + + Always + + + + diff --git a/src/DotTiled.Example/Program.cs b/src/DotTiled.Example/Program.cs new file mode 100644 index 0000000..0f0f806 --- /dev/null +++ b/src/DotTiled.Example/Program.cs @@ -0,0 +1,78 @@ +using DotTiled.Serialization; + +namespace DotTiled.Example; + +class Program +{ + static void Main(string[] args) + { + Quick(); + Manual(); + } + + // QUICK START + // Automatic and easy way to load tilemaps. + static void Quick() + { + var loader = Loader.Default(); + var map = loader.LoadMap("tilemap.tmx"); + + // You can do stuff with it like... + Console.WriteLine($"Tile width and height: {map.TileWidth}x{map.TileHeight}"); + TileLayer layer0 = (TileLayer)map.Layers[0]; // Get a layer + Console.WriteLine($"Tile in layer 0 at 0, 0: {layer0.Data.Value.GlobalTileIDs.Value[0]}"); + } + + // MANUAL + // Manually load a map, if you need to load from a custom source + static void Manual() + { + using var mapFileReader = new StreamReader("tilemap.tmx"); + var mapString = mapFileReader.ReadToEnd(); + using var mapReader = new MapReader(mapString, ResolveTileset, ResolveTemplate, ResolveCustomType); + var map = mapReader.ReadMap(); + + // Now do some other stuff with it... + StringProperty hello = (StringProperty)map.Properties.FirstOrDefault(property => property.Name == "hello"); + Console.WriteLine($"Layer 1 name: {map.Layers[0].Name}"); + Console.WriteLine($"Property 'hello': {hello.Value}"); + + // Now with tileset + Tileset tileset = map.Tilesets[0]; + Console.WriteLine($"Tileset 0 source: {tileset.Source.Value}"); + Console.WriteLine($"Tileset 0 image 0 source: {tileset.Image.Value.Source.Value}"); + } + + /* This function is responsible for loading all tilesets required by a tilemap, if you + want to use a custom source. */ + static Tileset ResolveTileset(string source) + { + string tilesetPath = Path.Combine(Directory.GetCurrentDirectory(), source); // Resolve path to a tileset. + using var tilesetFileReader = new StreamReader(tilesetPath); // Read tileset file itself. + var tilesetString = tilesetFileReader.ReadToEnd(); // You can replace this with any custom function + // to load data from any source, eg. .zip file. + using var tilesetReader = new TilesetReader(tilesetString, ResolveTileset, ResolveTemplate, ResolveCustomType); // Parse loaded tileset. + + return tilesetReader.ReadTileset(); // Return loaded tileset + } + + // This is pretty similar to above, but instead it loads templates, not tilesets. + static Template ResolveTemplate(string source) + { + string templatePath = Path.Combine(Directory.GetCurrentDirectory(), source); + using var templateFileReader = new StreamReader(templatePath); + var templateString = templateFileReader.ReadToEnd(); + using var templateReader = new TemplateReader(templateString, ResolveTileset, ResolveTemplate, ResolveCustomType); + return templateReader.ReadTemplate(); + } + + static ICustomTypeDefinition ResolveCustomType(string name) + { + CustomClassDefinition[] allDefinedTypes = + [ + new CustomClassDefinition() { Name = "a" }, + ]; + return allDefinedTypes.FirstOrDefault(type => type.Name == name); + } + +} diff --git a/src/DotTiled.Example/tilemap.tmx b/src/DotTiled.Example/tilemap.tmx new file mode 100644 index 0000000..7a9a294 --- /dev/null +++ b/src/DotTiled.Example/tilemap.tmx @@ -0,0 +1,12 @@ + + + + + + + + + AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAA== + + + diff --git a/src/DotTiled.Example/tileset.png b/src/DotTiled.Example/tileset.png new file mode 100644 index 0000000000000000000000000000000000000000..67b9eeea3c0801c3a7092d76b229e2560ee5d7c0 GIT binary patch literal 149 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1qucK`l=g#}Etu})QSZQ qC$3zWaLH~Bhp(&*Z!NEp7Q<%lsS3|M&zb^tF?hQAxvX + + + diff --git a/src/DotTiled.sln b/src/DotTiled.sln index 421e996..3eec160 100644 --- a/src/DotTiled.sln +++ b/src/DotTiled.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Tests", "DotTiled. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Benchmark", "DotTiled.Benchmark\DotTiled.Benchmark.csproj", "{510F3077-8EA4-47D1-8D01-E2D538F1B899}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Example", "DotTiled.Example\DotTiled.Example.csproj", "{31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,5 +32,9 @@ Global {510F3077-8EA4-47D1-8D01-E2D538F1B899}.Debug|Any CPU.Build.0 = Debug|Any CPU {510F3077-8EA4-47D1-8D01-E2D538F1B899}.Release|Any CPU.ActiveCfg = Release|Any CPU {510F3077-8EA4-47D1-8D01-E2D538F1B899}.Release|Any CPU.Build.0 = Release|Any CPU + {31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal From eb22de169cf5d15bb07b2ac2c9484c16119f04bf Mon Sep 17 00:00:00 2001 From: krnlexception Date: Tue, 10 Sep 2024 00:57:23 +0200 Subject: [PATCH 2/8] JetBrains Rider .idea directory ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a2b8d83..1b33c6c 100644 --- a/.gitignore +++ b/.gitignore @@ -402,3 +402,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +.idea From 44cbf5b90a3babc95a3eed28213e6b88bbf214df Mon Sep 17 00:00:00 2001 From: krnlException Date: Wed, 11 Sep 2024 17:11:24 +0200 Subject: [PATCH 3/8] Style changes, and usage of embedded resources --- src/DotTiled.Example/DotTiled.Example.csproj | 9 ++++ src/DotTiled.Example/Program.cs | 53 ++++++++++--------- src/DotTiled.Example/embedded-tilemap.tmx | 12 +++++ src/DotTiled.Example/embedded-tileset.png | Bin 0 -> 149 bytes src/DotTiled.Example/embedded-tileset.tsx | 4 ++ 5 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 src/DotTiled.Example/embedded-tilemap.tmx create mode 100644 src/DotTiled.Example/embedded-tileset.png create mode 100644 src/DotTiled.Example/embedded-tileset.tsx diff --git a/src/DotTiled.Example/DotTiled.Example.csproj b/src/DotTiled.Example/DotTiled.Example.csproj index 89e9d09..b1e806e 100644 --- a/src/DotTiled.Example/DotTiled.Example.csproj +++ b/src/DotTiled.Example/DotTiled.Example.csproj @@ -26,4 +26,13 @@ + + + + + + + + + diff --git a/src/DotTiled.Example/Program.cs b/src/DotTiled.Example/Program.cs index 0f0f806..785369b 100644 --- a/src/DotTiled.Example/Program.cs +++ b/src/DotTiled.Example/Program.cs @@ -1,10 +1,11 @@ -using DotTiled.Serialization; +using System.Reflection; +using DotTiled.Serialization; namespace DotTiled.Example; class Program { - static void Main(string[] args) + private static void Main() { Quick(); Manual(); @@ -12,7 +13,7 @@ class Program // QUICK START // Automatic and easy way to load tilemaps. - static void Quick() + private static void Quick() { var loader = Loader.Default(); var map = loader.LoadMap("tilemap.tmx"); @@ -25,7 +26,7 @@ class Program // MANUAL // Manually load a map, if you need to load from a custom source - static void Manual() + private static void Manual() { using var mapFileReader = new StreamReader("tilemap.tmx"); var mapString = mapFileReader.ReadToEnd(); @@ -33,7 +34,7 @@ class Program var map = mapReader.ReadMap(); // Now do some other stuff with it... - StringProperty hello = (StringProperty)map.Properties.FirstOrDefault(property => property.Name == "hello"); + StringProperty hello = map.GetProperty("hello"); Console.WriteLine($"Layer 1 name: {map.Layers[0].Name}"); Console.WriteLine($"Property 'hello': {hello.Value}"); @@ -43,36 +44,38 @@ class Program Console.WriteLine($"Tileset 0 image 0 source: {tileset.Image.Value.Source.Value}"); } - /* This function is responsible for loading all tilesets required by a tilemap, if you - want to use a custom source. */ - static Tileset ResolveTileset(string source) + // This function is responsible for loading all tilesets required by a tilemap, if you + // want to use a custom source. + private static Tileset ResolveTileset(string source) { - string tilesetPath = Path.Combine(Directory.GetCurrentDirectory(), source); // Resolve path to a tileset. - using var tilesetFileReader = new StreamReader(tilesetPath); // Read tileset file itself. - var tilesetString = tilesetFileReader.ReadToEnd(); // You can replace this with any custom function - // to load data from any source, eg. .zip file. - using var tilesetReader = new TilesetReader(tilesetString, ResolveTileset, ResolveTemplate, ResolveCustomType); // Parse loaded tileset. + // Read a file from assembly + // You can use any other source for files, eg. compressed archive, or even file from internet. + using Stream? tilesetStream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"DotTiled.Example.embedded-{source}") + ?? throw new FileLoadException($"{source} not found in assembly."); + string tilesetString = new StreamReader(tilesetStream).ReadToEnd(); + + using TilesetReader tilesetReader = new TilesetReader(tilesetString, ResolveTileset, ResolveTemplate, ResolveCustomType); // Parse loaded tileset. return tilesetReader.ReadTileset(); // Return loaded tileset } // This is pretty similar to above, but instead it loads templates, not tilesets. - static Template ResolveTemplate(string source) + private static Template ResolveTemplate(string source) { - string templatePath = Path.Combine(Directory.GetCurrentDirectory(), source); - using var templateFileReader = new StreamReader(templatePath); - var templateString = templateFileReader.ReadToEnd(); - using var templateReader = new TemplateReader(templateString, ResolveTileset, ResolveTemplate, ResolveCustomType); + using Stream? templateStream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"DotTiled.Example.{source}") + ?? throw new FileLoadException($"{source} not found in assembly."); + string templateString = new StreamReader(templateStream).ReadToEnd(); + + using TemplateReader templateReader = new TemplateReader(templateString, ResolveTileset, ResolveTemplate, ResolveCustomType); return templateReader.ReadTemplate(); } - static ICustomTypeDefinition ResolveCustomType(string name) + private static ICustomTypeDefinition ResolveCustomType(string name) { - CustomClassDefinition[] allDefinedTypes = - [ - new CustomClassDefinition() { Name = "a" }, - ]; - return allDefinedTypes.FirstOrDefault(type => type.Name == name); + ICustomTypeDefinition[] allDefinedTypes = + [ + new CustomClassDefinition() { Name = "a" }, + ]; + return allDefinedTypes.FirstOrDefault(type => type.Name == name) ?? throw new InvalidOperationException(); } - } diff --git a/src/DotTiled.Example/embedded-tilemap.tmx b/src/DotTiled.Example/embedded-tilemap.tmx new file mode 100644 index 0000000..7a9a294 --- /dev/null +++ b/src/DotTiled.Example/embedded-tilemap.tmx @@ -0,0 +1,12 @@ + + + + + + + + + AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAA== + + + diff --git a/src/DotTiled.Example/embedded-tileset.png b/src/DotTiled.Example/embedded-tileset.png new file mode 100644 index 0000000000000000000000000000000000000000..67b9eeea3c0801c3a7092d76b229e2560ee5d7c0 GIT binary patch literal 149 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1qucK`l=g#}Etu})QSZQ qC$3zWaLH~Bhp(&*Z!NEp7Q<%lsS3|M&zb^tF?hQAxvX + + + From 3d649fab95a6e4cbd210214b06b36eeb91ba61a4 Mon Sep 17 00:00:00 2001 From: krnlException Date: Thu, 12 Sep 2024 20:23:43 +0200 Subject: [PATCH 4/8] Program class to public and top-level warning disabled --- .editorconfig | 1 + src/DotTiled.Example/Program.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 8e747e9..a784f75 100644 --- a/.editorconfig +++ b/.editorconfig @@ -237,6 +237,7 @@ dotnet_diagnostic.IDE0008.severity = silent dotnet_diagnostic.IDE0055.severity = silent dotnet_diagnostic.IDE0058.severity = silent dotnet_diagnostic.IDE0160.severity = none +dotnet_diagnostic.IDE0210.severity = none dotnet_diagnostic.CA1707.severity = silent dotnet_diagnostic.CA1852.severity = none dotnet_diagnostic.CA1805.severity = none diff --git a/src/DotTiled.Example/Program.cs b/src/DotTiled.Example/Program.cs index 785369b..234012a 100644 --- a/src/DotTiled.Example/Program.cs +++ b/src/DotTiled.Example/Program.cs @@ -3,7 +3,7 @@ using DotTiled.Serialization; namespace DotTiled.Example; -class Program +public class Program { private static void Main() { From 0515ba3256dd517618f18735ccbf432e3a1c0072 Mon Sep 17 00:00:00 2001 From: krnlexception Date: Fri, 13 Sep 2024 19:32:24 +0200 Subject: [PATCH 5/8] Godot example --- .../DotTiled.Example.Console.csproj} | 2 +- .../DotTiled.Example.Console}/Program.cs | 0 .../embedded-tilemap.tmx | 0 .../embedded-tileset.png | Bin .../embedded-tileset.tsx | 0 .../DotTiled.Example.Console}/tilemap.tmx | 0 .../DotTiled.Example.Console}/tileset.png | Bin .../DotTiled.Example.Console}/tileset.tsx | 0 .../DotTiled.Example.Godot/.gitattributes | 2 + .../DotTiled.Example.Godot/.gitignore | 2 + .../DotTiled.Example.Godot.csproj | 11 +++ .../DotTiled.Example.Godot.csproj.old | 11 +++ .../DotTiled.Example.Godot.sln | 19 +++++ .../DotTiled.Example.Godot/MapParser.cs | 68 ++++++++++++++++++ .../DotTiled.Example.Godot/blocks/1.tscn | 9 +++ .../DotTiled.Example.Godot/icon.svg | 1 + .../DotTiled.Example.Godot/icon.svg.import | 37 ++++++++++ .../DotTiled.Example.Godot/main.tscn | 6 ++ .../DotTiled.Example.Godot/project.godot | 20 ++++++ .../DotTiled.Example.Godot/tilemap.tmx | 12 ++++ .../DotTiled.Example.Godot/tileset.png | Bin 0 -> 149 bytes .../DotTiled.Example.Godot/tileset.png.import | 34 +++++++++ .../DotTiled.Example.Godot/tileset.tsx | 4 ++ src/DotTiled.sln | 22 ++++-- 24 files changed, 254 insertions(+), 6 deletions(-) rename src/{DotTiled.Example/DotTiled.Example.csproj => DotTiled.Examples/DotTiled.Example.Console/DotTiled.Example.Console.csproj} (94%) rename src/{DotTiled.Example => DotTiled.Examples/DotTiled.Example.Console}/Program.cs (100%) rename src/{DotTiled.Example => DotTiled.Examples/DotTiled.Example.Console}/embedded-tilemap.tmx (100%) rename src/{DotTiled.Example => DotTiled.Examples/DotTiled.Example.Console}/embedded-tileset.png (100%) rename src/{DotTiled.Example => DotTiled.Examples/DotTiled.Example.Console}/embedded-tileset.tsx (100%) rename src/{DotTiled.Example => DotTiled.Examples/DotTiled.Example.Console}/tilemap.tmx (100%) rename src/{DotTiled.Example => DotTiled.Examples/DotTiled.Example.Console}/tileset.png (100%) rename src/{DotTiled.Example => DotTiled.Examples/DotTiled.Example.Console}/tileset.tsx (100%) create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/.gitattributes create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/.gitignore create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj.old create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.sln create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/MapParser.cs create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/blocks/1.tscn create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/icon.svg create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/icon.svg.import create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/main.tscn create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/project.godot create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/tilemap.tmx create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/tileset.png create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/tileset.png.import create mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/tileset.tsx diff --git a/src/DotTiled.Example/DotTiled.Example.csproj b/src/DotTiled.Examples/DotTiled.Example.Console/DotTiled.Example.Console.csproj similarity index 94% rename from src/DotTiled.Example/DotTiled.Example.csproj rename to src/DotTiled.Examples/DotTiled.Example.Console/DotTiled.Example.Console.csproj index b1e806e..c9bf1a8 100644 --- a/src/DotTiled.Example/DotTiled.Example.csproj +++ b/src/DotTiled.Examples/DotTiled.Example.Console/DotTiled.Example.Console.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/DotTiled.Example/Program.cs b/src/DotTiled.Examples/DotTiled.Example.Console/Program.cs similarity index 100% rename from src/DotTiled.Example/Program.cs rename to src/DotTiled.Examples/DotTiled.Example.Console/Program.cs diff --git a/src/DotTiled.Example/embedded-tilemap.tmx b/src/DotTiled.Examples/DotTiled.Example.Console/embedded-tilemap.tmx similarity index 100% rename from src/DotTiled.Example/embedded-tilemap.tmx rename to src/DotTiled.Examples/DotTiled.Example.Console/embedded-tilemap.tmx diff --git a/src/DotTiled.Example/embedded-tileset.png b/src/DotTiled.Examples/DotTiled.Example.Console/embedded-tileset.png similarity index 100% rename from src/DotTiled.Example/embedded-tileset.png rename to src/DotTiled.Examples/DotTiled.Example.Console/embedded-tileset.png diff --git a/src/DotTiled.Example/embedded-tileset.tsx b/src/DotTiled.Examples/DotTiled.Example.Console/embedded-tileset.tsx similarity index 100% rename from src/DotTiled.Example/embedded-tileset.tsx rename to src/DotTiled.Examples/DotTiled.Example.Console/embedded-tileset.tsx diff --git a/src/DotTiled.Example/tilemap.tmx b/src/DotTiled.Examples/DotTiled.Example.Console/tilemap.tmx similarity index 100% rename from src/DotTiled.Example/tilemap.tmx rename to src/DotTiled.Examples/DotTiled.Example.Console/tilemap.tmx diff --git a/src/DotTiled.Example/tileset.png b/src/DotTiled.Examples/DotTiled.Example.Console/tileset.png similarity index 100% rename from src/DotTiled.Example/tileset.png rename to src/DotTiled.Examples/DotTiled.Example.Console/tileset.png diff --git a/src/DotTiled.Example/tileset.tsx b/src/DotTiled.Examples/DotTiled.Example.Console/tileset.tsx similarity index 100% rename from src/DotTiled.Example/tileset.tsx rename to src/DotTiled.Examples/DotTiled.Example.Console/tileset.tsx diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/.gitattributes b/src/DotTiled.Examples/DotTiled.Example.Godot/.gitattributes new file mode 100644 index 0000000..8ad74f7 --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/.gitattributes @@ -0,0 +1,2 @@ +# Normalize EOL for all files that Git considers text files. +* text=auto eol=lf diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/.gitignore b/src/DotTiled.Examples/DotTiled.Example.Godot/.gitignore new file mode 100644 index 0000000..4709183 --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/.gitignore @@ -0,0 +1,2 @@ +# Godot 4+ specific ignores +.godot/ diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj b/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj new file mode 100644 index 0000000..99e4b77 --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj @@ -0,0 +1,11 @@ + + + net8.0 + net7.0 + net8.0 + true + + + + + \ No newline at end of file diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj.old b/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj.old new file mode 100644 index 0000000..a7de93f --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj.old @@ -0,0 +1,11 @@ + + + net8.0 + net7.0 + net8.0 + true + + + + + diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.sln b/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.sln new file mode 100644 index 0000000..c707fa2 --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.sln @@ -0,0 +1,19 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Example.Godot", "DotTiled.Example.Godot.csproj", "{61468FCF-ACC1-4E3B-B4B4-270279E45BF5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + ExportDebug|Any CPU = ExportDebug|Any CPU + ExportRelease|Any CPU = ExportRelease|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {61468FCF-ACC1-4E3B-B4B4-270279E45BF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {61468FCF-ACC1-4E3B-B4B4-270279E45BF5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {61468FCF-ACC1-4E3B-B4B4-270279E45BF5}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU + {61468FCF-ACC1-4E3B-B4B4-270279E45BF5}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU + {61468FCF-ACC1-4E3B-B4B4-270279E45BF5}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU + {61468FCF-ACC1-4E3B-B4B4-270279E45BF5}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU + EndGlobalSection +EndGlobal diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/MapParser.cs b/src/DotTiled.Examples/DotTiled.Example.Godot/MapParser.cs new file mode 100644 index 0000000..5ad960b --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/MapParser.cs @@ -0,0 +1,68 @@ +using System; +using System.Globalization; +using System.Linq; +using DotTiled.Serialization; +using Godot; + +namespace DotTiled.Example.Godot; +public partial class MapParser : Node2D +{ + public override void _Ready() + { + // Load map + var mapString = FileAccess.Open("res://tilemap.tmx", FileAccess.ModeFlags.Read).GetAsText(); //Get file from Godot filesystem + using var mapReader = new MapReader(mapString, ResolveTileset, ResolveTemplate, ResolveCustomType); + var map = mapReader.ReadMap(); + + TileLayer layer0 = (TileLayer)map.Layers[0]; + + for (int y = 0; y < layer0.Height; y++) + { + for (int x = 0; x < layer0.Width; x++) + { + uint tile = layer0.Data.Value.GlobalTileIDs.Value[(y * layer0.Width) + x]; + if (tile == 0) continue; // If block is 0, i.e. air, then continue + + // Load actual block from Godot resources + Node2D block = (Node2D)GD.Load($"res://blocks/{tile}.tscn").Instantiate(); + + // Calculate where block should be + Vector2I scale = (Vector2I)block.GetNode(tile.ToString(CultureInfo.CurrentCulture)).Scale; + int blockX = (block.GetNode(tile.ToString(CultureInfo.CurrentCulture)).Texture.GetWidth() * scale.X / 2) + + (x * block.GetNode(tile.ToString(CultureInfo.CurrentCulture)).Texture.GetWidth() * scale.X); + int blockY = (block.GetNode(tile.ToString(CultureInfo.CurrentCulture)).Texture.GetHeight() * scale.Y / 2) + + (y * block.GetNode(tile.ToString(CultureInfo.CurrentCulture)).Texture.GetHeight() * scale.Y); + block.Position = new Vector2(blockX, blockY); + + // Add block to current scene + AddChild(block); + GD.Print($"{blockX}, {blockY}: {tile}"); + } + } + } + + private Tileset ResolveTileset(string source) + { + string tilesetString = FileAccess.Open($"res://{source}", FileAccess.ModeFlags.Read).GetAsText(); + using TilesetReader tilesetReader = + new TilesetReader(tilesetString, ResolveTileset, ResolveTemplate, ResolveCustomType); + return tilesetReader.ReadTileset(); + } + + private Template ResolveTemplate(string source) + { + string templateString = FileAccess.Open($"res://{source}", FileAccess.ModeFlags.Read).GetAsText(); + using TemplateReader templateReader = + new TemplateReader(templateString, ResolveTileset, ResolveTemplate, ResolveCustomType); + return templateReader.ReadTemplate(); + } + + private static ICustomTypeDefinition ResolveCustomType(string name) + { + ICustomTypeDefinition[] allDefinedTypes = + [ + new CustomClassDefinition() { Name = "a" }, + ]; + return allDefinedTypes.FirstOrDefault(type => type.Name == name) ?? throw new InvalidOperationException(); + } +} diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/blocks/1.tscn b/src/DotTiled.Examples/DotTiled.Example.Godot/blocks/1.tscn new file mode 100644 index 0000000..8c10a13 --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/blocks/1.tscn @@ -0,0 +1,9 @@ +[gd_scene load_steps=2 format=3 uid="uid://ce10iald4cb3f"] + +[ext_resource type="Texture2D" uid="uid://da08vay832u8c" path="res://tileset.png" id="1_c5fs4"] + +[node name="1" type="Node2D"] + +[node name="1" type="Sprite2D" parent="."] +scale = Vector2(2, 2) +texture = ExtResource("1_c5fs4") diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/icon.svg b/src/DotTiled.Examples/DotTiled.Example.Godot/icon.svg new file mode 100644 index 0000000..3fe4f4a --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/icon.svg @@ -0,0 +1 @@ + diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/icon.svg.import b/src/DotTiled.Examples/DotTiled.Example.Godot/icon.svg.import new file mode 100644 index 0000000..164e9de --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/icon.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://0kywmrvvqqyr" +path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.svg" +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/main.tscn b/src/DotTiled.Examples/DotTiled.Example.Godot/main.tscn new file mode 100644 index 0000000..4fdfc60 --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/main.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://p4rpwsvyslew"] + +[ext_resource type="Script" path="res://MapParser.cs" id="1_xjmxv"] + +[node name="Node2D" type="Node2D"] +script = ExtResource("1_xjmxv") diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/project.godot b/src/DotTiled.Examples/DotTiled.Example.Godot/project.godot new file mode 100644 index 0000000..539b623 --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/project.godot @@ -0,0 +1,20 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="DotTiled.Example.Godot" +run/main_scene="res://main.tscn" +config/features=PackedStringArray("4.3", "C#", "Forward Plus") +config/icon="res://icon.svg" + +[dotnet] + +project/assembly_name="DotTiled.Example.Godot" diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/tilemap.tmx b/src/DotTiled.Examples/DotTiled.Example.Godot/tilemap.tmx new file mode 100644 index 0000000..7a9a294 --- /dev/null +++ b/src/DotTiled.Examples/DotTiled.Example.Godot/tilemap.tmx @@ -0,0 +1,12 @@ + + + + + + + + + AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAA== + + + diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/tileset.png b/src/DotTiled.Examples/DotTiled.Example.Godot/tileset.png new file mode 100644 index 0000000000000000000000000000000000000000..67b9eeea3c0801c3a7092d76b229e2560ee5d7c0 GIT binary patch literal 149 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1qucK`l=g#}Etu})QSZQ qC$3zWaLH~Bhp(&*Z!NEp7Q<%lsS3|M&zb^tF?hQAxvX + + + diff --git a/src/DotTiled.sln b/src/DotTiled.sln index 3eec160..7208481 100644 --- a/src/DotTiled.sln +++ b/src/DotTiled.sln @@ -9,7 +9,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Tests", "DotTiled. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Benchmark", "DotTiled.Benchmark\DotTiled.Benchmark.csproj", "{510F3077-8EA4-47D1-8D01-E2D538F1B899}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Example", "DotTiled.Example\DotTiled.Example.csproj", "{31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{8C54542E-3C2C-486C-9BEF-4C510391AFDA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Example.Console", "DotTiled.Examples\DotTiled.Example.Console\DotTiled.Example.Console.csproj", "{F9892295-6C2C-4ABD-9D6F-2AC81D2C6E67}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Example.Godot", "DotTiled.Examples\DotTiled.Example.Godot\DotTiled.Example.Godot.csproj", "{7541A9B3-43A5-45A7-939E-6F542319D990}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -32,9 +36,17 @@ Global {510F3077-8EA4-47D1-8D01-E2D538F1B899}.Debug|Any CPU.Build.0 = Debug|Any CPU {510F3077-8EA4-47D1-8D01-E2D538F1B899}.Release|Any CPU.ActiveCfg = Release|Any CPU {510F3077-8EA4-47D1-8D01-E2D538F1B899}.Release|Any CPU.Build.0 = Release|Any CPU - {31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Release|Any CPU.Build.0 = Release|Any CPU + {F9892295-6C2C-4ABD-9D6F-2AC81D2C6E67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F9892295-6C2C-4ABD-9D6F-2AC81D2C6E67}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F9892295-6C2C-4ABD-9D6F-2AC81D2C6E67}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F9892295-6C2C-4ABD-9D6F-2AC81D2C6E67}.Release|Any CPU.Build.0 = Release|Any CPU + {7541A9B3-43A5-45A7-939E-6F542319D990}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7541A9B3-43A5-45A7-939E-6F542319D990}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7541A9B3-43A5-45A7-939E-6F542319D990}.Release|Any CPU.ActiveCfg = Debug|Any CPU + {7541A9B3-43A5-45A7-939E-6F542319D990}.Release|Any CPU.Build.0 = Debug|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {F9892295-6C2C-4ABD-9D6F-2AC81D2C6E67} = {8C54542E-3C2C-486C-9BEF-4C510391AFDA} + {7541A9B3-43A5-45A7-939E-6F542319D990} = {8C54542E-3C2C-486C-9BEF-4C510391AFDA} EndGlobalSection EndGlobal From f72cfd397bdabbaaa4886eda9797085693fee490 Mon Sep 17 00:00:00 2001 From: krnlException Date: Mon, 16 Sep 2024 16:33:07 +0200 Subject: [PATCH 6/8] Removed backup file --- .../DotTiled.Example.Godot.csproj.old | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj.old diff --git a/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj.old b/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj.old deleted file mode 100644 index a7de93f..0000000 --- a/src/DotTiled.Examples/DotTiled.Example.Godot/DotTiled.Example.Godot.csproj.old +++ /dev/null @@ -1,11 +0,0 @@ - - - net8.0 - net7.0 - net8.0 - true - - - - - From 50c14011bc0fc97835511cee26f496613b43201a Mon Sep 17 00:00:00 2001 From: dcronqvist Date: Sat, 28 Sep 2024 19:52:28 +0200 Subject: [PATCH 7/8] Remove text about MonoGame support --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d9de13..fa181c4 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Other similar libraries exist, and you may want to consider them for your projec > [!NOTE] > *Both benchmark time and memory ratios are relative to DotTiled. Lower is better. Benchmark (time) refers to the execution time of loading the same map from an in-memory string that contains XML data in the `.tmx` format. Benchmark (memory) refers to the memory allocated during that loading process. For further details on the benchmark results, see the collapsible section below. -[MonoGame](https://www.monogame.net) users may also want to consider using [MonoGame.Extended](https://github.com/craftworkgames/MonoGame.Extended) for loading Tiled maps and tilesets. Like MonoGame.Extended, DotTiled also provides a way to properly import Tiled maps and tilesets with the MonoGame content pipeline (with the DotTiled.MonoGame.Pipeline NuGet). However, unlike MonoGame.Extended, DotTiled does *not* include any kind of rendering capabilities, and it is up to you as a developer to implement any kind of rendering for your maps when using DotTiled. The feature coverage by MonoGame.Extended is less than that of DotTiled, so you may want to consider using DotTiled if you need access to more Tiled features and flexibility. +[MonoGame](https://www.monogame.net) users may also want to consider using [MonoGame.Extended](https://github.com/craftworkgames/MonoGame.Extended) for loading Tiled maps and tilesets. The feature coverage by MonoGame.Extended is less than that of DotTiled, so you may want to consider using DotTiled if you need access to more Tiled features and flexibility.
From e8dc677341f684bf38ef4b3b8ed047d196c7de0e Mon Sep 17 00:00:00 2001 From: dcronqvist Date: Sat, 28 Sep 2024 19:55:06 +0200 Subject: [PATCH 8/8] Update README that is published with NuGet to reflect MonoGame support --- src/DotTiled/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DotTiled/README.md b/src/DotTiled/README.md index cd16fe2..692a997 100644 --- a/src/DotTiled/README.md +++ b/src/DotTiled/README.md @@ -23,7 +23,7 @@ Other similar libraries exist, and you may want to consider them for your projec > *Both benchmark time and memory ratios are relative to DotTiled. Lower is better. Benchmark (time) refers to the execution time of loading the same map from an in-memory string that contains XML data in the `.tmx` format. Benchmark (memory) refers to the memory allocated during that loading process. -[MonoGame](https://www.monogame.net) users may also want to consider using [MonoGame.Extended](https://github.com/craftworkgames/MonoGame.Extended) for loading Tiled maps and tilesets. Like MonoGame.Extended, DotTiled also provides a way to properly import Tiled maps and tilesets with the MonoGame content pipeline (with the DotTiled.MonoGame.Pipeline NuGet). However, unlike MonoGame.Extended, DotTiled does *not* include any kind of rendering capabilities, and it is up to you as a developer to implement any kind of rendering for your maps when using DotTiled. The feature coverage by MonoGame.Extended is less than that of DotTiled, so you may want to consider using DotTiled if you need access to more Tiled features and flexibility. +[MonoGame](https://www.monogame.net) users may also want to consider using [MonoGame.Extended](https://github.com/craftworkgames/MonoGame.Extended) for loading Tiled maps and tilesets. The feature coverage by MonoGame.Extended is less than that of DotTiled, so you may want to consider using DotTiled if you need access to more Tiled features and flexibility. # Feature coverage comparison