GameMapComponentSystem.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Linq;
  2. namespace ET.Server
  3. {
  4. [FriendOf(typeof (GameMapComponent))]
  5. public static class GameMapComponentSystem
  6. {
  7. public class GameMapComponentAwakeSystem: AwakeSystem<GameMapComponent>
  8. {
  9. /// <summary>
  10. /// 游戏服场景组件创建
  11. /// </summary>
  12. /// <param name="self"></param>
  13. protected override void Awake(GameMapComponent self)
  14. {
  15. GameMapComponent.Instance = self;
  16. // todo 添加场景销毁检测定时器
  17. }
  18. }
  19. public class GameMapComponentDestroySystem: DestroySystem<GameMapComponent>
  20. {
  21. /// <summary>
  22. /// 游戏服场景组件销毁
  23. /// </summary>
  24. /// <param name="self"></param>
  25. protected override void Destroy(GameMapComponent self)
  26. {
  27. }
  28. }
  29. public static void Add(this GameMapComponent self, long roomId, Map map)
  30. {
  31. self.allMaps.TryAdd(map.Id, map);
  32. // 映射一下roomId与instanceId的关系
  33. self.rooms.TryAdd(roomId, map.Id);
  34. }
  35. public static Map Get(this GameMapComponent self, long instanceId)
  36. {
  37. self.allMaps.TryGetValue(instanceId, out Map map);
  38. return map;
  39. }
  40. public static void Remove(this GameMapComponent self, long instanceId, long roomId)
  41. {
  42. self.allMaps.Remove(instanceId);
  43. // 移除一下roomId与instanceId的关系
  44. self.rooms.Remove(roomId);
  45. }
  46. public static Map[] GetAll(this GameMapComponent self)
  47. {
  48. return self.allMaps.Values.ToArray();
  49. }
  50. public static Map GetMapByRoomId(this GameMapComponent self, long roomId)
  51. {
  52. if (!self.rooms.TryGetValue(roomId, out long instanceId))
  53. {
  54. return null;
  55. }
  56. if (instanceId > 0)
  57. {
  58. return self.Get(instanceId);
  59. }
  60. return null;
  61. }
  62. }
  63. }