EcsUtils.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Leopotam.Ecs;
  4. namespace Asset_Cleaner {
  5. static class EcsUtils {
  6. public static IEnumerable<(T Group, IEnumerable<int> Indices)> GroupBy1<T, T1, T2>(this EcsFilter<T, T1, T2> f, IEqualityComparer<T> comp)
  7. where T : class
  8. where T1 : class
  9. where T2 : class {
  10. foreach (var group in Inner().GroupBy(tuple => tuple.Group, comp))
  11. yield return (group.Key, group.Select(g => g.EcsIndex));
  12. IEnumerable<(T Group, int EcsIndex)> Inner() {
  13. var get1 = f.Get1;
  14. foreach (var i in f) yield return (get1[i], i);
  15. }
  16. }
  17. public static EcsFilter<T> Out<T>(this EcsFilter<T> filter, out T[] get1, out EcsEntity[] entities) where T : class {
  18. get1 = filter.Get1;
  19. entities = filter.Entities;
  20. return filter;
  21. }
  22. public static EcsFilter<T1, T2> Out<T1, T2>(this EcsFilter<T1, T2> filter, out T1[] get1, out T2[] get2, out EcsEntity[] entities)
  23. where T1 : class where T2 : class {
  24. get1 = filter.Get1;
  25. get2 = filter.Get2;
  26. entities = filter.Entities;
  27. return filter;
  28. }
  29. public static EcsFilter<T1, T2, T3> Out<T1, T2, T3>(this EcsFilter<T1, T2, T3> filter, out T1[] get1, out T2[] get2, out T3[] get3, out EcsEntity[] entities)
  30. where T1 : class where T2 : class where T3 : class {
  31. get1 = filter.Get1;
  32. get2 = filter.Get2;
  33. get3 = filter.Get3;
  34. entities = filter.Entities;
  35. return filter;
  36. }
  37. public static void AllDestroy(this EcsFilter f) {
  38. var ecsEntities = f.Entities;
  39. foreach (var i in f)
  40. ecsEntities[i].Destroy();
  41. }
  42. public static void AllUnset<T>(this EcsFilter f) where T : class {
  43. var e = f.Entities;
  44. foreach (var i in f)
  45. e[i].Unset<T>();
  46. }
  47. }
  48. }