Game.cs 1.8 KB

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