Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion LDtk.Codegen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace LDtk.Codegen;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using CommandLine;
Expand Down
2 changes: 1 addition & 1 deletion LDtk.JsonSchema/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static async Task<int> Main()
};

string[] ignoreFields = {
"__FORCED_REFS",
"_FORCED_REFS",
"LevelFields"
};

Expand Down
34 changes: 32 additions & 2 deletions LDtk/JsonPartials/LDtkFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ namespace LDtk;
[DebuggerDisplay("ExternalFiles: {ExternalLevels} Path: {FilePath}")]
public partial class LDtkFile
{
/// <inheritdoc cref="Definitions.Entities"/>
public EntityDefinition[] Entities => Defs.Entities;
/// <inheritdoc cref="Definitions.Enums"/>
public EnumDefinition[] Enums => Defs.Enums;
/// <inheritdoc cref="Definitions.ExternalEnums"/>
public EnumDefinition[] ExternalEnums => Defs.ExternalEnums;
/// <inheritdoc cref="Definitions.Layers"/>
public LayerDefinition[] Layers => Defs.Layers;
/// <inheritdoc cref="Definitions.Tilesets"/>
public TilesetDefinition[] Tilesets => Defs.Tilesets;

/// <summary> Initializes a new instance of the <see cref="LDtkFile"/> class. Used by json deserializer not for use by user. </summary>
public LDtkFile() { }

Expand All @@ -25,6 +36,7 @@ public LDtkFile() { }
/// <summary> Loads the ldtk world file from disk directly using json source generator. </summary>
/// <param name="filePath"> Path to the .ldtk file. </param>
/// <returns> Returns the file loaded from the path. </returns>
/// <exception cref="LDtkException">Failed to Deserialize ldtk file from</exception>
public static LDtkFile FromFile(string filePath)
{
LDtkFile? file = JsonSerializer.Deserialize(File.ReadAllText(filePath), Constants.JsonSourceGenerator.LDtkFile);
Expand All @@ -39,6 +51,7 @@ public static LDtkFile FromFile(string filePath)
/// <summary> Loads the ldtk world file from disk directly. </summary>
/// <param name="filePath"> Path to the .ldtk file. </param>
/// <returns> Returns the file loaded from the path. </returns>
/// <exception cref="LDtkException"> Thrown when the file failed to deserialize. </exception>
public static LDtkFile FromFileReflection(string filePath)
{
LDtkFile? file = JsonSerializer.Deserialize<LDtkFile>(File.ReadAllText(filePath), Constants.SerializeOptions);
Expand All @@ -65,7 +78,8 @@ public static LDtkFile FromFile(string filePath, ContentManager content)
/// <summary> Loads the ldtkl world file from disk directly or from the embeded one depending on if the file uses externalworlds. </summary>
/// <param name="iid" > The iid of the world to load. </param>
/// <returns> Returns the world from the iid. </returns>
public LDtkWorld? LoadWorld(Guid iid)
/// <exception cref="LDtkException"> Throws if no world with the <paramref name="iid"/> is found. </exception>
public LDtkWorld LoadWorld(Guid iid)
{
foreach (LDtkWorld world in Worlds)
{
Expand All @@ -89,13 +103,14 @@ public static LDtkFile FromFile(string filePath, ContentManager content)
return world;
}

return null;
throw new LDtkException($"No World({iid}) found in this project");
}

/// <summary> Gets an entity from an <paramref name="reference"/> converted to <typeparamref name="T"/>. </summary>
/// <typeparam name="T"> The type to convert the entity to. </typeparam>
/// <param name="reference"> The entityRef to convert. </param>
/// <returns> The converted entity. </returns>
/// <exception cref="LDtkException"> Throws if no entity with the <paramref name="reference"/> is found. </exception>
public T GetEntityRef<T>(EntityReference reference)
where T : new()
{
Expand All @@ -111,4 +126,19 @@ public T GetEntityRef<T>(EntityReference reference)

throw new LDtkException($"No EntityRef of type {typeof(T).Name} found in this level");
}

/// <summary> ToString </summary>
public override string ToString()
{
return $"""
LDtkFile: {JsonVersion}
BgColor: {BgColor}
Defs: {Defs}
ExternalLevels: {ExternalLevels}
Iid: {Iid}
Levels: {Levels}
Toc: {Toc}
Worlds: {Worlds}
""";
}
}
83 changes: 79 additions & 4 deletions LDtk/JsonPartials/LDtkLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace LDtk;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -33,10 +34,12 @@ public partial class LDtkLevel
[JsonIgnore]
public bool Loaded { get; internal set; }

/// <summary>
/// Initializes a new instance of the <see cref="LDtkLevel"/> class. Used by json deserializer not for use by user!. </summary>
/// <summary> Initializes a new instance of the <see cref="LDtkLevel"/> class. Used by json deserializer not for use by user!. </summary>
#pragma warning disable CS8618
public LDtkLevel() { }
public LDtkLevel()
{
layersLookup = new(LayerInstances?.Length ?? 0);
}
#pragma warning restore

/// <summary> Loads the ldtk world file from disk directly using json source generator. </summary>
Expand Down Expand Up @@ -76,6 +79,49 @@ public LDtkLevel() { }
return file;
}

/// <summary> Get the level with an iid. </summary>
/// <param name="content"> Content pipeline </param>
/// <exception cref="LDtkException">No level with identifier found in this world</exception>
public void Load(ContentManager? content = null)
{
if (ExternalRelPath != null)
{
LDtkLevel? level;

if (content != null)
{
level = FromFile(FilePath, content);
}
else
{
level = FromFile(FilePath);
}

if (level == null)
{
throw new LDtkException("External level not found");
}

_BgColor = level._BgColor;
_BgPos = level._BgPos;
BgRelPath = level.BgRelPath;
ExternalRelPath = level.ExternalRelPath;
FieldInstances = level.FieldInstances;
Identifier = level.Identifier;
Iid = level.Iid;
LayerInstances = level.LayerInstances;
_Neighbours = level._Neighbours;
PxHei = level.PxHei;
PxWid = level.PxWid;
Uid = level.Uid;
WorldDepth = level.WorldDepth;
WorldX = level.WorldX;
WorldY = level.WorldY;

Loaded = true;
}
}

/// <summary> Gets an intgrid with the <paramref name="identifier"/> in a <see cref="LDtkLevel"/>. </summary>
/// <param name="identifier"></param>
/// <exception cref="LDtkException">Throws when identifier is not valid</exception>
Expand Down Expand Up @@ -111,6 +157,7 @@ public LDtkIntGrid GetIntGrid(string identifier)
}

/// <summary> Gets the custom fields of the level. </summary>
/// <typeparam name="T"></typeparam>
public T GetCustomFields<T>() where T : new()
{
T levelFields = new();
Expand All @@ -121,6 +168,7 @@ public LDtkIntGrid GetIntGrid(string identifier)
}

/// <summary> Gets one entity of type T in the current level best used with 1 per level constraint. </summary>
/// <typeparam name="T"></typeparam>
/// <exception cref="LDtkException"></exception>
public T GetEntity<T>()
where T : new()
Expand All @@ -140,6 +188,7 @@ public T GetEntity<T>()
}

/// <summary> Gets an entity from an <paramref name="reference"/> converted to <typeparamref name="T"/>. </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reference"></param>
/// <exception cref="LDtkException"></exception>
public T GetEntityRef<T>(EntityReference reference)
Expand Down Expand Up @@ -169,6 +218,7 @@ public T GetEntityRef<T>(EntityReference reference)
}

/// <summary> Gets an array of entities of type <typeparamref name="T"/> in the current level. </summary>
/// <typeparam name="T"></typeparam>
public T[] GetEntities<T>()
where T : new()
{
Expand All @@ -191,7 +241,7 @@ public T[] GetEntities<T>()
}
}

return [.. entities];
return entities.ToArray();
}

T GetEntityFromInstance<T>(EntityInstance entityInstance)
Expand All @@ -218,4 +268,29 @@ public bool Contains(Point point)
{
return point.X >= Position.X && point.Y >= Position.Y && point.X <= Position.X + Size.X && point.Y <= Position.Y + Size.Y;
}

Dictionary<string, LayerInstance> layersLookup;

/// <summary> Gets the layer instance with the given <paramref name="layerIdentifier"/>. </summary>
/// <param name="layerIdentifier">Layer Identifier</param>
/// <exception cref="LDtkException"></exception>
public LayerInstance this[string layerIdentifier]
{
get
{
if (layersLookup.TryGetValue(layerIdentifier, out LayerInstance? layerLookup))
{
return layerLookup;
}

LayerInstance? layer = LayerInstances!.FirstOrDefault(x => x?._Identifier == layerIdentifier, null);
if (layer != null)
{
layersLookup.Add(layerIdentifier, layer);
return layer;
}

throw new LDtkException($"Layer {layerIdentifier} not found in level {Identifier}");
}
}
}
4 changes: 0 additions & 4 deletions LDtk/LDtkJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public partial class LDtkFile
[JsonPropertyName("externalLevels")]
public bool ExternalLevels { get; set; }

/// <summary> This object is not actually used by LDtk. It ONLY exists to force explicit references to all types, to make sure QuickType finds them and integrate all of them. Otherwise, Quicktype will drop types that are not explicitely used. </summary>
[JsonPropertyName("__FORCED_REFS")]
public object _FORCED_REFS { get; set; }

/// <summary> Unique project identifier </summary>
[JsonPropertyName("iid")]
public Guid Iid { get; set; }
Expand Down
Loading