LocationProxyComponentSystem.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. namespace ET
  3. {
  4. [ObjectSystem]
  5. public class LocationProxyComponentSystem: AwakeSystem<LocationProxyComponent>
  6. {
  7. public override void Awake(LocationProxyComponent self)
  8. {
  9. self.Awake();
  10. }
  11. }
  12. public static class LocationProxyComponentEx
  13. {
  14. public static void Awake(this LocationProxyComponent self)
  15. {
  16. LocationProxyComponent.Instance = self;
  17. }
  18. private static long GetLocationSceneId(long key)
  19. {
  20. return StartSceneConfigCategory.Instance.LocationConfig.SceneId;
  21. }
  22. public static async ETTask Add(this LocationProxyComponent self, long key, long instanceId)
  23. {
  24. Log.Info($"location proxy add {key}, {instanceId} {TimeHelper.ServerNow()}");
  25. await MessageHelper.CallActor(GetLocationSceneId(key),
  26. new ObjectAddRequest() { Key = key, InstanceId = instanceId });
  27. }
  28. public static async ETTask Lock(this LocationProxyComponent self, long key, long instanceId, int time = 1000)
  29. {
  30. Log.Info($"location proxy lock {key}, {instanceId} {TimeHelper.ServerNow()}");
  31. await MessageHelper.CallActor(GetLocationSceneId(key),
  32. new ObjectLockRequest() { Key = key, InstanceId = instanceId, Time = time });
  33. }
  34. public static async ETTask UnLock(this LocationProxyComponent self, long key, long oldInstanceId, long instanceId)
  35. {
  36. Log.Info($"location proxy unlock {key}, {instanceId} {TimeHelper.ServerNow()}");
  37. await MessageHelper.CallActor(GetLocationSceneId(key),
  38. new ObjectUnLockRequest() { Key = key, OldInstanceId = oldInstanceId, InstanceId = instanceId });
  39. }
  40. public static async ETTask Remove(this LocationProxyComponent self, long key)
  41. {
  42. Log.Info($"location proxy add {key}, {TimeHelper.ServerNow()}");
  43. await MessageHelper.CallActor(GetLocationSceneId(key),
  44. new ObjectRemoveRequest() { Key = key });
  45. }
  46. public static async ETTask<long> Get(this LocationProxyComponent self, long key)
  47. {
  48. if (key == 0)
  49. {
  50. throw new Exception($"get location key 0");
  51. }
  52. // location server配置到共享区,一个大战区可以配置N多个location server,这里暂时为1
  53. ObjectGetResponse response =
  54. (ObjectGetResponse) await MessageHelper.CallActor(GetLocationSceneId(key),
  55. new ObjectGetRequest() { Key = key });
  56. return response.InstanceId;
  57. }
  58. public static async ETTask AddLocation(this Entity self)
  59. {
  60. await LocationProxyComponent.Instance.Add(self.Id, self.InstanceId);
  61. }
  62. public static async ETTask RemoveLocation(this Entity self)
  63. {
  64. await LocationProxyComponent.Instance.Remove(self.Id);
  65. }
  66. }
  67. }