MapSystem.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using BattleIce;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. namespace ET.Server
  6. {
  7. public class MapAwakeSystem: AwakeSystem<Map, JObject>
  8. {
  9. /// <summary>
  10. /// 本地地图场景实体创建
  11. /// </summary>
  12. /// <param name="self"></param>
  13. protected override void Awake(Map self, JObject opts)
  14. {
  15. self.createTime = TimeHelper.ServerNow();
  16. Log.Debug($"create area opts:{opts}");
  17. self.LogicServerId = opts.SelectToken("logicServerId") != null? Convert.ToInt32(opts.SelectToken("logicServerId")) : 0;
  18. self.MapId = Convert.ToInt32(opts.SelectToken("areaId"));
  19. self.Prop = MapConfigCategory.Instance.Get(self.MapId);
  20. self.Type = self.Prop.Type;
  21. }
  22. }
  23. public class MapDestroySystem: DestroySystem<Map>
  24. {
  25. /// <summary>
  26. /// 本地地图场景实体销毁
  27. /// </summary>
  28. /// <param name="self"></param>
  29. protected override void Destroy(Map self)
  30. {
  31. }
  32. }
  33. [FriendOf(typeof (Map))]
  34. [FriendOf(typeof (BattleIceAgentComponent))]
  35. public static class MapSystem
  36. {
  37. public static ZoneManagerPrx GetZoneManager(this Map self)
  38. {
  39. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceZoneManager;
  40. }
  41. public static XmdsManagerPrx GetXmdsManager(this Map self)
  42. {
  43. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceXmdsManager;
  44. }
  45. public static GetPlayerData GetPlayerData(this Map self, long playerId)
  46. {
  47. string result = self.GetXmdsManager().getPlayerData(playerId.ToString().Trim(), true);
  48. return string.IsNullOrEmpty(result)? null : JsonConvert.DeserializeObject<GetPlayerData>(result);
  49. }
  50. /** 从战斗服同步角色数据 **/
  51. public static void SyncPlayerHistoryData(this Map self, WNPlayer player)
  52. {
  53. GetPlayerData result = self.GetPlayerData(player.GetId());
  54. if (result == null)
  55. {
  56. return;
  57. }
  58. player.GetComponent<PlayerTempDataComponent>().SyncNowData(self.MapId, self.InstanceId, result);
  59. player.GetComponent<PlayerTempDataComponent>().SyncHistoryData(self.Prop, self.InstanceId, result);
  60. }
  61. /** 场景添加角色 **/
  62. public static void AddPlayer(this Map self, WNPlayer player)
  63. {
  64. Log.Info($"addPlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  65. self.SetForce(player);
  66. player.Map = self;
  67. }
  68. /** 分配阵营 **/
  69. public static void SetForce(this Map self, WNPlayer player)
  70. {
  71. player.Force = (int)AreaForce.FORCEA;
  72. }
  73. /** 移除角色,通用框架接口 切换场景/掉线会自动调用,尽量 不要手动调用 **/
  74. public static void RemovePlayer(this Map self, WNPlayer player, bool keepObject)
  75. {
  76. Log.Info($"removePlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  77. self.Players.TryGetValue(player.GetId(), out WNPlayer actorPlayer);
  78. if (actorPlayer != null)
  79. {
  80. self.Players.Remove(player.GetId());
  81. }
  82. }
  83. /** 玩家进入场景请求 **/
  84. public static void PlayerEnterRequest(this Map self, WNPlayer player)
  85. {
  86. try
  87. {
  88. self.GetZoneManager().playerEnterRequest(player.GetId().ToString().Trim(), self.InstanceId.ToString().Trim(),
  89. player.toJSON4EnterScene(self));
  90. }
  91. catch (Exception e)
  92. {
  93. Log.Warning(
  94. $"playerEnterRequest: playerName={player.GetName()}, instanceId={self.InstanceId}, mapName={self.Prop.Name}, serverID={self.BattleServerId}, e={e}");
  95. }
  96. }
  97. /** 玩家离开场景请求 **/
  98. public static void PlayerLeaveRequest(this Map self, WNPlayer player, bool keepObject)
  99. {
  100. try
  101. {
  102. self.GetZoneManager().playerLeaveRequest(player.GetId().ToString().Trim(), self.InstanceId.ToString().Trim(), keepObject);
  103. }
  104. catch (Exception e)
  105. {
  106. Log.Warning($"playerLeaveRequest: catch - {e}");
  107. }
  108. Log.Debug($"playerLeaveRequest--------------------{player.GetName()} - {self.InstanceId} - {self.Prop.Name}");
  109. }
  110. /** 玩家登录事件 **/
  111. public static void OnPlayerLogin(this Map self, WNPlayer player)
  112. {
  113. }
  114. /** 角色成功进入场景 **/
  115. public static void OnPlayerEntered(this Map self, WNPlayer player)
  116. {
  117. }
  118. public static void BindBattleServer(this Map self, WNPlayer player, string serverId)
  119. {
  120. self.BattleServerId = serverId;
  121. }
  122. }
  123. }