add initial implementation of LDtk importer with data models and scene builder

This commit is contained in:
2025-09-23 01:54:37 +02:00
parent faa4715250
commit d92e2b004e
22 changed files with 1710 additions and 3 deletions

View File

@@ -1,6 +1,29 @@
using Godot;
using System;
public partial class LdtkImporter : Node
namespace CSharpLdtkImporter;
[Tool]
public partial class LdtkImporter : EditorPlugin
{
private LdtkResourceImporter _importer;
public override void _EnterTree()
{
GD.Print("LdtkImporter: Plugin has been enabled.");
_importer = new LdtkResourceImporter();
AddImportPlugin(_importer);
GD.Print("LdtkImporter: Import plugin registered.");
}
public override void _ExitTree()
{
RemoveImportPlugin(_importer);
_importer = null;
GD.Print("LdtkImporter: Import plugin unregistered.");
GD.Print("LdtkImporter: Plugin has been disabled.");
}
}

View File

@@ -0,0 +1,95 @@
using System.Text.Json;
using CSharpLdtkImporter.Models;
using Godot;
using Godot.Collections;
namespace CSharpLdtkImporter;
[Tool]
public partial class LdtkResourceImporter : EditorImportPlugin
{
public override string _GetImporterName() => "ldtk.importer";
public override string _GetVisibleName() => "LDtk Level Importer";
public override string[] _GetRecognizedExtensions() => new string[] { "ldtk" };
public override string _GetResourceType() => "PackedScene";
public override string _GetSaveExtension() => "tscn";
public override int _GetPresetCount() => 0;
public override float _GetPriority() => 1.0f;
public override Error _Import(string sourceFile, string savePath, Godot.Collections.Dictionary options, Godot.Collections.Array<string> platformVariants, Godot.Collections.Array<string> genFiles)
{
// 1. Read the .ldtk file content
using var file = FileAccess.Open(sourceFile, FileAccess.ModeFlags.Read);
if (file == null)
{
GD.PushError($"Failed to open LDTK file: {sourceFile}");
return Error.CantOpen;
}
string jsonContent = file.GetAsText();
// 2. Deserialize the JSON into our C# data models
LdtkData ldtkData;
try
{
ldtkData = JsonSerializer.Deserialize<LdtkData>(jsonContent);
}
catch (JsonException e)
{
GD.PushError($"Failed to parse LDTK JSON: {e.Message}");
return Error.ParseError;
}
if (ldtkData == null)
{
GD.PushError("Parsed LDTK data is null.");
return Error.Failed;
}
// 3. Use the Scene Builder to generate the Godot scene
var builder = new LdtkSceneBuilder(ldtkData, sourceFile);
var rootNode = builder.BuildLdtkProjectRoot();
var scene = new PackedScene();
scene.Pack(rootNode);
var newSceneNodeCount = scene.GetState().GetNodeCount();
GD.Print($"New scene node count: {newSceneNodeCount}, expected: {rootNode.GetChildCount() + 1} (including root)");
// 4. Save the generated scene
var destinationPath = $"{savePath}.{_GetSaveExtension()}";
var error = ResourceSaver.Save(scene, destinationPath);
if (error != Error.Ok)
{
GD.PushError($"Failed to save generated scene to {destinationPath}");
}
return error;
}
public override Array<Dictionary> _GetImportOptions(string path, int presetIndex)
{
var options = new Array<Dictionary>()
{
new()
{
{ "name", "import_entities" },
{ "default_value", true },
{ "usage", (int)(PropertyUsageFlags.Default | PropertyUsageFlags.UpdateAllIfModified) }
},
new()
{
{ "name", "import_tilemaps" },
{ "default_value", true },
{ "usage", (int)(PropertyUsageFlags.Default | PropertyUsageFlags.UpdateAllIfModified) }
}
};
return options;
}
public override bool _GetOptionVisibility(string path, StringName optionName, Dictionary options)
{
return true;
}
}

View File

@@ -0,0 +1 @@
uid://dbybnkjcgt6tn

View File

@@ -0,0 +1,152 @@
using System.Linq;
using CSharpLdtkImporter.Models;
using Godot;
namespace CSharpLdtkImporter;
public class LdtkSceneBuilder
{
private readonly LdtkData _ldtkData;
private readonly string _basePath;
private readonly Godot.Collections.Dictionary<int, TileSet> _tileSetCache = new();
public LdtkSceneBuilder(LdtkData ldtkData, string sourceFile)
{
_ldtkData = ldtkData;
_basePath = sourceFile.GetBaseDir();
}
public Node2D BuildLdtkProjectRoot()
{
var root = new Node2D { Name = "LDTKProject" };
// Step 1: Build the entire node hierarchy in memory.
foreach (var level in _ldtkData.Levels)
{
var levelNode = BuildLevel(level);
root.AddChild(levelNode);
}
// Step 2: After the tree is built, set the owner for all descendants.
// This is the crucial step that fixes the "Invalid owner" error.
SetOwnerRecursive(root, root);
return root;
}
// A helper function to recursively set the owner on all children.
private void SetOwnerRecursive(Node node, Node owner)
{
foreach (var child in node.GetChildren())
{
child.Owner = owner;
SetOwnerRecursive(child, owner);
}
}
private Node2D BuildLevel(LdtkLevel level)
{
var levelRoot = new Node2D { Name = level.Identifier };
foreach (var layer in level.LayerInstances.Reverse())
{
var layerNode = layer.Type switch
{
"Tiles" => BuildTileMapLayer(layer),
"AutoLayer" => BuildTileMapLayer(layer),
"Entities" => BuildEntityLayer(layer),
_ => new Node2D { Name = $"{layer.Identifier}_Unsupported" }
};
levelRoot.AddChild(layerNode);
}
return levelRoot;
}
private TileMapLayer BuildTileMapLayer(LdtkLayerInstance layer)
{
var tileMapLayer = new TileMapLayer { Name = layer.Identifier };
if (!layer.TilesetDefUid.HasValue) return tileMapLayer;
var tileSet = GetOrCreateTileSet(layer.TilesetDefUid.Value);
if (tileSet == null) return tileMapLayer;
tileMapLayer.TileSet = tileSet;
var allTiles = layer.GridTiles.Concat(layer.AutoLayerTiles);
if (tileMapLayer.TileSet.GetSource(0) is not TileSetAtlasSource atlasSource) return tileMapLayer;
int atlasWidthInTiles = atlasSource.GetAtlasGridSize().X;
if (atlasWidthInTiles == 0) return tileMapLayer;
foreach (var tile in allTiles)
{
var gridCoords = new Vector2I(tile.Px[0] / layer.GridSize, tile.Px[1] / layer.GridSize);
var atlasCoords = new Vector2I(tile.TileId % atlasWidthInTiles, tile.TileId / atlasWidthInTiles);
long alternativeId = 0;
if ((tile.FlipBits & 1) == 1) alternativeId |= TileSetAtlasSource.TransformFlipH;
if ((tile.FlipBits & 2) == 2) alternativeId |= TileSetAtlasSource.TransformFlipV;
tileMapLayer.SetCell(gridCoords, 0, atlasCoords, (int)alternativeId);
}
return tileMapLayer;
}
private Node2D BuildEntityLayer(LdtkLayerInstance layer)
{
var entityLayerRoot = new Node2D { Name = layer.Identifier };
foreach (var entity in layer.EntityInstances)
{
var marker = new Marker2D
{
Name = entity.Identifier,
Position = new Vector2(entity.Px[0], entity.Px[1])
};
entityLayerRoot.AddChild(marker);
}
return entityLayerRoot;
}
private TileSet GetOrCreateTileSet(int tilesetDefUid)
{
if (_tileSetCache.TryGetValue(tilesetDefUid, out var cachedTileSet)) return cachedTileSet;
var tilesetDef = _ldtkData.Defs.Tilesets.FirstOrDefault(t => t.Uid == tilesetDefUid);
if (tilesetDef?.RelPath == null) return null;
var texturePath = _basePath.PathJoin(tilesetDef.RelPath);
var texture = ResourceLoader.Load<Texture2D>(texturePath);
if (texture == null)
{
GD.PushError($"LDTK Importer: Could not load texture at path: {texturePath}");
return null;
}
var newTileSet = new TileSet
{
TileShape = TileSet.TileShapeEnum.Square,
TileLayout = TileSet.TileLayoutEnum.Stacked,
TileSize = new Vector2I(tilesetDef.TileGridSize, tilesetDef.TileGridSize)
};
var atlasSource = new TileSetAtlasSource
{
Texture = texture,
TextureRegionSize = new Vector2I(tilesetDef.TileGridSize, tilesetDef.TileGridSize)
};
newTileSet.AddSource(atlasSource);
var (widthInTiles, heightInTiles) = (texture.GetWidth() / tilesetDef.TileGridSize, texture.GetHeight() / tilesetDef.TileGridSize);
for (int x = 0; x < widthInTiles; x++)
{
for (int y = 0; y < heightInTiles; y++)
{
atlasSource.CreateTile(new Vector2I(x, y));
}
}
_tileSetCache[tilesetDefUid] = newTileSet;
return newTileSet;
}
}

View File

@@ -0,0 +1 @@
uid://nf7qcxuuaajw

View File

@@ -0,0 +1,30 @@
using System.Text.Json.Serialization;
namespace CSharpLdtkImporter.Models;
public class LdtkData
{
[JsonPropertyName("levels")]
public LdtkLevel[] Levels { get; set; }
[JsonPropertyName("defs")]
public LdtkDefinitions Defs { get; set; }
}
public class LdtkDefinitions
{
[JsonPropertyName("tilesets")]
public LdtkTilesetDef[] Tilesets { get; set; }
}
public class LdtkTilesetDef
{
[JsonPropertyName("uid")]
public int Uid { get; set; }
[JsonPropertyName("relPath")]
public string RelPath { get; set; }
[JsonPropertyName("tileGridSize")]
public int TileGridSize { get; set; }
}

View File

@@ -0,0 +1 @@
uid://8f7a424k36sb

View File

@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;
namespace CSharpLdtkImporter.Models;
public class LdtkEntityInstance
{
[JsonPropertyName("__identifier")]
public string Identifier { get; set; }
[JsonPropertyName("px")]
public int[] Px { get; set; }
}

View File

@@ -0,0 +1 @@
uid://ck2tr052fmrx2

View File

@@ -0,0 +1,27 @@
using System.Text.Json.Serialization;
namespace CSharpLdtkImporter.Models;
public class LdtkLayerInstance
{
[JsonPropertyName("__identifier")]
public string Identifier { get; set; }
[JsonPropertyName("__type")]
public string Type { get; set; }
[JsonPropertyName("__gridSize")]
public int GridSize { get; set; }
[JsonPropertyName("__tilesetDefUid")]
public int? TilesetDefUid { get; set; } // Nullable for entity layers
[JsonPropertyName("gridTiles")]
public LdtkTileInstance[] GridTiles { get; set; }
[JsonPropertyName("autoLayerTiles")]
public LdtkTileInstance[] AutoLayerTiles { get; set; }
[JsonPropertyName("entityInstances")]
public LdtkEntityInstance[] EntityInstances { get; set; }
}

View File

@@ -0,0 +1 @@
uid://crre3v7sfnqq6

View File

@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;
namespace CSharpLdtkImporter.Models;
public class LdtkLevel
{
[JsonPropertyName("identifier")]
public string Identifier { get; set; }
[JsonPropertyName("layerInstances")]
public LdtkLayerInstance[] LayerInstances { get; set; }
}

View File

@@ -0,0 +1 @@
uid://c5kc2tyb2kf1e

View File

@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;
namespace CSharpLdtkImporter.Models;
public class LdtkTileInstance
{
// Pixel coordinates [x,y]
[JsonPropertyName("px")]
public int[] Px { get; set; }
// Tile ID in the tileset
[JsonPropertyName("t")]
public int TileId { get; set; }
// Flip bits (0=none, 1=X, 2=Y, 3=X&Y)
[JsonPropertyName("f")]
public int FlipBits { get; set; }
}

View File

@@ -0,0 +1 @@
uid://dal7woqq2nlrd

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

@@ -0,0 +1,40 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://c4ssxvikdpnld"
path="res://.godot/imported/PS_Tileset_10_nes.png-4155769689f4e11687b219cee5a0f90d.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://assets/PS_Tileset_10_nes.png"
dest_files=["res://.godot/imported/PS_Tileset_10_nes.png-4155769689f4e11687b219cee5a0f90d.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/uastc_level=0
compress/rdo_quality_loss=0.0
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/channel_remap/red=0
process/channel_remap/green=1
process/channel_remap/blue=2
process/channel_remap/alpha=3
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

1267
assets/test.ldtk Normal file

File diff suppressed because it is too large Load Diff

16
assets/test.ldtk.import Normal file
View File

@@ -0,0 +1,16 @@
[remap]
importer="ldtk.importer"
type="PackedScene"
uid="uid://c4xhtpmir26ah"
path="res://.godot/imported/test.ldtk-5da58debf60b2ac4adee332e85c3c4bc.tscn"
[deps]
source_file="res://assets/test.ldtk"
dest_files=["res://.godot/imported/test.ldtk-5da58debf60b2ac4adee332e85c3c4bc.tscn"]
[params]
import_entities=true
import_tilemaps=true

View File

@@ -11,13 +11,18 @@ config_version=5
[application]
config/name="ldtk-importer"
config/features=PackedStringArray("4.5", "GL Compatibility")
run/main_scene="uid://c1hl3wapfm8dh"
config/features=PackedStringArray("4.5", "C#", "GL Compatibility")
config/icon="res://icon.svg"
[dotnet]
project/assembly_name="ldtk-importer"
[editor_plugins]
enabled=PackedStringArray("res://addons/csharp_ldtk_importer/plugin.cfg")
[rendering]
renderer/rendering_method="gl_compatibility"

3
scenes/world.tscn Normal file
View File

@@ -0,0 +1,3 @@
[gd_scene format=3 uid="uid://c1hl3wapfm8dh"]
[node name="World" type="Node2D"]