MapHelper.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. namespace ET.Server
  5. {
  6. /// <summary>
  7. /// 地图工具类
  8. /// </summary>
  9. public static class MapHelper
  10. {
  11. /// <summary>
  12. /// 进入指定场景
  13. /// </summary>
  14. /// <param name="player"></param>
  15. /// <param name="instanceId"></param>
  16. /// <param name="targetX"></param>
  17. /// <param name="targetY"></param>
  18. /// <param name="forceChange"></param>
  19. public static void EnterAreaByInstanceId(WNPlayer player, long instanceId, float targetX, float targetY, bool forceChange)
  20. {
  21. Map map = player.DomainScene().GetComponent<GameMapComponent>().Get(instanceId);
  22. AreaData areaData = new AreaData(map.MapId, instanceId);
  23. areaData.targetX = targetX;
  24. areaData.targetY = targetY;
  25. DispatchByInstanceId(player, areaData, forceChange);
  26. }
  27. /// <summary>
  28. /// 根据场景实例id进入相应场景
  29. /// </summary>
  30. /// <param name="player"></param>
  31. /// <param name="areaData"></param>
  32. /// <param name="forceCreate"></param>
  33. /// <returns></returns>
  34. public static Map DispatchByInstanceId(WNPlayer player, AreaData areaData, bool forceCreate)
  35. {
  36. if (player.GetMapInstanceId() == areaData.instanceId)
  37. {
  38. return null;
  39. }
  40. ChangeArea(player, areaData, forceCreate);
  41. return player.DomainScene().GetComponent<GameMapComponent>().Get(areaData.instanceId);
  42. }
  43. /// <summary>
  44. /// 实际切换地图
  45. /// </summary>
  46. /// <param name="player"></param>
  47. /// <param name="areaData"></param>
  48. /// <param name="forceChange"></param>
  49. /// <returns></returns>
  50. public static Map ChangeArea(WNPlayer player, AreaData areaData, bool forceChange)
  51. {
  52. Map map = player.Map;
  53. if (map != null)
  54. {
  55. if (map.Id == areaData.instanceId)
  56. {
  57. Log.Info($"changeArea1已在场景:{map.Prop.Name}, playerId={player.GetId()}, playerName={player.GetName()}");
  58. return map;
  59. }
  60. map.SyncPlayerHistoryData(player);
  61. }
  62. Map targetMap = player.DomainScene().GetComponent<GameMapComponent>().Get(areaData.instanceId);
  63. if (targetMap == null)
  64. {
  65. Log.Debug($"目标场景为空");
  66. return null;
  67. }
  68. // 非强制切图,检测一下pk模式,是否允许切图
  69. if (!forceChange)
  70. {
  71. int resCode = player.GetXmdsManager().refreshPlayerPKMode(player.GetId().ToString().Trim(), true, (int)PkModel.Peace);
  72. if (resCode != 0)
  73. {
  74. // 战斗状态下无法传送
  75. Log.Warning($"refreshPlayerPKMode resCode={resCode}, playerId={player.GetId()}, 当前场景={player.GetComponent<PlayerTempDataComponent>().MapData.mapId}, 传送场景={areaData.areaId}");
  76. return null;
  77. }
  78. }
  79. player.GetComponent<PlayerTempDataComponent>().SyncBornData(areaData.targetX, areaData.targetY, areaData.areaId);
  80. player.BornType = (int) BORN_TYPE.BORN;
  81. player.EnterState = (int) ENTER_STATE.changeArea;
  82. // 进入新的地图
  83. map = PlayerEnterAreaServer(player, areaData.instanceId);
  84. return map;
  85. }
  86. /// <summary>
  87. /// 进入map
  88. /// </summary>
  89. /// <param name="player"></param>
  90. /// <param name="instanceId"></param>
  91. public static Map PlayerEnterAreaServer(WNPlayer player, long instanceId)
  92. {
  93. Map map = player.DomainScene().GetComponent<GameMapComponent>().Get(instanceId);
  94. if (map != null)
  95. {
  96. Map oldMap = player.Map;
  97. if (oldMap != null)
  98. {
  99. oldMap.RemovePlayer(player, false);
  100. }
  101. // 初始化角色出生坐标数据
  102. player.InitBornData();
  103. map.AddPlayer(player);
  104. }
  105. else
  106. {
  107. Log.Warning($"playerEnterAreaServer场景不存在: playerId={player.GetId()}, instanceId={instanceId}");
  108. }
  109. return map;
  110. }
  111. /// <summary>
  112. /// 角色bind的时候分配场景
  113. /// </summary>
  114. /// <param name="player"></param>
  115. /// <returns></returns>
  116. public static Map BindPlayerDispatch(WNPlayer player)
  117. {
  118. Map map = null;
  119. // 角色身上绑定的场景信息
  120. PlayerMapInfo playerMapInfo = player.GetComponent<PlayerTempDataComponent>().MapData;
  121. // 执行普通场景逻辑
  122. MapConfig prop = MapConfigCategory.Instance.Get(playerMapInfo.mapId);
  123. if (prop != null)
  124. {
  125. map = player.DomainScene().GetComponent<GameMapComponent>().Get(playerMapInfo.mapInstanceId);
  126. }
  127. if (map == null)
  128. {
  129. // 创建一个场景
  130. JObject jsonObject = new JObject();
  131. jsonObject.Add("id", player.GetId());
  132. jsonObject.Add("logicServerId", player.GetLogicServerId());
  133. jsonObject.Add("areaId", playerMapInfo.mapId);
  134. map = CreateMap(player, jsonObject, false);
  135. }
  136. if (map != null)
  137. {
  138. ChangeArea(player, new AreaData(10098, map.Id), false);
  139. }
  140. return map;
  141. }
  142. /// <summary>
  143. /// 创建战斗服场景需求数据
  144. /// </summary>
  145. /// <param name="player"></param>
  146. /// <param name="jObject"></param>
  147. /// <param name="forceCreate"></param>
  148. /// <returns></returns>
  149. public static Map CreateMap(WNPlayer player, JObject jObject, bool forceCreate)
  150. {
  151. Log.Debug($"createArea areaData:{JsonConvert.SerializeObject(jObject, Formatting.Indented)}");
  152. int mapId = Convert.ToInt32(jObject.SelectToken("areaId"));
  153. MapConfig prop = MapConfigCategory.Instance.Get(mapId);
  154. if (prop == null)
  155. {
  156. return null;
  157. }
  158. long instanceId = IdGenerater.Instance.GenerateInstanceId();
  159. bool usespaceDiv = prop.Type == (int) MapType.NORMAL;
  160. JObject jsonObject = new ();
  161. jsonObject.Add("monsterHard", prop.MonsterHard);
  162. jsonObject.Add("calPKValue", prop.ignorePkRule == 0);
  163. jsonObject.Add("allowAutoGuard", prop.AutoFight);
  164. jsonObject.Add("usespaceDiv", usespaceDiv);
  165. jsonObject.Add("sceneType", prop.Type);
  166. jsonObject.Add("areaType", prop.AreaType);
  167. jsonObject.Add("canRiding", prop.RideMount == 1);
  168. jsonObject.Add("killInterval", prop.killInterval);
  169. jsonObject.Add("killFullNum", prop.killFullNum);
  170. jsonObject.Add("killFullCollSec", prop.killFullCollSec);
  171. jsonObject.Add("averageLevel", 0);
  172. string enterData = JsonConvert.SerializeObject(jsonObject, Formatting.Indented);
  173. Log.Info($"createArea instanceId:{instanceId}, templateId:{prop.TemplateID}, bs:{player.GetZoneManager()}");
  174. int resCode = player.GetZoneManager().createZoneRequest(player.GetId().ToString(),
  175. "bs-" + ConstGame.GameServerId, prop.TemplateID, instanceId.ToString(), forceCreate, enterData);
  176. if (resCode == 0)
  177. {
  178. jsonObject.Add("instanceId", instanceId);
  179. jsonObject.Add("areaId", mapId);
  180. return CreaLocalMap(player, prop, jsonObject, instanceId, mapId, "bs-" + ConstGame.GameServerId);
  181. }
  182. Log.Warning($"createZoneRequest resCode={resCode}, mapID={prop.Id}, playerId={player.GetId()}");
  183. return null;
  184. }
  185. /// <summary>
  186. /// 创建本地场景
  187. /// </summary>
  188. /// <param name="player"></param>
  189. /// <param name="prop"></param>
  190. /// <param name="data"></param>
  191. /// <param name="instanceId"></param>
  192. /// <param name="mapId"></param>
  193. /// <param name="bsServerId"></param>
  194. /// <returns></returns>
  195. public static Map CreaLocalMap(WNPlayer player, MapConfig prop, JObject data, long instanceId, int mapId, string bsServerId)
  196. {
  197. Scene scene = player.DomainScene();
  198. Map map = scene.GetComponent<GameMapComponent>().AddChildWithId<Map, JObject, WNPlayer>(instanceId, data, player);
  199. if (map != null)
  200. {
  201. Log.Info($"创建Area场景:{map.MapId}, instanceId:{instanceId}, srvId:" + map.LogicServerId);
  202. map.BindBattleServer(player, bsServerId);
  203. scene.GetComponent<GameMapComponent>().Add(map.RoomId, map);
  204. }
  205. else
  206. {
  207. Log.Error($"createZone:{mapId} fail! there is no area type of this,type:{prop.Type}");
  208. }
  209. return map;
  210. }
  211. }
  212. }