forked from lilybeevee/eevee-helper
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEntityContainer.cs
More file actions
432 lines (359 loc) · 12.2 KB
/
Copy pathEntityContainer.cs
File metadata and controls
432 lines (359 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using Celeste.Mod.EeveeHelper.Handlers;
using Microsoft.Xna.Framework;
using Monocle;
using System;
using System.Collections.Generic;
using System.Linq;
using Celeste.Mod.Registry;
namespace Celeste.Mod.EeveeHelper.Components;
[Tracked(true)]
public class EntityContainer : Component
{
public enum ContainMode
{
FlagChanged,
RoomStart,
Always,
DelayedRoomStart
}
public List<IEntityHandler> Contained = [];
public Dictionary<Entity, List<IEntityHandler>> HandlersFor = [];
public Dictionary<string, HashSet<int>> Blacklist = [];
public Dictionary<string, HashSet<int>> Whitelist = [];
public ContainMode Mode = ContainMode.RoomStart;
public string ContainFlag;
public bool NotFlag;
public bool ForceStandardBehavior;
public bool IgnoreContainerBounds;
public bool WhitelistAll;
public Func<Entity, bool> IsValid;
public Func<Entity, bool> DefaultIgnored;
public Action<IEntityHandler> OnAttach;
public Action<IEntityHandler> OnDetach;
public bool Attached;
public bool CollideWithContained;
private List<IEntityHandler> containedSaved = [];
private bool updatedOnce;
public EntityContainer() : base(true, true) { }
public EntityContainer(EntityData data) : this()
{
var whitelistString = data.Attr("whitelist");
if (whitelistString.Equals("all", StringComparison.OrdinalIgnoreCase))
WhitelistAll = true;
else
Whitelist = ParseEntityTypeList(data.Attr("whitelist"));
Blacklist = ParseEntityTypeList(data.Attr("blacklist"));
Mode = data.Enum("containMode", ContainMode.FlagChanged);
var flag = EeveeUtils.ParseFlagAttr(data.Attr("containFlag"));
ContainFlag = flag.Item1;
NotFlag = flag.Item2;
ForceStandardBehavior = data.Bool("forceStandardBehavior", true);
IgnoreContainerBounds = data.Bool("ignoreContainerBounds");
}
public override void EntityAwake()
{
base.EntityAwake();
Attached = string.IsNullOrEmpty(ContainFlag) || SceneAs<Level>().Session.GetFlag(ContainFlag) != NotFlag;
if (Attached && Mode != ContainMode.DelayedRoomStart)
{
AttachInside(true);
}
updatedOnce = false;
}
public override void Update()
{
base.Update();
Cleanup();
var newAttached = string.IsNullOrEmpty(ContainFlag) || SceneAs<Level>().Session.GetFlag(ContainFlag) != NotFlag;
if (!updatedOnce)
{
if (newAttached && Mode == ContainMode.DelayedRoomStart)
{
Attached = newAttached;
AttachInside(true);
}
updatedOnce = true;
}
if (Mode != ContainMode.Always)
{
if (newAttached != Attached)
{
Attached = newAttached;
if (Attached)
{
AttachInside();
}
else
{
DetachAll();
}
}
}
else
{
var attachChanged = newAttached != Attached;
Attached = newAttached;
if (Attached)
{
DetachOutside();
AttachInside();
}
else if (attachChanged)
{
DetachAll();
}
}
}
public virtual List<IEntityHandler> GetHandlersFor(Entity entity)
{
if (entity != null && HandlersFor.TryGetValue(entity, out var handlers))
return handlers;
return new List<IEntityHandler>();
}
public virtual bool HasHandlerFor<T>(Entity entity)
{
if (entity != null && HandlersFor.TryGetValue(entity, out var handlers))
return handlers.Any(h => h is T);
return false;
}
public virtual bool IsFirstHandler(IEntityHandler handler)
{
if (HandlersFor.TryGetValue(handler.Entity, out var handlers))
return handlers[0] == handler;
return true;
}
public virtual List<Entity> GetEntities()
{
var list = new List<Entity>();
foreach (var handler in Contained)
{
if (!list.Contains(handler.Entity))
{
list.Add(handler.Entity);
}
}
return list;
}
protected virtual void AddContained(IEntityHandler handler)
{
handler.OnAttach(this);
handler.Entity.AddOrAppendContainer(this.Entity as IContainer);
Contained.Add(handler);
if (!HandlersFor.TryGetValue(handler.Entity, out var handlers))
HandlersFor[handler.Entity] = handlers = [];
handlers.Add(handler);
}
protected virtual void RemoveContained(IEntityHandler handler)
{
Contained.Remove(handler);
var handlers = HandlersFor[handler.Entity];
handlers.Remove(handler);
if (handlers.Count == 0)
HandlersFor.Remove(handler.Entity);
handler.OnDetach(this);
}
/// <summary>
/// Parses a comma separated list of C# short type names or entity SIDs, with each type name or SID optionally followed by specific indices to affect, separated by colons.
/// </summary>
/// <param name="list">
/// A comma separated list of C# short type names or entity SIDs, with each type name or SID optionally followed by specific indices to affect, separated by colons (e.g. <c>"MoveBlock,dashBlock,Spring:2,refill:1:2:4"</c>).
/// If no indices are specified for a type name/SID, an index of <c>-1</c> will be added by default.
/// </param>
/// <returns>A dictionary containing C# short type names and entity SIDs as its keys, with hash sets containing their affected indices as its values.</returns>
protected static Dictionary<string, HashSet<int>> ParseEntityTypeList(string list)
{
var result = new Dictionary<string, HashSet<int>>();
foreach (string entry in list.Split(',', StringSplitOptions.RemoveEmptyEntries))
{
var split = entry.Split(':');
var entityType = split[0];
if (!result.TryGetValue(entityType, out var affectedIndices))
result[entityType] = affectedIndices = [];
if (split.Length >= 2)
{
for (int i = 1; i < split.Length; i++)
{
if (int.TryParse(split[i], out var affectedIndex))
{
affectedIndices.Add(affectedIndex);
}
}
}
else
{
affectedIndices.Add(-1);
}
}
return result;
}
/// <summary>
/// Checks if a type list contains an entity's C# short type name or entity SID.
/// If the entity has no SID in its <see cref="Entity.SourceData"/>, uses the SIDs given by <see cref="EntityRegistry.GetKnownSidsFromType"/> instead.
/// </summary>
/// <param name="typeList">A dictionary containing C# short type names and entity SIDs as its keys.</param>
/// <param name="entity">An entity with the type or SID to locate in the type list.</param>
/// <returns> <see langword="true"/> if the type list contains the entity's C# short type name or entity SID; otherwise, <see langword="false"/>.</returns>
protected static bool TypeListContains(Dictionary<string, HashSet<int>> typeList, Entity entity)
{
var type = entity.GetType();
if (typeList.ContainsKey(type.Name))
return true;
if (entity.SourceData?.Name is { } sourceSid
? typeList.ContainsKey(sourceSid)
: EntityRegistry.GetKnownSidsFromType(type).Any(typeList.ContainsKey))
return true;
return false;
}
/// <summary>
/// Checks if a type list contains an entity's C# short type name or entity SID, and at least one of their sets of affected indices pass a specified check.
/// If the entity has no SID in its <see cref="Entity.SourceData"/>, uses the SIDs given by <see cref="EntityRegistry.GetKnownSidsFromType"/> instead.
/// </summary>
/// <param name="typeList">A dictionary containing C# short type names and entity SIDs as its keys, with hash sets containing their affected indices as its values.</param>
/// <param name="entity">An entity with the type or SID to locate in the type list.</param>
/// <param name="affectedIndicesCheck">A check to run on the type list's affected indices for the entity's type or SID.</param>
/// <returns> <see langword="true"/> if the type list contains the entity's C# short type name or SID and the affected indices for either of them pass the specified <paramref name="affectedIndicesCheck"/>; otherwise, <see langword="false"/>.</returns>
protected static bool TypeListContains(Dictionary<string, HashSet<int>> typeList, Entity entity, Func<HashSet<int>, bool> affectedIndicesCheck)
{
var type = entity.GetType();
if (typeList.TryGetValue(type.Name, out var affectedIndices) && affectedIndicesCheck(affectedIndices))
return true;
if (entity.SourceData?.Name is { } sourceSid
? typeList.TryGetValue(sourceSid, out affectedIndices) && affectedIndicesCheck(affectedIndices)
: EntityRegistry.GetKnownSidsFromType(type).Any(typeSid => typeList.TryGetValue(typeSid, out affectedIndices) && affectedIndicesCheck(affectedIndices)))
return true;
return false;
}
protected virtual bool WhitelistCheck(Entity entity)
{
if (TypeListContains(Blacklist, entity, affectedIndices => affectedIndices.Contains(-1)))
return false;
if (WhitelistAll)
return true;
if (Whitelist.Count == 0)
return !((DefaultIgnored?.Invoke(entity) ?? false) || entity is Player || entity is SolidTiles || entity is BackgroundTiles || entity is Decal || entity is Trigger || entity is WindController);
return TypeListContains(Whitelist, entity);
}
protected virtual bool WhitelistCheckIndex(Entity entity, int index)
{
if (TypeListContains(Blacklist, entity, affectedIndices => affectedIndices.Contains(-1) || affectedIndices.Contains(index)))
return false;
if (WhitelistAll || Whitelist.Count == 0)
return true;
return TypeListContains(Whitelist, entity, affectedIndices => affectedIndices.Contains(-1) || affectedIndices.Contains(index));
}
protected virtual void AttachInside(bool first = false)
{
if (first || (Mode != ContainMode.RoomStart && Mode != ContainMode.DelayedRoomStart))
{
var entityCountsInside = new Dictionary<Type, int>();
foreach (var entity in Scene.Entities)
{
if (entity != Entity && WhitelistCheck(entity) && (IsValid?.Invoke(entity) ?? true))
{
var entityType = entity.GetType();
entityCountsInside.TryAdd(entityType, 0);
var anyInside = false;
var handlers = EntityHandler.CreateAll(entity, this, ForceStandardBehavior);
foreach (var handler in handlers)
{
if (IgnoreContainerBounds || handler.IsInside(this))
{
anyInside = true;
if ((!Contained.Contains(handler) || Mode != ContainMode.Always) && WhitelistCheckIndex(entity, entityCountsInside[entityType] + 1))
{
AddContained(handler);
OnAttach?.Invoke(handler);
}
}
}
if (anyInside)
entityCountsInside[entityType]++;
}
}
}
else
{
Cleanup();
foreach (var handler in containedSaved)
{
AddContained(handler);
OnAttach?.Invoke(handler);
}
containedSaved.Clear();
}
}
protected virtual void DetachAll()
{
var lastContained = new List<IEntityHandler>(Contained);
if (Mode == ContainMode.RoomStart || Mode == ContainMode.DelayedRoomStart)
containedSaved = lastContained;
foreach (var handler in lastContained)
{
RemoveContained(handler);
OnDetach?.Invoke(handler);
}
}
protected virtual void DetachOutside()
{
var toRemove = new List<IEntityHandler>();
foreach (var handler in Contained)
{
if (!IgnoreContainerBounds && !handler.IsInside(this))
{
toRemove.Add(handler);
}
}
foreach (var handler in toRemove)
{
RemoveContained(handler);
OnDetach?.Invoke(handler);
}
}
protected void Cleanup()
{
Contained.RemoveAll(e => e.Entity?.Scene == null);
}
public bool CheckCollision(Entity entity)
{
if (IgnoreContainerBounds)
return true;
if (entity.Collider == null)
return entity.X >= Entity.Left && entity.Y >= Entity.Top && entity.X <= Entity.Right && entity.Y <= Entity.Bottom;
var collidable = entity.Collidable;
var parentCollidable = Entity.Collidable;
entity.Collidable = true;
Entity.Collidable = true;
CollideWithContained = true;
var result = Entity.CollideCheck(entity);
CollideWithContained = false;
entity.Collidable = collidable;
Entity.Collidable = parentCollidable;
return result;
}
internal bool CheckDecal(Decal decal)
{
if (decal.textures.Count == 0)
return false;
var decalTexture = decal.textures[0];
var decalRect = new Rectangle((int)(decal.Position.X - (decalTexture.ClipRect.Width / 2)), (int)(decal.Position.Y - (decalTexture.ClipRect.Height / 2)), decalTexture.ClipRect.Width, decalTexture.ClipRect.Height);
return Collide.CheckRect(Entity, decalRect);
}
public Rectangle GetContainedBounds()
{
var bounds = new Rectangle();
var first = true;
foreach (var handler in Contained)
{
var rect = handler.GetBounds();
bounds = first ? rect : Rectangle.Union(bounds, rect);
first = false;
}
return bounds;
}
public void DestroyContained()
{
foreach (var handler in Contained)
handler.Destroy();
Contained.Clear();
}
}