Skip to content

Commit 405eccf

Browse files
committed
Tidy up content pipeline support
1 parent 3035e15 commit 405eccf

File tree

4 files changed

+42
-114
lines changed

4 files changed

+42
-114
lines changed

Examples/Api/ApiGame.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected override void Initialize()
3838
levels = new LDtkLevel[world.Levels.Length];
3939
for (int i = 0; i < world.Levels.Length; i++)
4040
{
41-
levels[i] = world.LoadLevel("Level_" + i, Content);
41+
levels[i] = world.LoadLevel("Level_" + i);
4242

4343
rects.AddRange(levels[i].GetEntities<RectRegion>());
4444

Examples/Platformer/LevelManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void Update()
4141
{
4242
for (int i = 0; i < CurrentLevel._Neighbours.Length; i++)
4343
{
44-
LDtkLevel neighbour = world.LoadLevel(CurrentLevel._Neighbours[i].LevelUid, content);
44+
LDtkLevel neighbour = world.LoadLevel(CurrentLevel._Neighbours[i].LevelUid);
4545
renderer.PrerenderLevel(neighbour);
4646

4747
if (neighbour.Contains(center))
@@ -58,20 +58,20 @@ public void Draw()
5858

5959
for (int i = 0; i < CurrentLevel._Neighbours.Length; i++)
6060
{
61-
LDtkLevel neighbour = world.LoadLevel(CurrentLevel._Neighbours[i].LevelUid, content);
61+
LDtkLevel neighbour = world.LoadLevel(CurrentLevel._Neighbours[i].LevelUid);
6262
renderer.RenderPrerenderedLevel(neighbour);
6363
}
6464
}
6565

6666
public void ChangeLevelTo(string identifier)
6767
{
68-
CurrentLevel = content != null ? world.LoadLevel(identifier, content) : world.LoadLevel(identifier);
68+
CurrentLevel = world.LoadLevel(identifier);
6969

7070
renderer.PrerenderLevel(CurrentLevel);
7171

7272
for (int ii = 0; ii < CurrentLevel._Neighbours.Length; ii++)
7373
{
74-
LDtkLevel neighbourLevel = world.LoadLevel(CurrentLevel._Neighbours[ii].LevelUid, content);
74+
LDtkLevel neighbourLevel = world.LoadLevel(CurrentLevel._Neighbours[ii].LevelUid);
7575
renderer.PrerenderLevel(neighbourLevel);
7676
}
7777

LDtk/LDtk.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<LangVersion>10</LangVersion>
9+
10+
<IsPackable>true</IsPackable>
911
</PropertyGroup>
1012

1113
<ItemGroup>

LDtk/LDtkWorld.cs

Lines changed: 35 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public partial class LDtkWorld
2323
/// </summary>
2424
public string RootFolder { get; set; }
2525

26+
/// <summary>
27+
/// The content manager used if you are using the contentpipeline
28+
/// </summary>
29+
public ContentManager Content { get; set; }
30+
2631
/// <summary>
2732
/// Loads the ldtk world file from disk directly
2833
/// </summary>
@@ -44,16 +49,12 @@ public static LDtkWorld LoadWorld(string filePath)
4449
/// <returns>LDtkWorld</returns>
4550
public static LDtkWorld LoadWorld(string filePath, ContentManager content)
4651
{
47-
if (content != null)
48-
{
49-
LDtkWorld world;
50-
world = content.Load<LDtkWorld>(filePath);
51-
world.RootFolder = Path.GetDirectoryName(filePath);
52+
LDtkWorld world;
53+
world = content.Load<LDtkWorld>(filePath);
54+
world.Content = content;
55+
world.RootFolder = Path.GetDirectoryName(filePath);
5256

53-
return world;
54-
}
55-
56-
throw new ContentLoadException($"Could not load ldtk world at {filePath}.");
57+
return world;
5758
}
5859

5960
/// <summary>
@@ -81,7 +82,7 @@ public LDtkLevel LoadLevel(string identifier)
8182

8283
string path = Path.Join(RootFolder, Levels[i].ExternalRelPath);
8384

84-
level = JsonSerializer.Deserialize<LDtkLevel>(File.ReadAllText(path), SerializeOptions);
85+
level = Content.Load<LDtkLevel>(path.Replace(".ldtkl", "")) ?? JsonSerializer.Deserialize<LDtkLevel>(File.ReadAllText(path), SerializeOptions);
8586
break;
8687
}
8788

@@ -94,29 +95,6 @@ public LDtkLevel LoadLevel(string identifier)
9495
throw new LevelNotFoundException($"Could not find {identifier} Level in {this}.");
9596
}
9697

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-
12098
/// <summary>
12199
/// Loads the ldtkl world file from disk directly or from the embeded one depending on if externalLevels is set
122100
/// </summary>
@@ -142,45 +120,7 @@ public LDtkLevel LoadLevel(int uid)
142120

143121
string path = Path.Join(RootFolder, Levels[i].ExternalRelPath);
144122

145-
level = JsonSerializer.Deserialize<LDtkLevel>(File.ReadAllText(path), SerializeOptions);
146-
break;
147-
}
148-
149-
if (level != null)
150-
{
151-
level.Parent = this;
152-
return level;
153-
}
154-
155-
throw new LevelNotFoundException($"Could not find {uid} Level in {this}.");
156-
}
157-
158-
/// <summary>
159-
/// Loads the ldtkl world file from disk directly or from the embeded one depending on if externalLevels is set
160-
/// </summary>
161-
/// <param name="uid">The Levels uid</param>
162-
/// <param name="content">Content Pipeline</param>
163-
/// <returns><see cref="LDtkLevel"/></returns>
164-
/// <exception cref="LevelNotFoundException"></exception>
165-
public LDtkLevel LoadLevel(int uid, ContentManager content)
166-
{
167-
LDtkLevel level = null;
168-
169-
for (int i = 0; i < Levels.Length; i++)
170-
{
171-
if (Levels[i].Uid != uid)
172-
{
173-
continue;
174-
}
175-
176-
if (ExternalLevels == false)
177-
{
178-
level = Levels[i];
179-
break;
180-
}
181-
182-
string path = Path.Join(RootFolder, Levels[i].ExternalRelPath);
183-
level = content.Load<LDtkLevel>(path.Replace(".ldtkl", ""));
123+
level = Content.Load<LDtkLevel>(path.Replace(".ldtkl", "")) ?? JsonSerializer.Deserialize<LDtkLevel>(File.ReadAllText(path), SerializeOptions);
184124
break;
185125
}
186126

@@ -212,43 +152,6 @@ public EntityDefinition GetEntityDefinitionFromUid(int uid)
212152
return null;
213153
}
214154

215-
/// <summary>
216-
/// Loads the ldtkl world file from disk directly or from the embeded one depending on if externalLevels is set
217-
/// </summary>
218-
/// <param name="identifier">The Level identifier</param>
219-
/// <param name="content">The optional XNA content manager if you are using the content pipeline</param>
220-
/// <returns>LDtkWorld</returns>
221-
public LDtkLevel LoadLevel(string identifier, ContentManager content)
222-
{
223-
LDtkLevel level = null;
224-
225-
for (int i = 0; i < Levels.Length; i++)
226-
{
227-
if (Levels[i].Identifier != identifier)
228-
{
229-
continue;
230-
}
231-
232-
if (ExternalLevels == false)
233-
{
234-
level = Levels[i];
235-
break;
236-
}
237-
238-
string path = Path.Join(RootFolder, Levels[i].ExternalRelPath);
239-
level = content.Load<LDtkLevel>(path.Replace(".ldtkl", ""));
240-
break;
241-
}
242-
243-
if (level != null)
244-
{
245-
level.Parent = this;
246-
return level;
247-
}
248-
249-
throw new LevelNotFoundException($"Could not Content.Load `{identifier}` in {this}.");
250-
}
251-
252155
/// <summary>
253156
/// Gets the intgrid value definitions
254157
/// </summary>
@@ -272,4 +175,27 @@ public IntGridValueDefinition[] GetIntgridValueDefinitions(string identifier)
272175

273176
throw new FieldNotFoundException();
274177
}
178+
179+
/// <summary>
180+
/// Gets a collection of entities of type <typeparamref name="T"/> in the current level
181+
/// </summary>
182+
/// <returns></returns>
183+
/// <exception cref="EntityNotFoundException"></exception>
184+
public T GetEntity<T>() where T : new()
185+
{
186+
List<T> entities = new List<T>();
187+
for (int i = 0; i < Levels.Length; i++)
188+
{
189+
entities.AddRange(Levels[i].GetEntities<T>());
190+
}
191+
192+
if (entities.Count == 1)
193+
{
194+
return entities[0];
195+
}
196+
else
197+
{
198+
throw new EntityNotFoundException($"Could not find entity with identifier {typeof(T).Name}");
199+
}
200+
}
275201
}

0 commit comments

Comments
 (0)