PlayerSystem.cs 11 KB

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