PlayerSystem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System.Collections.Generic;
  2. using System.Text.Json;
  3. using BattleIce;
  4. using JsonSerializer = System.Text.Json.JsonSerializer;
  5. namespace ET.Server
  6. {
  7. [FriendOf(typeof (WNPlayer))]
  8. [FriendOf(typeof (BattleIceAgentComponent))]
  9. public static class PlayerSystem
  10. {
  11. public class PlayerAwakeSystem: AwakeSystem<WNPlayer, Session>
  12. {
  13. protected override void Awake(WNPlayer self, Session session)
  14. {
  15. Log.Info($"创建玩家实体...");
  16. PlayerInfo playerInfo = session.GetComponent<SessionPlayerComponent>().PlayerInfo;
  17. // 绑定sessionId
  18. self.GameSessionActorId = session.InstanceId;
  19. self.Session = session;
  20. self.BasicProp = CharacterConfigCategory.Instance.Get(playerInfo.Pro);
  21. self.BornType = (int)BORN_TYPE.NORMAL;
  22. self.EnterState = (int)ENTER_STATE.online;
  23. // 玩家基础数据组件
  24. self.AddComponent<PlayerDataComponent, PlayerInfo>(playerInfo);
  25. // 玩家临时数据组件
  26. self.AddComponent<PlayerTempDataComponent>();
  27. // 玩家技能组件
  28. self.AddComponent<PlayerSkillComponent>();
  29. // 玩家货币组件
  30. self.AddComponent<PlayerMoneyComponent>();
  31. // 玩家背包组件
  32. // self.AddComponent<BagComponent>();
  33. // 玩家属性组件
  34. self.AddComponent<PlayerBtlComponent>();
  35. }
  36. }
  37. public class PlayerDestroySystem: DestroySystem<WNPlayer>
  38. {
  39. protected override void Destroy(WNPlayer self)
  40. {
  41. Log.Info($"销毁玩家实体...");
  42. }
  43. }
  44. /// <summary>
  45. /// 获取角色id
  46. /// </summary>
  47. /// <param name="self"></param>
  48. /// <returns></returns>
  49. public static long GetId(this WNPlayer self)
  50. {
  51. return self.GetComponent<PlayerDataComponent>().Data.Id;
  52. }
  53. /// <summary>
  54. /// 获取玩家类型
  55. /// </summary>
  56. /// <param name="self"></param>
  57. /// <returns></returns>
  58. public static int GetPlayerType(this WNPlayer self)
  59. {
  60. return self.GetComponent<PlayerDataComponent>().Data.PlayerType;
  61. }
  62. /// <summary>
  63. /// 获取直播间id
  64. /// </summary>
  65. /// <param name="self"></param>
  66. /// <returns></returns>
  67. public static string GetRoomId(this WNPlayer self)
  68. {
  69. return self.GetComponent<PlayerDataComponent>().Data.RoomId;
  70. }
  71. /// <summary>
  72. /// 获取名称
  73. /// </summary>
  74. /// <param name="self"></param>
  75. /// <returns></returns>
  76. public static string GetName(this WNPlayer self)
  77. {
  78. return self.GetComponent<PlayerDataComponent>().Data.Name;
  79. }
  80. /// <summary>
  81. /// 获取性别
  82. /// </summary>
  83. /// <param name="self"></param>
  84. /// <returns></returns>
  85. public static int GetSex(this WNPlayer self)
  86. {
  87. return self.GetComponent<PlayerDataComponent>().Data.Sex;
  88. }
  89. /// <summary>
  90. /// 获取职业
  91. /// </summary>
  92. /// <param name="self"></param>
  93. /// <returns></returns>
  94. public static int GetPro(this WNPlayer self)
  95. {
  96. return self.GetComponent<PlayerDataComponent>().Data.Pro;
  97. }
  98. /// <summary>
  99. /// 获取经验
  100. /// </summary>
  101. /// <param name="self"></param>
  102. /// <returns></returns>
  103. public static long GetExp(this WNPlayer self)
  104. {
  105. return self.GetComponent<PlayerDataComponent>().Data.Exp;
  106. }
  107. /// <summary>
  108. /// 获取等级
  109. /// </summary>
  110. /// <param name="self"></param>
  111. /// <returns></returns>
  112. public static int GetLevel(this WNPlayer self)
  113. {
  114. return self.GetComponent<PlayerDataComponent>().Data.Level;
  115. }
  116. /// <summary>
  117. /// 获取金币
  118. /// </summary>
  119. /// <param name="self"></param>
  120. /// <returns></returns>
  121. public static long GetGold(this WNPlayer self)
  122. {
  123. return self.GetComponent<PlayerDataComponent>().Data.Gold;
  124. }
  125. /// <summary>
  126. /// 获取场景实例id
  127. /// </summary>
  128. /// <param name="self"></param>
  129. /// <returns></returns>
  130. public static long GetMapInstanceId(this WNPlayer self)
  131. {
  132. return self.Map.InstanceId;
  133. }
  134. /** 获取服务器id **/
  135. public static int GetLogicServerId(this WNPlayer self)
  136. {
  137. return self.GetComponent<PlayerDataComponent>().Data.LogicServerId;
  138. }
  139. public static ZoneManagerPrx GetZoneManager(this WNPlayer self)
  140. {
  141. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceZoneManager;
  142. }
  143. public static XmdsManagerPrx GetXmdsManager(this WNPlayer self)
  144. {
  145. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceXmdsManager;
  146. }
  147. /** bornData相关初始化 **/
  148. public static void InitBornData(this WNPlayer self)
  149. {
  150. switch (self.BornType)
  151. {
  152. case (int)BORN_TYPE.HISTORY:
  153. self.GetComponent<PlayerTempDataComponent>().MapData.mapId = self.GetComponent<PlayerTempDataComponent>().MapData.historyMapId;
  154. self.GetComponent<PlayerTempDataComponent>().MapData.mapInstanceId =
  155. self.GetComponent<PlayerTempDataComponent>().MapData.historyMapInstanceId;
  156. self.GetComponent<PlayerTempDataComponent>().MapData.x = self.GetComponent<PlayerTempDataComponent>().MapData.historyX;
  157. self.GetComponent<PlayerTempDataComponent>().MapData.y = self.GetComponent<PlayerTempDataComponent>().MapData.historyY;
  158. break;
  159. case (int)BORN_TYPE.BORN:
  160. self.GetComponent<PlayerTempDataComponent>().MapData.mapId = self.GetComponent<PlayerTempDataComponent>().MapData.bornMapId;
  161. self.GetComponent<PlayerTempDataComponent>().MapData.mapInstanceId =
  162. self.GetComponent<PlayerTempDataComponent>().MapData.bornMapInstanceId;
  163. self.GetComponent<PlayerTempDataComponent>().MapData.x = self.GetComponent<PlayerTempDataComponent>().MapData.bornX;
  164. self.GetComponent<PlayerTempDataComponent>().MapData.y = self.GetComponent<PlayerTempDataComponent>().MapData.bornY;
  165. break;
  166. }
  167. }
  168. /** 客户端资源加载完成通知 给客户端推送的数据要在这里 **/
  169. public static void OnReady(this WNPlayer self)
  170. {
  171. self.OnEndEnterScene();
  172. // 登录第一次进场景处理,此次登陆登出期间只处理一次
  173. if (self.ReadyFirst)
  174. {
  175. self.ReadyFirst = false;
  176. }
  177. }
  178. /** 玩家登录事件 **/
  179. public static void OnLogin(this WNPlayer self)
  180. {
  181. self.ReadyFirst = true;
  182. self.DomainScene().GetComponent<GamePlayerComponent>().Add(self.GetId(), self);
  183. }
  184. /** 向客户端推送角色相关数据 **/
  185. public static void OnEndEnterScene(this WNPlayer self)
  186. {
  187. bool ready = self.GetComponent<PlayerTempDataComponent>().MapData.ready;
  188. if (ready)
  189. {
  190. Log.Warning($"$OnEndEnterScene跳过 : playerId={self.GetId()}, 玩家场景:{self.Map.MapId}, 进入场景:" + (self.Map?.MapId ?? -1));
  191. return;
  192. }
  193. self.GetComponent<PlayerTempDataComponent>().MapData.ready = true;
  194. self.GetXmdsManager().playerReady(self.GetId().ToString().Trim());
  195. //PKMode设置为All
  196. self.GetXmdsManager().refreshPlayerPKMode(self.GetId().ToString().Trim(), false, (int)PkModel.All);
  197. //设置为自动战斗
  198. self.GetXmdsManager().autoBattle(self.Map.Id.ToString().Trim(), self.GetId().ToString().Trim(), true);
  199. }
  200. /// <summary>
  201. /// 战斗服角色数据
  202. /// </summary>
  203. /// <param name="self"></param>
  204. /// <returns></returns>
  205. private static Dictionary<string, object> GetBattlerServerBasic(this WNPlayer self)
  206. {
  207. Dictionary<string, object> data = new Dictionary<string, object>();
  208. data.Add("name", self.GetName());
  209. data.Add("alliesForce", 0);
  210. data.Add("force", 1);
  211. data.Add("pro", self.GetPro());
  212. data.Add("serverId", ConstGame.GameServerId);
  213. data.Add("titleId", 0);
  214. data.Add("level", self.GetLevel());
  215. data.Add("vip", 0);
  216. data.Add("upLevel", 1);
  217. // 无悬赏
  218. data.Add("beReward", 0);
  219. data.Add("logicServerId", ConstGame.GameServerId);
  220. data.Add("sex", self.GetSex());
  221. data.Add("uuid", self.GetId().ToString());
  222. data.Add("potionAddition", 0);
  223. return data;
  224. }
  225. /// <summary>
  226. /// 场景中角色需求数据
  227. /// </summary>
  228. /// <param name="self"></param>
  229. /// <param name="map"></param>
  230. /// <returns></returns>
  231. public static string ToJSON4EnterScene(this WNPlayer self, Map map)
  232. {
  233. var json = new
  234. {
  235. effects = self.GetComponent<PlayerBtlComponent>().GetBattleServerEffects(),
  236. effectsExt = new { },
  237. skills = self.GetComponent<PlayerSkillComponent>().ToJson4BattleServerSkills,
  238. tasks = new { },
  239. flags = new { },
  240. playerEntered = false,
  241. avatars = new { },
  242. basic = self.GetBattlerServerBasic(),
  243. connectServerId = "bs-" + ConstGame.GameServerId,
  244. uid = self.GetId().ToString(),
  245. unitTemplateID = self.BasicProp.TemplateId,
  246. robot = false,
  247. tempData = self.GetComponent<PlayerTempDataComponent>().ToJson4BattleServerTempData,
  248. pkInfo = new { mode = 0, value = 0, level = 1, },
  249. //petBase,
  250. addTestPetData = 0,
  251. sceneData = new { allowAutoGuard = 3, },
  252. };
  253. return JsonSerializer.Serialize(json, new JsonSerializerOptions { IncludeFields = true });
  254. }
  255. }
  256. }