Root.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ET
  6. {
  7. // 管理根部的Scene
  8. public class Root: Singleton<Root>, ISingletonAwake
  9. {
  10. // 管理所有的Entity
  11. private readonly Dictionary<long, Entity> allEntities = new();
  12. public Scene Scene { get; private set; }
  13. public void Awake()
  14. {
  15. this.Scene = EntitySceneFactory.CreateScene(0, SceneType.Process, "Process");
  16. }
  17. public override void Dispose()
  18. {
  19. this.Scene.Dispose();
  20. }
  21. public void Add(Entity entity)
  22. {
  23. this.allEntities.Add(entity.InstanceId, entity);
  24. }
  25. public void Remove(long instanceId)
  26. {
  27. this.allEntities.Remove(instanceId);
  28. }
  29. public Entity Get(long instanceId)
  30. {
  31. Entity component = null;
  32. this.allEntities.TryGetValue(instanceId, out component);
  33. return component;
  34. }
  35. public override string ToString()
  36. {
  37. StringBuilder sb = new();
  38. HashSet<Type> noParent = new HashSet<Type>();
  39. Dictionary<Type, int> typeCount = new Dictionary<Type, int>();
  40. HashSet<Type> noDomain = new HashSet<Type>();
  41. foreach (var kv in this.allEntities)
  42. {
  43. Type type = kv.Value.GetType();
  44. if (kv.Value.Parent == null)
  45. {
  46. noParent.Add(type);
  47. }
  48. if (kv.Value.Domain == null)
  49. {
  50. noDomain.Add(type);
  51. }
  52. if (typeCount.ContainsKey(type))
  53. {
  54. typeCount[type]++;
  55. }
  56. else
  57. {
  58. typeCount[type] = 1;
  59. }
  60. }
  61. sb.AppendLine("not set parent type: ");
  62. foreach (Type type in noParent)
  63. {
  64. sb.AppendLine($"\t{type.Name}");
  65. }
  66. sb.AppendLine("not set domain type: ");
  67. foreach (Type type in noDomain)
  68. {
  69. sb.AppendLine($"\t{type.Name}");
  70. }
  71. IOrderedEnumerable<KeyValuePair<Type, int>> orderByDescending = typeCount.OrderByDescending(s => s.Value);
  72. sb.AppendLine("Entity Count: ");
  73. foreach (var kv in orderByDescending)
  74. {
  75. if (kv.Value == 1)
  76. {
  77. continue;
  78. }
  79. sb.AppendLine($"\t{kv.Key.Name}: {kv.Value}");
  80. }
  81. return sb.ToString();
  82. }
  83. }
  84. }