MapHelper.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. Struct.AreaData areaData = new Struct.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, Struct.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, Struct.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="map"></param>
  91. public static void PlayerEnterAreaServerInner(WNPlayer player, Map map)
  92. {
  93. // 初始化角色出生坐标数据
  94. player.InitBornData();
  95. map.AddPlayer(player);
  96. }
  97. public static Map PlayerEnterAreaServer(WNPlayer player, long instanceId)
  98. {
  99. Map map = player.DomainScene().GetComponent<GameMapComponent>().Get(instanceId);
  100. if (map != null)
  101. {
  102. Map oldMap = player.Map;
  103. if (oldMap != null)
  104. {
  105. oldMap.RemovePlayer(player, false);
  106. }
  107. PlayerEnterAreaServerInner(player, map);
  108. }
  109. else
  110. {
  111. Log.Warning($"playerEnterAreaServer场景不存在: playerId={player.GetId()}, instanceId={instanceId}");
  112. }
  113. return map;
  114. }
  115. /// <summary>
  116. /// 角色bind的时候分配场景
  117. /// </summary>
  118. /// <param name="player"></param>
  119. /// <returns></returns>
  120. public static Map BindPlayerDispatch(WNPlayer player)
  121. {
  122. Map map = null;
  123. // 角色身上绑定的场景信息
  124. PlayerMapInfo playerMapInfo = player.GetComponent<PlayerTempDataComponent>().MapData;
  125. // 执行普通场景逻辑
  126. MapConfig prop = MapConfigCategory.Instance.Get(playerMapInfo.mapId);
  127. if (prop != null)
  128. {
  129. map = player.DomainScene().GetComponent<GameMapComponent>().Get(playerMapInfo.mapInstanceId);
  130. if (map == null)
  131. {
  132. Map[] list = player.DomainScene().GetComponent<GameMapComponent>().GetAll();
  133. if (list is { Length: > 0 })
  134. {
  135. map = list[0];
  136. }
  137. }
  138. }
  139. if (map != null)
  140. {
  141. return map;
  142. }
  143. // 创建一个场景
  144. JObject jsonObject = new JObject();
  145. jsonObject.Add("id", player.GetId());
  146. jsonObject.Add("logicServerId", player.GetLogicServerId());
  147. jsonObject.Add("areaId", playerMapInfo.mapId);
  148. map = CreateMap(player, jsonObject, false);
  149. if (map != null)
  150. {
  151. ChangeArea(player, new Struct.AreaData(10098, map.Id), false);
  152. }
  153. return map;
  154. }
  155. /// <summary>
  156. /// 创建战斗服场景需求数据
  157. /// </summary>
  158. /// <param name="player"></param>
  159. /// <param name="jObject"></param>
  160. /// <param name="forceCreate"></param>
  161. /// <returns></returns>
  162. public static Map CreateMap(WNPlayer player, JObject jObject, bool forceCreate)
  163. {
  164. Log.Debug($"createArea areaData:{jObject}");
  165. int mapId = Convert.ToInt32(jObject.SelectToken("areaId"));
  166. MapConfig prop = MapConfigCategory.Instance.Get(mapId);
  167. if (prop == null)
  168. {
  169. return null;
  170. }
  171. long instanceId = IdGenerater.Instance.GenerateInstanceId();
  172. bool usespaceDiv = prop.Type == (int) MapType.NORMAL;
  173. JObject jsonObject = new JObject();
  174. jsonObject.Add("monsterHard", prop.MonsterHard);
  175. jsonObject.Add("calPKValue", prop.ignorePkRule == 0);
  176. jsonObject.Add("allowAutoGuard", prop.AutoFight);
  177. jsonObject.Add("usespaceDiv", usespaceDiv);
  178. jsonObject.Add("sceneType", prop.Type);
  179. jsonObject.Add("areaType", prop.AreaType);
  180. jsonObject.Add("canRiding", prop.RideMount == 1);
  181. jsonObject.Add("killInterval", prop.killInterval);
  182. jsonObject.Add("killFullNum", prop.killFullNum);
  183. jsonObject.Add("killFullCollSec", prop.killFullCollSec);
  184. jsonObject.Add("averageLevel", 0);
  185. string enterData = JsonConvert.SerializeObject(jsonObject, Formatting.Indented);
  186. Log.Info($"createArea instanceId:{instanceId}, templateId:{prop.TemplateID}, bs:{player.GetZoneManager()}");
  187. int resCode = player.GetZoneManager().createZoneRequest(player.GetId().ToString(),
  188. player.GetLogicServerId().ToString(), prop.TemplateID, instanceId.ToString(), forceCreate, enterData);
  189. if (resCode == 0)
  190. {
  191. jsonObject.Add("instanceId", instanceId);
  192. jsonObject.Add("areaId", mapId);
  193. return CreaLocalMap(player, prop, jsonObject, instanceId, mapId, player.GetLogicServerId().ToString());
  194. }
  195. Log.Warning($"createZoneRequest resCode={resCode}, mapID={prop.Id}, playerId={player.GetId()}");
  196. return null;
  197. }
  198. /// <summary>
  199. /// 创建本地场景
  200. /// </summary>
  201. /// <param name="player"></param>
  202. /// <param name="prop"></param>
  203. /// <param name="data"></param>
  204. /// <param name="instanceId"></param>
  205. /// <param name="mapId"></param>
  206. /// <param name="bsServerId"></param>
  207. /// <returns></returns>
  208. public static Map CreaLocalMap(WNPlayer player, MapConfig prop, JObject data, long instanceId, int mapId, string bsServerId)
  209. {
  210. Scene scene = player.DomainScene();
  211. Map map = scene.GetComponent<GameMapComponent>().AddChildWithId<Map, JObject>(instanceId, data);
  212. if (map != null)
  213. {
  214. Log.Info($"创建Area场景:{map.MapId}, instanceId:{instanceId}, srvId:" + map.LogicServerId);
  215. scene.GetComponent<GameMapComponent>().Add(map);
  216. map.BindBattleServer(player, bsServerId);
  217. }
  218. else
  219. {
  220. Log.Error($"createZone:{mapId} fail! there is no area type of this,type:{prop.Type}");
  221. }
  222. return map;
  223. }
  224. }
  225. }