Game.cs 1.7 KB

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