TransferHelper.cs 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. namespace ET.Server
  2. {
  3. public static class TransferHelper
  4. {
  5. public static async ETTask Transfer(Unit unit, long sceneInstanceId, string sceneName)
  6. {
  7. // 通知客户端开始切场景
  8. M2C_StartSceneChange m2CStartSceneChange = new M2C_StartSceneChange() {SceneInstanceId = sceneInstanceId, SceneName = sceneName};
  9. MessageHelper.SendToClient(unit, m2CStartSceneChange);
  10. M2M_UnitTransferRequest request = new M2M_UnitTransferRequest();
  11. request.Unit = unit;
  12. foreach (Entity entity in unit.Components.Values)
  13. {
  14. if (entity is ITransfer)
  15. {
  16. request.Entitys.Add(entity);
  17. }
  18. }
  19. // 删除Mailbox,让发给Unit的ActorLocation消息重发
  20. unit.RemoveComponent<MailBoxComponent>();
  21. // location加锁
  22. long oldInstanceId = unit.InstanceId;
  23. await LocationProxyComponent.Instance.Lock(unit.Id, unit.InstanceId);
  24. M2M_UnitTransferResponse response = await ActorMessageSenderComponent.Instance.Call(sceneInstanceId, request) as M2M_UnitTransferResponse;
  25. await LocationProxyComponent.Instance.UnLock(unit.Id, oldInstanceId, response.NewInstanceId);
  26. unit.Dispose();
  27. }
  28. }
  29. }