PlayerSystem.cs 9.9 KB

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