Game.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. namespace Model
  5. {
  6. public static class Game
  7. {
  8. private static HashSet<Disposer> disposers;
  9. private static EntityEventManager entityEventManager;
  10. public static TPoller Poller { get; } = new TPoller();
  11. private static Scene scene;
  12. public static Scene Scene
  13. {
  14. get
  15. {
  16. if (scene == null)
  17. {
  18. scene = new Scene();
  19. #if SERVER
  20. scene.AddComponent<EventComponent>();
  21. #else
  22. scene.AddComponent<EventComponent>();
  23. #endif
  24. scene.AddComponent<TimerComponent>();
  25. }
  26. return scene;
  27. }
  28. }
  29. public static HashSet<Disposer> Disposers
  30. {
  31. get
  32. {
  33. if (disposers == null)
  34. {
  35. disposers = new HashSet<Disposer>();
  36. }
  37. return disposers;
  38. }
  39. }
  40. public static void CloseScene()
  41. {
  42. scene.Dispose();
  43. scene = null;
  44. }
  45. public static void ClearDisposers()
  46. {
  47. foreach (Disposer disposer in Disposers)
  48. {
  49. disposer.Dispose();
  50. }
  51. disposers.Clear();
  52. disposers = null;
  53. }
  54. public static EntityEventManager EntityEventManager
  55. {
  56. get
  57. {
  58. if (entityEventManager == null)
  59. {
  60. entityEventManager = new EntityEventManager();
  61. }
  62. return entityEventManager;
  63. }
  64. set
  65. {
  66. entityEventManager = value;
  67. }
  68. }
  69. public static string DisposerInfo()
  70. {
  71. var info = new Dictionary<string, int>();
  72. foreach (Disposer disposer in Disposers)
  73. {
  74. if (info.ContainsKey(disposer.GetType().Name))
  75. {
  76. info[disposer.GetType().Name] += 1;
  77. }
  78. else
  79. {
  80. info[disposer.GetType().Name] = 1;
  81. }
  82. }
  83. info = info.OrderByDescending(s => s.Value).ToDictionary(p => p.Key, p => p.Value);
  84. StringBuilder sb = new StringBuilder();
  85. sb.Append("\r\n");
  86. foreach (string key in info.Keys)
  87. {
  88. sb.Append($"{info[key],10} {key}\r\n");
  89. }
  90. return sb.ToString();
  91. }
  92. }
  93. }