PlayerSystem.cs 10 KB

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