Scene.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Base
  4. {
  5. public enum SceneType
  6. {
  7. Share,
  8. Game,
  9. Login,
  10. Lobby,
  11. Map,
  12. Launcher,
  13. Robot,
  14. BehaviorTreeScene,
  15. RobotClient,
  16. }
  17. public sealed class Scene: Entity<Scene>
  18. {
  19. public Scene Parent { get; set; }
  20. public string Name { get; set; }
  21. public SceneType SceneType { get; }
  22. private readonly Dictionary<long, Scene> Children = new Dictionary<long, Scene>();
  23. private readonly Dictionary<SceneType, HashSet<Scene>> SceneTypeChildren = new Dictionary<SceneType, HashSet<Scene>>();
  24. public Scene(string name, SceneType sceneType)
  25. {
  26. this.Name = name;
  27. this.SceneType = sceneType;
  28. }
  29. public int Count
  30. {
  31. get
  32. {
  33. return this.Children.Count;
  34. }
  35. }
  36. public void Add(Scene scene)
  37. {
  38. scene.Parent = this;
  39. this.Children.Add(scene.Id, scene);
  40. HashSet<Scene> listScene;
  41. if (!this.SceneTypeChildren.TryGetValue(scene.SceneType, out listScene))
  42. {
  43. listScene = new HashSet<Scene>();
  44. this.SceneTypeChildren.Add(scene.SceneType, listScene);
  45. }
  46. listScene.Add(scene);
  47. }
  48. public Scene Get(SceneType sceneType)
  49. {
  50. HashSet<Scene> scenes;
  51. if (!this.SceneTypeChildren.TryGetValue(sceneType, out scenes))
  52. {
  53. return null;
  54. }
  55. if (scenes.Count == 0)
  56. {
  57. return null;
  58. }
  59. return scenes.First();
  60. }
  61. public void Remove(SceneType sceneType)
  62. {
  63. HashSet<Scene> scenes;
  64. if (!this.SceneTypeChildren.TryGetValue(sceneType, out scenes))
  65. {
  66. return;
  67. }
  68. foreach (Scene scene in scenes)
  69. {
  70. Children.Remove(scene.Id);
  71. scene.Dispose();
  72. }
  73. this.SceneTypeChildren.Remove(sceneType);
  74. }
  75. public void Remove(long id)
  76. {
  77. Scene scene;
  78. if (!this.Children.TryGetValue(id, out scene))
  79. {
  80. return;
  81. }
  82. HashSet<Scene> scenes;
  83. if (!this.SceneTypeChildren.TryGetValue(scene.SceneType, out scenes))
  84. {
  85. return;
  86. }
  87. scenes.Remove(scene);
  88. scene.Dispose();
  89. }
  90. public override void Dispose()
  91. {
  92. if (this.Id == 0)
  93. {
  94. return;
  95. }
  96. base.Dispose();
  97. this.Parent?.Remove(this.Id);
  98. }
  99. }
  100. }