Skip to content

Commit 3035e15

Browse files
committed
Clean up and fixed TODOs
1 parent 3c9f81d commit 3035e15

File tree

6 files changed

+54
-27
lines changed

6 files changed

+54
-27
lines changed

Examples/Shooter/Data/World.ldtk

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@
14181418
{ "coordId": 324, "v": 1 },
14191419
{ "coordId": 325, "v": 4 },
14201420
{ "coordId": 326, "v": 2 },
1421-
{ "coordId": 327, "v": 1 },
1421+
{ "coordId": 329, "v": 5 },
14221422
{ "coordId": 330, "v": 5 },
14231423
{ "coordId": 331, "v": 5 },
14241424
{ "coordId": 332, "v": 5 },
@@ -1433,13 +1433,13 @@
14331433
{ "coordId": 364, "v": 5 },
14341434
{ "coordId": 365, "v": 5 },
14351435
{ "coordId": 366, "v": 5 },
1436-
{ "coordId": 367, "v": 5 },
14371436
{ "coordId": 382, "v": 4 },
14381437
{ "coordId": 391, "v": 4 },
14391438
{ "coordId": 392, "v": 4 },
14401439
{ "coordId": 398, "v": 3 },
14411440
{ "coordId": 402, "v": 5 },
14421441
{ "coordId": 403, "v": 5 },
1442+
{ "coordId": 406, "v": 5 },
14431443
{ "coordId": 407, "v": 5 },
14441444
{ "coordId": 412, "v": 2 },
14451445
{ "coordId": 422, "v": 4 },
@@ -1544,9 +1544,9 @@
15441544
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,6,0,0,0,0,0,0,0,
15451545
5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,
15461546
0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,5,0,0,
1547-
0,0,0,0,0,0,0,0,0,2,5,3,2,0,0,6,6,6,6,6,6,0,0,0,0,0,0,5,0,0,0,0,0,0,0,
1548-
0,5,5,0,0,0,0,0,0,0,0,0,3,6,6,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,
1549-
0,0,0,0,0,0,5,5,0,0,0,0,0,4,0,0,0,6,6,0,0,0,6,0,0,0,0,3,0,0,0,0,0,0,0,
1547+
0,0,0,0,0,0,0,0,0,2,5,3,0,0,6,6,6,6,6,6,6,0,0,0,0,0,0,5,0,0,0,0,0,0,0,
1548+
0,5,5,0,0,0,0,0,0,0,0,0,3,6,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,
1549+
0,0,0,0,0,0,5,5,0,0,0,0,0,4,0,0,0,6,6,0,0,6,6,0,0,0,0,3,0,0,0,0,0,0,0,
15501550
0,0,5,0,0,0,0,0,2,2,6,6,6,6,0,0,0,6,6,6,0,0,7,0,0,0,0,6,0,6,6,6,6,6,0,
15511551
3,0,0,0,3,0,6,6,6,0,3,0,0,6,6,6,0,0,6,6,6,0,7,0,6,0,0,7,0,0,0,0,6,0,7,
15521552
0,0,0,6,6,6,6,0,6,6,6,6,0,6,6,6,6,0,7,0,0,0,0,0,0,6,0,7,0,6,0,0,7,0,0,
@@ -1598,7 +1598,6 @@
15981598
{ "px": [624,288], "src": [16,16], "f": 0, "t": 14, "d": [68,759] },
15991599
{ "px": [560,48], "src": [48,48], "f": 0, "t": 42, "d": [109,155] },
16001600
{ "px": [592,48], "src": [48,48], "f": 0, "t": 42, "d": [109,157] },
1601-
{ "px": [112,128], "src": [48,48], "f": 0, "t": 42, "d": [109,327] },
16021601
{ "px": [464,160], "src": [48,48], "f": 0, "t": 42, "d": [109,429] },
16031602
{ "px": [544,48], "src": [48,48], "f": 0, "t": 42, "d": [59,154] },
16041603
{ "px": [64,128], "src": [48,48], "f": 0, "t": 42, "d": [59,324] },
@@ -1626,12 +1625,12 @@
16261625
{ "px": [352,128], "src": [160,0], "f": 0, "t": 10, "d": [80,342] },
16271626
{ "px": [560,64], "src": [112,0], "f": 0, "t": 7, "d": [91,195] },
16281627
{ "px": [576,64], "src": [112,0], "f": 0, "t": 7, "d": [91,196] },
1628+
{ "px": [160,128], "src": [112,0], "f": 0, "t": 7, "d": [91,330] },
16291629
{ "px": [176,128], "src": [112,0], "f": 0, "t": 7, "d": [91,331] },
16301630
{ "px": [192,128], "src": [112,0], "f": 0, "t": 7, "d": [91,332] },
16311631
{ "px": [208,128], "src": [112,0], "f": 0, "t": 7, "d": [91,333] },
16321632
{ "px": [224,128], "src": [112,0], "f": 0, "t": 7, "d": [91,334] },
16331633
{ "px": [64,144], "src": [112,0], "f": 0, "t": 7, "d": [91,364] },
1634-
{ "px": [96,144], "src": [112,0], "f": 0, "t": 7, "d": [91,366] },
16351634
{ "px": [608,160], "src": [112,0], "f": 0, "t": 7, "d": [91,438] },
16361635
{ "px": [160,176], "src": [112,0], "f": 0, "t": 7, "d": [91,450] },
16371636
{ "px": [176,176], "src": [112,0], "f": 0, "t": 7, "d": [91,451] },
@@ -1677,7 +1676,6 @@
16771676
{ "px": [592,256], "src": [96,16], "f": 0, "t": 19, "d": [95,677] },
16781677
{ "px": [144,272], "src": [96,16], "f": 0, "t": 19, "d": [95,689] },
16791678
{ "px": [288,272], "src": [96,16], "f": 0, "t": 19, "d": [95,698] },
1680-
{ "px": [112,160], "src": [80,16], "f": 0, "t": 18, "d": [96,407] },
16811679
{ "px": [112,176], "src": [80,16], "f": 0, "t": 18, "d": [96,447] },
16821680
{ "px": [624,176], "src": [80,16], "f": 0, "t": 18, "d": [96,479] },
16831681
{ "px": [112,192], "src": [80,16], "f": 0, "t": 18, "d": [96,487] },
@@ -1703,16 +1701,18 @@
17031701
{ "px": [416,256], "src": [128,16], "f": 0, "t": 21, "d": [97,666] },
17041702
{ "px": [560,256], "src": [128,16], "f": 0, "t": 21, "d": [97,675] },
17051703
{ "px": [256,272], "src": [128,16], "f": 0, "t": 21, "d": [97,696] },
1704+
{ "px": [96,160], "src": [64,32], "f": 0, "t": 30, "d": [82,406] },
17061705
{ "px": [528,176], "src": [64,32], "f": 0, "t": 30, "d": [82,473] },
17071706
{ "px": [208,192], "src": [96,32], "f": 0, "t": 32, "d": [82,493] },
17081707
{ "px": [368,192], "src": [96,32], "f": 0, "t": 32, "d": [82,503] },
17091708
{ "px": [48,160], "src": [80,32], "f": 0, "t": 31, "d": [81,403] },
17101709
{ "px": [480,176], "src": [48,32], "f": 0, "t": 29, "d": [81,470] },
17111710
{ "px": [336,192], "src": [80,32], "f": 0, "t": 31, "d": [81,501] },
1712-
{ "px": [160,128], "src": [112,32], "f": 0, "t": 33, "d": [71,330] },
1711+
{ "px": [144,128], "src": [112,32], "f": 0, "t": 33, "d": [71,329] },
17131712
{ "px": [240,128], "src": [128,32], "f": 0, "t": 34, "d": [70,335] },
17141713
{ "px": [592,64], "src": [80,0], "f": 0, "t": 5, "d": [72,197] },
1715-
{ "px": [112,144], "src": [80,0], "f": 0, "t": 5, "d": [72,367] },
1714+
{ "px": [96,144], "src": [80,0], "f": 0, "t": 5, "d": [72,366] },
1715+
{ "px": [112,160], "src": [128,0], "f": 0, "t": 8, "d": [72,407] },
17161716
{ "px": [528,160], "src": [80,0], "f": 0, "t": 5, "d": [72,433] },
17171717
{ "px": [624,160], "src": [80,0], "f": 0, "t": 5, "d": [72,439] },
17181718
{ "px": [208,176], "src": [80,0], "f": 0, "t": 5, "d": [72,453] },

Examples/Shooter/ShooterGame.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class ShooterGame : Game
1515
{
1616
// LDtk stuff
1717
private LDtkWorld world;
18-
private LDtkLevel[] levels;
1918
private LDtkRenderer renderer;
2019
private readonly List<EnemyEntity> enemies = new List<EnemyEntity>();
2120
private readonly List<BulletEntity> bullets = new List<BulletEntity>();
@@ -34,7 +33,6 @@ public class ShooterGame : Game
3433
private readonly GraphicsDeviceManager graphics;
3534
private float pixelScale = 1f;
3635
public static Texture2D Pixel { get; set; }
37-
3836
private KeyboardState oldKeyboard;
3937

4038
public ShooterGame()
@@ -74,27 +72,26 @@ protected override void Initialize()
7472
renderer = new LDtkRenderer(spriteBatch);
7573

7674
world = LDtkWorld.LoadWorld("Data\\World.ldtk");
77-
levels = new LDtkLevel[world.Levels.Length];
7875

7976
spriteSheet = Texture2D.FromFile(GraphicsDevice, Path.Combine(world.RootFolder, "Characters.png"));
8077

8178
for (int i = 0; i < world.Levels.Length; i++)
8279
{
83-
levels[i] = world.LoadLevel(world.Levels[i].Identifier);
80+
world.Levels[i] = world.LoadLevel(world.Levels[i].Identifier);
8481

85-
foreach (Enemy enemy in levels[i].GetEntities<Enemy>())
82+
foreach (Enemy enemy in world.Levels[i].GetEntities<Enemy>())
8683
{
8784
enemies.Add(new EnemyEntity(enemy, spriteSheet, renderer));
8885
}
8986

90-
renderer.PrerenderLevel(levels[i]);
87+
renderer.PrerenderLevel(world.Levels[i]);
9188
}
9289

93-
Gun_Pickup gunData = levels[1].GetEntity<Gun_Pickup>();
90+
Gun_Pickup gunData = world.GetEntity<Gun_Pickup>();
9491
gun = new GunEntity(gunData, spriteSheet, renderer);
9592

96-
Player playerData = world.Levels[1].GetEntity<Player>();// TODO: get entity in levels
97-
player = new PlayerEntity(playerData, spriteSheet, renderer, gun);// TODO: levels
93+
Player playerData = world.GetEntity<Player>();
94+
player = new PlayerEntity(playerData, spriteSheet, renderer, gun);
9895

9996
player.onShoot += () =>
10097
{
@@ -165,9 +162,9 @@ protected override void Draw(GameTime gameTime)
165162
spriteBatch.Begin(camera, SpriteSortMode.Deferred, null, SamplerState.PointClamp);
166163
{
167164
// Draw Levels layers
168-
for (int i = 0; i < levels.Length; i++)
165+
for (int i = 0; i < world.Levels.Length; i++)
169166
{
170-
renderer.RenderPrerenderedLevel(levels[i]);
167+
renderer.RenderPrerenderedLevel(world.Levels[i]);
171168
}
172169

173170
player.Draw(totalTime);

LDtk.ContentPipeline/Level/LDtkLevelWritter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text.Json;
23
using Microsoft.Xna.Framework.Content.Pipeline;
34
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
45

@@ -12,8 +13,7 @@ protected override void Write(ContentWriter output, LDtkLevel json)
1213
try
1314
{
1415
ContentLogger.LogMessage($"Writting");
15-
// TODO: binary serialize this eventually
16-
output.Write(System.Text.Json.JsonSerializer.Serialize(json, LDtkWorld.SerializeOptions));
16+
output.Write(JsonSerializer.Serialize(json, LDtkWorld.SerializeOptions));
1717
}
1818
catch (Exception ex)
1919
{

LDtk.ContentPipeline/World/LDtkWorldWritter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text.Json;
23
using Microsoft.Xna.Framework.Content.Pipeline;
34
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
45

@@ -12,8 +13,7 @@ protected override void Write(ContentWriter output, LDtkWorld json)
1213
try
1314
{
1415
ContentLogger.LogMessage($"Writting");
15-
// TODO: binary serialize this eventually
16-
output.Write(System.Text.Json.JsonSerializer.Serialize(json, LDtkWorld.SerializeOptions));
16+
output.Write(JsonSerializer.Serialize(json, LDtkWorld.SerializeOptions));
1717
}
1818
catch (Exception ex)
1919
{

LDtk/LDtkFieldParser.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,14 @@ private static void ParseCustomFields<T>(T classFields, FieldInstance[] fields,
131131
case Field.PointArrayType:
132132
List<Point> points = JsonSerializer.Deserialize<List<Point>>(fieldInstance._Value.ToString(), new JsonSerializerOptions() { Converters = { new CxCyConverter() } });
133133

134-
// TODO: check all layers
135-
int gridSize = level.LayerInstances[0]._GridSize;
134+
int gridSize = 0;
135+
for (int j = 0; j < level.LayerInstances.Length; j++)
136+
{
137+
if (level.LayerInstances[j]._Type == LayerType.Entities)
138+
{
139+
gridSize = level.LayerInstances[j]._GridSize;
140+
}
141+
}
136142

137143
for (int j = 0; j < points.Count; j++)
138144
{

LDtk/LDtkWorld.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Text.Json;
45
using System.Text.Json.Serialization;
@@ -93,6 +94,29 @@ public LDtkLevel LoadLevel(string identifier)
9394
throw new LevelNotFoundException($"Could not find {identifier} Level in {this}.");
9495
}
9596

97+
/// <summary>
98+
/// Gets a collection of entities of type <typeparamref name="T"/> in the current level
99+
/// </summary>
100+
/// <returns></returns>
101+
/// <exception cref="EntityNotFoundException"></exception>
102+
public T GetEntity<T>() where T : new()
103+
{
104+
List<T> entities = new List<T>();
105+
for (int i = 0; i < Levels.Length; i++)
106+
{
107+
entities.AddRange(Levels[i].GetEntities<T>());
108+
}
109+
110+
if (entities.Count == 1)
111+
{
112+
return entities[0];
113+
}
114+
else
115+
{
116+
throw new EntityNotFoundException($"Could not find entity with identifier {typeof(T).Name}");
117+
}
118+
}
119+
96120
/// <summary>
97121
/// Loads the ldtkl world file from disk directly or from the embeded one depending on if externalLevels is set
98122
/// </summary>

0 commit comments

Comments
 (0)