C2G_AddUnitsToMapHandler.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. namespace ET.Server
  3. {
  4. /// <summary>
  5. /// 向场景增加玩家
  6. /// </summary>
  7. [MessageHandler(SceneType.Game)]
  8. public class C2G_AddUnitsToMapHandler: AMRpcHandler<C2G_AddUnitsToMap, G2C_AddUnitsToMap>
  9. {
  10. protected override async ETTask Run(Session session, C2G_AddUnitsToMap request, G2C_AddUnitsToMap response, Action reply)
  11. {
  12. if (session.DomainScene().SceneType != SceneType.Game)
  13. {
  14. Log.Debug($"Game 请求的Scene错误...SceneType={session.DomainScene().SceneType}");
  15. session.Dispose();
  16. return;
  17. }
  18. // 移除session自动超时组件
  19. session.RemoveComponent<SessionAcceptTimeoutComponent>();
  20. WNPlayer player = session.GetComponent<SessionPlayerComponent>().GetMyPlayer();
  21. if (player == null)
  22. {
  23. Log.Debug($"操作错误, player is null");
  24. response.Error = ErrorCode.ERR_OperationError;
  25. reply();
  26. return;
  27. }
  28. // 参数判断
  29. if (request.UnitId <= 0 || request.Force < 0 || request.Force > (int)AreaForce.FORCEB )
  30. {
  31. Log.Debug($"参数错误...unitId={request.UnitId}, force={request.Force}");
  32. response.Error = ErrorCode.ERR_ParameterError;
  33. reply();
  34. return;
  35. }
  36. // Struct.MonsterUnit unit = new Struct.MonsterUnit();
  37. // unit.id = request.UnitId;
  38. // unit.force = request.Force;
  39. // if (!string.IsNullOrEmpty(request.Flag))
  40. // {
  41. // unit.flag = request.Flag;
  42. // }
  43. // else
  44. // {
  45. // unit.x = request.X;
  46. // unit.y = request.Y;
  47. // }
  48. // unit.autoGuard = true;
  49. //
  50. // await player.Map.AddUnits(unit, false);
  51. // 预先创建数据
  52. Scene scene = session.DomainScene();
  53. long _id = IdGenerater.Instance.GenerateUnitId(scene.DomainZone());
  54. PlayerInfo playerInfo = new PlayerInfo();
  55. playerInfo.Id = _id;
  56. playerInfo.UserId = session.GetComponent<SessionPlayerComponent>().UserId;
  57. playerInfo.PlayerType = 1;
  58. playerInfo.Name = "玩家-" + _id;
  59. playerInfo.Sex = 0;
  60. playerInfo.Pro = (int)PlayerProType.CANG_LANG;
  61. playerInfo.Level = 1;
  62. playerInfo.Exp = 0;
  63. WNPlayer unitPlayer = await PlayerFactory.CreatPlayer(session, playerInfo);
  64. if (unitPlayer == null)
  65. {
  66. Log.Debug($"创建单位玩家有误...unitPlayerId={_id}, force={request.Force}");
  67. response.Error = ErrorCode.ERR_CreateUnitPlayerError;
  68. reply();
  69. return;
  70. }
  71. // 进入主播场景
  72. player.Map.PlayerEnterRequest(unitPlayer);
  73. reply();
  74. }
  75. }
  76. }