C2G_EnterMapHandler.cs 1.3 KB

123456789101112131415161718192021222324252627
  1. namespace ET.Server
  2. {
  3. [MessageHandler(SceneType.Game)]
  4. public class C2G_EnterMapHandler : AMRpcHandler<C2G_EnterMap, G2C_EnterMap>
  5. {
  6. protected override async ETTask Run(Session session, C2G_EnterMap request, G2C_EnterMap response)
  7. {
  8. Player player = session.GetComponent<SessionPlayerComponent>().GetMyPlayer();
  9. // 在Game上动态创建一个Map Scene,把Unit从DB中加载放进来,然后传送到真正的Map中,这样登陆跟传送的逻辑就完全一样了
  10. GameMapComponent gateMapComponent = player.AddComponent<GameMapComponent>();
  11. gateMapComponent.Scene = await SceneFactory.CreateServerScene(gateMapComponent, player.Id, IdGenerater.Instance.GenerateInstanceId(), gateMapComponent.DomainZone(), "GateMap", SceneType.Map);
  12. Scene scene = gateMapComponent.Scene;
  13. // 这里可以从DB中加载Unit
  14. Unit unit = UnitFactory.Create(scene, player.Id, UnitType.Player);
  15. unit.AddComponent<UnitGateComponent, long>(session.InstanceId);
  16. StartSceneConfig startSceneConfig = StartSceneConfigCategory.Instance.GetBySceneName(session.DomainZone(), "Map1");
  17. response.MyId = player.Id;
  18. // 等到一帧的最后面再传送,先让G2C_EnterMap返回,否则传送消息可能比G2C_EnterMap还早
  19. TransferHelper.TransferAtFrameFinish(unit, startSceneConfig.InstanceId, startSceneConfig.Name).Coroutine();
  20. }
  21. }
  22. }