From 0515ba3256dd517618f18735ccbf432e3a1c0072 Mon Sep 17 00:00:00 2001 From: krnlexception Date: Fri, 13 Sep 2024 19:32:24 +0200 Subject: [PATCH] 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