Skip to content

Commit e467334

Browse files
committed
Added .ldtkl file support
1 parent 203e8c9 commit e467334

File tree

4 files changed

+38
-30
lines changed

4 files changed

+38
-30
lines changed

LDtkMonogame.Examples/Source/LDtkExample.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ public class LDtkExample : Game
1919
private Vector3 cameraOrigin;
2020
private float cameraZoom = 1f;
2121

22-
private int currentLevel = 3;
22+
private int currentLevel = 0;
23+
private KeyboardState oldKeyboard;
24+
private readonly string filename = "SeparateLevelFiles";
2325

24-
KeyboardState oldKeyboard;
25-
26-
string filename = "SeparateLevelFiles";
2726
public LDtkExample()
2827
{
2928
graphics = new GraphicsDeviceManager(this);
@@ -111,7 +110,7 @@ protected override void Draw(GameTime gameTime)
111110

112111
spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: Matrix.CreateTranslation(cameraPosition) * Matrix.CreateScale(cameraZoom) * Matrix.CreateTranslation(cameraOrigin));
113112
{
114-
if(level.Background != null)
113+
if(level.Background.Image != null)
115114
{
116115
spriteBatch.Draw(level.Background.Image, level.Background.TopLeft, level.Background.CropRect, Color.White, 0, Vector2.Zero, level.Background.Scale, SpriteEffects.None, 0);
117116
}

LDtkMonogame/Source/LdtkJson.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// <auto-generated />
2-
//
3-
// 0.7.0
4-
//
2+
// LDtk version 0.7.1
53
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
64
//
7-
// using ldtk;
5+
// using LDtk.Internal;
86
//
97
// var ldtkJson = LdtkJson.FromJson(jsonString);
108

@@ -308,7 +306,7 @@ public partial class FieldDefinition
308306
/// Default value if selected value is null or invalid.
309307
/// </summary>
310308
[JsonProperty("defaultOverride")]
311-
public dynamic DefaultOverride { get; set; }
309+
public object DefaultOverride { get; set; }
312310

313311
[JsonProperty("editorAlwaysShow")]
314312
public bool EditorAlwaysShow { get; set; }
@@ -361,7 +359,7 @@ public partial class FieldDefinition
361359
/// Internal type enum
362360
/// </summary>
363361
[JsonProperty("type")]
364-
public dynamic FieldDefinitionType { get; set; }
362+
public object FieldDefinitionType { get; set; }
365363

366364
/// <summary>
367365
/// Unique Intidentifier
@@ -440,7 +438,7 @@ public partial class LayerDefinition
440438
/// Contains all the auto-layer rule definitions.
441439
/// </summary>
442440
[JsonProperty("autoRuleGroups")]
443-
public Dictionary<string, dynamic>[] AutoRuleGroups { get; set; }
441+
public Dictionary<string, object>[] AutoRuleGroups { get; set; }
444442

445443
[JsonProperty("autoSourceLayerDefUid")]
446444
public long? AutoSourceLayerDefUid { get; set; }
@@ -551,7 +549,7 @@ public partial class TilesetDefinition
551549
/// source image changes.
552550
/// </summary>
553551
[JsonProperty("cachedPixelData")]
554-
public Dictionary<string, dynamic> CachedPixelData { get; set; }
552+
public Dictionary<string, object> CachedPixelData { get; set; }
555553

556554
/// <summary>
557555
/// Unique String identifier
@@ -587,7 +585,7 @@ public partial class TilesetDefinition
587585
/// Array of group of tiles selections, only meant to be used in the editor
588586
/// </summary>
589587
[JsonProperty("savedSelections")]
590-
public Dictionary<string, dynamic>[] SavedSelections { get; set; }
588+
public Dictionary<string, object>[] SavedSelections { get; set; }
591589

592590
/// <summary>
593591
/// Space in pixels between all tiles
@@ -969,7 +967,7 @@ public partial class FieldInstance
969967
/// (Integer, Boolean, String etc.)<br/> It can also be an `Array` of those same types.
970968
/// </summary>
971969
[JsonProperty("__value")]
972-
public dynamic Value { get; set; }
970+
public object Value { get; set; }
973971

974972
/// <summary>
975973
/// Reference of the **Field definition** UID
@@ -981,7 +979,7 @@ public partial class FieldInstance
981979
/// Editor internal raw values
982980
/// </summary>
983981
[JsonProperty("realEditorValues")]
984-
public dynamic[] RealEditorValues { get; set; }
982+
public object[] RealEditorValues { get; set; }
985983
}
986984

987985
/// <summary>
@@ -1523,4 +1521,4 @@ public override void WriteJson(JsonWriter writer, object untypedValue, JsonSeria
15231521

15241522
public static readonly WorldLayoutConverter Singleton = new WorldLayoutConverter();
15251523
}
1526-
}
1524+
}

LDtkMonogame/Source/Level.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public struct Level
2424
/// <summary>
2525
/// The background image class
2626
/// </summary>
27-
public class Background
27+
public struct Background
2828
{
2929
/// <summary>
3030
/// The croped texture if your settings caused it to

LDtkMonogame/Source/Project.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,43 @@ public void Render(int levelId)
4747
{
4848
GraphicsDevice GraphicsDevice = spriteBatch.GraphicsDevice;
4949

50+
Internal.Level level;
5051

52+
if(json.ExternalLevels == true)
53+
{
54+
string path = Path.Combine(absoluteProjectFolder, json.Levels[levelId].ExternalRelPath);
55+
level = Newtonsoft.Json.JsonConvert.DeserializeObject<Internal.Level>(File.ReadAllText(path));
56+
}
57+
else
58+
{
59+
level = json.Levels[levelId];
60+
}
5161

5262
// Cache the Background Color
53-
Levels[levelId].BgColor = Utility.ConvertStringToColor(json.Levels[levelId].BgColor);
63+
Levels[levelId].BgColor = Utility.ConvertStringToColor(level.BgColor);
5464

5565
// The current Level has a background
56-
if(json.Levels[levelId].BgRelPath != null)
66+
if(level.BgRelPath != null)
5767
{
58-
Levels[levelId].Background = new Background();
68+
Background background = Levels[levelId].Background;
69+
70+
background.Image = Texture2D.FromFile(GraphicsDevice, Path.Combine(absoluteProjectFolder, level.BgRelPath));
5971

60-
Levels[levelId].Background.Image = Texture2D.FromFile(GraphicsDevice, Path.Combine(absoluteProjectFolder, json.Levels[levelId].BgRelPath));
72+
long[] topleft = level.BgPos.TopLeftPx;
73+
background.TopLeft = new Vector2(topleft[0], topleft[1]);
6174

62-
long[] topleft = json.Levels[levelId].BgPos.TopLeftPx;
63-
Levels[levelId].Background.TopLeft = new Vector2(topleft[0], topleft[1]);
75+
double[] rect = level.BgPos.CropRect;
76+
background.CropRect = new Rectangle((int)rect[0], (int)rect[1], (int)rect[2], (int)rect[3]);
6477

65-
double[] rect = json.Levels[levelId].BgPos.CropRect;
66-
Levels[levelId].Background.CropRect = new Rectangle((int)rect[0], (int)rect[1], (int)rect[2], (int)rect[3]);
78+
var scale = level.BgPos.Scale;
79+
background.Scale = new Vector2((float)scale[0], (float)scale[1]);
6780

68-
var scale = json.Levels[levelId].BgPos.Scale;
69-
Levels[levelId].Background.Scale = new Vector2((float)scale[0], (float)scale[1]);
81+
Levels[levelId].Background = background;
7082
}
7183

72-
LayerInstance[] jsonLayerInstances = json.Levels[levelId].LayerInstances;
84+
LayerInstance[] jsonLayerInstances = level.LayerInstances;
7385
Levels[levelId].Layers = new RenderTarget2D[jsonLayerInstances.Length];
7486

75-
7687
for(int i = jsonLayerInstances.Length - 1; i >= 0; i--)
7788
{
7889
LayerInstance jsonLayer = jsonLayerInstances[i];

0 commit comments

Comments
 (0)