using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace ET.Server { /// /// 地图工具类 /// public static class MapHelper { /// /// 进入指定场景 /// /// /// /// /// /// public static void EnterAreaByInstanceId(WNPlayer player, long instanceId, float targetX, float targetY, bool forceChange) { Map map = player.DomainScene().GetComponent().Get(instanceId); Struct.AreaData areaData = new Struct.AreaData(map.MapId, instanceId); areaData.targetX = targetX; areaData.targetY = targetY; DispatchByInstanceId(player, areaData, forceChange); } /// /// 根据场景实例id进入相应场景 /// /// /// /// /// public static Map DispatchByInstanceId(WNPlayer player, Struct.AreaData areaData, bool forceCreate) { if (player.GetMapInstanceId() == areaData.instanceId) { return null; } ChangeArea(player, areaData, forceCreate); return player.DomainScene().GetComponent().Get(areaData.instanceId); } /// /// 实际切换地图 /// /// /// /// /// public static Map ChangeArea(WNPlayer player, Struct.AreaData areaData, bool forceChange) { Map map = player.Map; if (map != null) { if (map.Id == areaData.instanceId) { Log.Info($"changeArea1已在场景:{map.Prop.Name}, playerId={player.GetId()}, playerName={player.GetName()}"); return map; } map.SyncPlayerHistoryData(player); } Map targetMap = player.DomainScene().GetComponent().Get(areaData.instanceId); if (targetMap == null) { Log.Debug($"目标场景为空"); return null; } // 非强制切图,检测一下pk模式,是否允许切图 if (!forceChange) { int resCode = player.GetXmdsManager().refreshPlayerPKMode(player.GetId().ToString().Trim(), true, (int)PkModel.Peace); if (resCode != 0) { // 战斗状态下无法传送 Log.Warning($"refreshPlayerPKMode resCode={resCode}, playerId={player.GetId()}, 当前场景={player.GetComponent().MapData.mapId}, 传送场景={areaData.areaId}"); return null; } } player.GetComponent().SyncBornData(areaData.targetX, areaData.targetY, areaData.areaId); player.BornType = (int) BORN_TYPE.BORN; player.EnterState = (int) ENTER_STATE.changeArea; // 进入新的地图 map = PlayerEnterAreaServer(player, areaData.instanceId); return map; } /// /// 进入map /// /// /// public static void PlayerEnterAreaServerInner(WNPlayer player, Map map) { // 初始化角色出生坐标数据 player.InitBornData(); map.AddPlayer(player); } public static Map PlayerEnterAreaServer(WNPlayer player, long instanceId) { Map map = player.DomainScene().GetComponent().Get(instanceId); if (map != null) { Map oldMap = player.Map; if (oldMap != null) { oldMap.RemovePlayer(player, false); } PlayerEnterAreaServerInner(player, map); } else { Log.Warning($"playerEnterAreaServer场景不存在: playerId={player.GetId()}, instanceId={instanceId}"); } return map; } /// /// 角色bind的时候分配场景 /// /// /// public static Map BindPlayerDispatch(WNPlayer player) { Map map = null; // 角色身上绑定的场景信息 PlayerMapInfo playerMapInfo = player.GetComponent().MapData; // 执行普通场景逻辑 MapConfig prop = MapConfigCategory.Instance.Get(playerMapInfo.mapId); if (prop != null) { map = player.DomainScene().GetComponent().Get(playerMapInfo.mapInstanceId); if (map == null) { Map[] list = player.DomainScene().GetComponent().GetAll(); if (list is { Length: > 0 }) { map = list[0]; } } } if (map != null) { return map; } // 创建一个场景 JObject jsonObject = new JObject(); jsonObject.Add("id", player.GetId()); jsonObject.Add("logicServerId", player.GetLogicServerId()); jsonObject.Add("areaId", playerMapInfo.mapId); map = CreateMap(player, jsonObject, false); if (map != null) { ChangeArea(player, new Struct.AreaData(10098, map.Id), false); } return map; } /// /// 创建战斗服场景需求数据 /// /// /// /// /// public static Map CreateMap(WNPlayer player, JObject jObject, bool forceCreate) { Log.Debug($"createArea areaData:{jObject}"); int mapId = Convert.ToInt32(jObject.SelectToken("areaId")); MapConfig prop = MapConfigCategory.Instance.Get(mapId); if (prop == null) { return null; } long instanceId = IdGenerater.Instance.GenerateInstanceId(); bool usespaceDiv = prop.Type == (int) MapType.NORMAL; JObject jsonObject = new JObject(); jsonObject.Add("monsterHard", prop.MonsterHard); jsonObject.Add("calPKValue", prop.ignorePkRule == 0); jsonObject.Add("allowAutoGuard", prop.AutoFight); jsonObject.Add("usespaceDiv", usespaceDiv); jsonObject.Add("sceneType", prop.Type); jsonObject.Add("areaType", prop.AreaType); jsonObject.Add("canRiding", prop.RideMount == 1); jsonObject.Add("killInterval", prop.killInterval); jsonObject.Add("killFullNum", prop.killFullNum); jsonObject.Add("killFullCollSec", prop.killFullCollSec); jsonObject.Add("averageLevel", 0); string enterData = JsonConvert.SerializeObject(jsonObject, Formatting.Indented); Log.Info($"createArea instanceId:{instanceId}, templateId:{prop.TemplateID}, bs:{player.GetZoneManager()}"); int resCode = player.GetZoneManager().createZoneRequest(player.GetId().ToString(), player.GetLogicServerId().ToString(), prop.TemplateID, instanceId.ToString(), forceCreate, enterData); if (resCode == 0) { jsonObject.Add("instanceId", instanceId); jsonObject.Add("areaId", mapId); return CreaLocalMap(player, prop, jsonObject, instanceId, mapId, player.GetLogicServerId().ToString()); } Log.Warning($"createZoneRequest resCode={resCode}, mapID={prop.Id}, playerId={player.GetId()}"); return null; } /// /// 创建本地场景 /// /// /// /// /// /// /// /// public static Map CreaLocalMap(WNPlayer player, MapConfig prop, JObject data, long instanceId, int mapId, string bsServerId) { Scene scene = player.DomainScene(); Map map = scene.GetComponent().AddChildWithId(instanceId, data); if (map != null) { Log.Info($"创建Area场景:{map.MapId}, instanceId:{instanceId}, srvId:" + map.LogicServerId); scene.GetComponent().Add(map); map.BindBattleServer(player, bsServerId); } else { Log.Error($"createZone:{mapId} fail! there is no area type of this,type:{prop.Type}"); } return map; } } }