MapSystem.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using BattleIce;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. namespace ET.Server
  9. {
  10. [FriendOf(typeof (Map))]
  11. [FriendOf(typeof (BattleIceAgentComponent))]
  12. public static class MapSystem
  13. {
  14. public class MapAwakeSystem: AwakeSystem<Map, JObject, WNPlayer>
  15. {
  16. protected override void Awake(Map self, JObject opts, WNPlayer player)
  17. {
  18. Log.Debug($"创建场景实体...create area opts:{opts.ToString()}");
  19. self.RoomId = player.GetRoomId();
  20. self.createTime = TimeHelper.ServerNow();
  21. self.LogicServerId = opts.SelectToken("logicServerId") != null? Convert.ToInt32(opts.SelectToken("logicServerId")) : 0;
  22. self.MapId = Convert.ToInt32(opts.SelectToken("areaId"));
  23. self.Prop = MapConfigCategory.Instance.Get(self.MapId);
  24. self.Type = self.Prop.Type;
  25. self.Player = player;
  26. self.UnitPlayers = new Dictionary<string, Struct.UnitPlayerData>();
  27. self.DeadUnits = new List<int>();
  28. self.DeadUnitPlayer = new List<int>();
  29. // 战斗服事件组件
  30. self.AddComponent<MapEventComponent>();
  31. // 场景排行榜组件
  32. self.AddComponent<MapRankComponent>();
  33. // token为空过滤一下抖音相关组件
  34. if (self.TokenIsNull())
  35. {
  36. return;
  37. }
  38. // 抖音直播评论任务组件
  39. self.AddComponent<MapDouyinLiveCommentComponent>();
  40. // 抖音直播礼物任务组件
  41. self.AddComponent<MapDouyinLiveGiftComponent>();
  42. // 抖音直播点赞任务组件
  43. self.AddComponent<MapDouyinLiveLikeComponent>();
  44. // 抖音直播礼物置顶
  45. self.DomainScene().GetComponent<GameDouyinComponent>().TopGifts(self.RoomId);
  46. }
  47. }
  48. public class MapDestroySystem: DestroySystem<Map>
  49. {
  50. protected override void Destroy(Map self)
  51. {
  52. Log.Info($"销毁场景");
  53. }
  54. }
  55. public static ZoneManagerPrx GetZoneManager(this Map self)
  56. {
  57. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceZoneManager;
  58. }
  59. public static XmdsManagerPrx GetXmdsManager(this Map self)
  60. {
  61. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceXmdsManager;
  62. }
  63. public static GetPlayerData GetPlayerData(this Map self, long playerId)
  64. {
  65. string result = self.GetXmdsManager().getPlayerData(playerId.ToString().Trim(), true);
  66. return string.IsNullOrEmpty(result)? null : JsonConvert.DeserializeObject<GetPlayerData>(result);
  67. }
  68. /// <summary>
  69. /// 从战斗服同步角色数据
  70. /// </summary>
  71. /// <param name="self"></param>
  72. /// <param name="player"></param>
  73. public static void SyncPlayerHistoryData(this Map self, WNPlayer player)
  74. {
  75. GetPlayerData result = self.GetPlayerData(player.GetId());
  76. if (result == null)
  77. {
  78. return;
  79. }
  80. player.GetComponent<PlayerTempDataComponent>().SyncNowData(self.MapId, self.InstanceId, result);
  81. player.GetComponent<PlayerTempDataComponent>().SyncHistoryData(self.Prop, self.InstanceId, result);
  82. }
  83. /// <summary>
  84. /// 获取场景角色
  85. /// </summary>
  86. /// <param name="self"></param>
  87. /// <param name="playerId"></param>
  88. /// <returns></returns>
  89. public static WNPlayer GetPlayer(this Map self, long playerId)
  90. {
  91. return self.Player;
  92. }
  93. /// <summary>
  94. /// 分配阵营
  95. /// </summary>
  96. /// <param name="self"></param>
  97. /// <param name="player"></param>
  98. public static void SetForce(this Map self, WNPlayer player)
  99. {
  100. player.Force = (int)AreaForce.FORCEA;
  101. }
  102. /// <summary>
  103. /// 移除角色,通用框架接口 切换场景/掉线会自动调用,尽量 不要手动调用
  104. /// </summary>
  105. /// <param name="self"></param>
  106. /// <param name="player"></param>
  107. /// <param name="keepObject"></param>
  108. public static void RemovePlayer(this Map self, WNPlayer player, bool keepObject)
  109. {
  110. Log.Info($"removePlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  111. WNPlayer actorPlayer = self.GetPlayer(player.GetId());
  112. if (actorPlayer != null)
  113. {
  114. // self.Players.Remove(player.GetId());
  115. }
  116. // 重置进入场景标识
  117. player.GetComponent<PlayerTempDataComponent>().MapData.ready = false;
  118. }
  119. /// <summary>
  120. /// 玩家进入场景请求
  121. /// </summary>
  122. /// <param name="self"></param>
  123. /// <param name="player"></param>
  124. public static void PlayerEnterRequest(this Map self, WNPlayer player)
  125. {
  126. bool ready = player.GetComponent<PlayerTempDataComponent>().MapData.ready;
  127. if (ready)
  128. {
  129. Log.Info($"PlayerEnterRequest: playerId={player.GetId()}, ready={ready}");
  130. return;
  131. }
  132. try
  133. {
  134. string enterData = player.ToJSON4EnterScene(self);
  135. Log.Debug($"{player.GetName()}, enterSceneData:{enterData}");
  136. self.GetZoneManager().playerEnterRequest(player.GetId().ToString().Trim(), self.Id.ToString().Trim(), enterData);
  137. }
  138. catch (Exception e)
  139. {
  140. Log.Warning(
  141. $"playerEnterRequest: playerName={player.GetName()}, instanceId={self.Id}, mapName={self.Prop.Name}, serverID={self.BattleServerId}, e={e}");
  142. }
  143. }
  144. /// <summary>
  145. /// 场景添加角色
  146. /// </summary>
  147. /// <param name="self"></param>
  148. /// <param name="player"></param>
  149. public static void AddPlayer(this Map self, WNPlayer player)
  150. {
  151. Log.Info($"addPlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  152. player.Map = self;
  153. self.SetForce(player);
  154. self.Player = player;
  155. }
  156. /// <summary>
  157. /// 玩家进场景后推的消息
  158. /// </summary>
  159. /// <param name="self"></param>
  160. /// <param name="player"></param>
  161. public static void PlayerReady(this Map self, WNPlayer player)
  162. {
  163. }
  164. /// <summary>
  165. /// 角色成功进入场景
  166. /// </summary>
  167. /// <param name="self"></param>
  168. /// <param name="player"></param>
  169. public static void PlayerEntered(this Map self, WNPlayer player)
  170. {
  171. }
  172. /// <summary>
  173. /// 玩家登录事件
  174. /// </summary>
  175. /// <param name="self"></param>
  176. /// <param name="player"></param>
  177. public static void PlayerLogin(this Map self, WNPlayer player)
  178. {
  179. }
  180. /// <summary>
  181. /// 玩家离开场景请求
  182. /// </summary>
  183. /// <param name="self"></param>
  184. /// <param name="player"></param>
  185. /// <param name="keepObject"></param>
  186. public static void PlayerLeaveRequest(this Map self, WNPlayer player, bool keepObject)
  187. {
  188. try
  189. {
  190. self.GetZoneManager().playerLeaveRequest(player.GetId().ToString().Trim(), self.InstanceId.ToString().Trim(), keepObject);
  191. }
  192. catch (Exception e)
  193. {
  194. Log.Warning($"playerLeaveRequest: catch - {e}");
  195. }
  196. Log.Debug($"playerLeaveRequest--------------------{player.GetName()} - {self.InstanceId} - {self.Prop.Name}");
  197. }
  198. /// <summary>
  199. /// 绑定战斗服
  200. /// </summary>
  201. /// <param name="self"></param>
  202. /// <param name="player"></param>
  203. /// <param name="serverId"></param>
  204. public static void BindBattleServer(this Map self, WNPlayer player, string serverId)
  205. {
  206. self.BattleServerId = serverId;
  207. }
  208. /// <summary>
  209. /// 创建单位
  210. /// </summary>
  211. /// <param name="self"></param>
  212. /// <param name="data"></param>
  213. /// <param name="needReturn"></param>
  214. /// <returns></returns>
  215. public static async ETTask<int> AddUnits(this Map self, List<Struct.MonsterUnit> data, bool needReturn)
  216. {
  217. if (data.Count <= 0)
  218. {
  219. return 0;
  220. }
  221. int addUnitsResult = 0;
  222. if (needReturn) {
  223. addUnitsResult = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(data, Formatting.Indented));
  224. Log.Info($"addUnits needReturn : mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, data={data}, add units result={addUnitsResult}");
  225. } else
  226. {
  227. int objId = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(data, Formatting.Indented));
  228. Log.Info($"addUnits: mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, objId={objId}");
  229. }
  230. await ETTask.CompletedTask;
  231. return addUnitsResult;
  232. }
  233. public static async ETTask<int> AddUnits(this Map self, Struct.MonsterUnit data, bool needReturn)
  234. {
  235. List<Struct.MonsterUnit> listData = new List<Struct.MonsterUnit>();
  236. listData.Add(data);
  237. return await self.AddUnits(listData, needReturn);
  238. }
  239. /// <summary>
  240. /// 移除单位,战斗服创建的unit
  241. /// </summary>
  242. /// <param name="self"></param>
  243. /// <param name="objectId"></param>
  244. public static async ETTask RemovePointUnit(this Map self, int objectId)
  245. {
  246. self.GetXmdsManager().removePointUnit(self.Id.ToString().Trim(), objectId);
  247. await ETTask.CompletedTask;
  248. }
  249. /// <summary>
  250. /// 场景复活数据
  251. /// </summary>
  252. /// <param name="self"></param>
  253. /// <param name="type">1:原地复活;2:出生点复活;3:复活点复活. 4:技能复活.</param>
  254. public static string ReliveData(this Map self, ReliveType type)
  255. {
  256. JObject jsonObject = new ();
  257. jsonObject.Add("type", (int)type);
  258. jsonObject.Add("qty", 0);
  259. jsonObject.Add("itemType", "diamond");
  260. jsonObject.Add("hp", 8);
  261. jsonObject.Add("mp", 0);
  262. return JsonConvert.SerializeObject(jsonObject, Formatting.Indented);
  263. }
  264. /// <summary>
  265. /// 是否结束
  266. /// </summary>
  267. /// <param name="self"></param>
  268. /// <returns></returns>
  269. public static bool IsGameOver(this Map self)
  270. {
  271. return self.IsGameOver;
  272. }
  273. /// <summary>
  274. /// 增加单位玩家本地数据
  275. /// </summary>
  276. /// <param name="self"></param>
  277. /// <param name="openId"></param>
  278. /// <param name="templateId"></param>
  279. /// <param name="objId"></param>
  280. /// <param name="likes"></param>
  281. /// <param name="name"></param>
  282. /// <param name="url"></param>
  283. public static Struct.UnitPlayerData AddUnitPlayer(this Map self, string openId, int templateId, int objId, long likes, string name, string url)
  284. {
  285. if (string.IsNullOrEmpty(openId) || templateId <= 0 || objId <= 0 || self.IsGameOver())
  286. {
  287. return null;
  288. }
  289. Struct.UnitPlayerData unitPlayerData = null;
  290. if (self.UnitPlayers.ContainsKey(openId))
  291. {
  292. unitPlayerData = self.UnitPlayers[openId];
  293. unitPlayerData.TemplateId = templateId;
  294. unitPlayerData.ObjId = objId;
  295. unitPlayerData.Name = name;
  296. unitPlayerData.Url = url;
  297. unitPlayerData.Map = self;
  298. self.UnitPlayers[openId] = unitPlayerData;
  299. }
  300. else
  301. {
  302. unitPlayerData = new Struct.UnitPlayerData();
  303. unitPlayerData.OpenId = openId;
  304. unitPlayerData.TemplateId = templateId;
  305. unitPlayerData.ObjId = objId;
  306. unitPlayerData.Name = name;
  307. unitPlayerData.Url = url;
  308. unitPlayerData.Level = 1;
  309. unitPlayerData.Likes = 0;
  310. unitPlayerData.ReliveTime = 0;
  311. unitPlayerData.x = 0;
  312. unitPlayerData.y = 0;
  313. unitPlayerData.Map = self;
  314. self.UnitPlayers.TryAdd(openId, unitPlayerData);
  315. }
  316. return unitPlayerData;
  317. }
  318. /// <summary>
  319. /// 获取单位玩家
  320. /// </summary>
  321. /// <param name="self"></param>
  322. /// <param name="openId"></param>
  323. /// <returns></returns>
  324. public static Struct.UnitPlayerData GetUnitPlayerByOpenId(this Map self, string openId)
  325. {
  326. if (self.UnitPlayers.TryGetValue(openId, out Struct.UnitPlayerData unitPlayer))
  327. {
  328. return unitPlayer;
  329. }
  330. return null;
  331. }
  332. /// <summary>
  333. /// 获取单位玩家
  334. /// </summary>
  335. /// <param name="self"></param>
  336. /// <param name="objId"></param>
  337. /// <returns></returns>
  338. public static Struct.UnitPlayerData GetUnitPlayerByObjId(this Map self, int objId)
  339. {
  340. return self.UnitPlayers.Values.FirstOrDefault(unitPlayer => unitPlayer != null && unitPlayer.ObjId == objId);
  341. }
  342. /// <summary>
  343. /// 玩家增加贡献值
  344. /// </summary>
  345. /// <param name="self"></param>
  346. /// <param name="openId"></param>
  347. /// <param name="addValue"></param>
  348. public static void AddContributeValue(this Map self, string openId, long addValue)
  349. {
  350. Struct.UnitPlayerData unitPlayerData = self.GetUnitPlayerByOpenId(openId);
  351. if (unitPlayerData == null)
  352. {
  353. return;
  354. }
  355. if (self.IsGameOver())
  356. {
  357. return;
  358. }
  359. long value = unitPlayerData.ContributeValue + addValue;
  360. if (value >= long.MaxValue)
  361. {
  362. value = long.MaxValue;
  363. }
  364. unitPlayerData.ContributeValue = value;
  365. }
  366. /// <summary>
  367. /// 获取抖音直播接口调用凭证
  368. /// </summary>
  369. /// <param name="self"></param>
  370. /// <returns></returns>
  371. public static string GetDouyinAccessToken(this Map self)
  372. {
  373. return self.DomainScene().GetComponent<GameDouyinComponent>().AccessToken;
  374. }
  375. /// <summary>
  376. /// Token 是否为空
  377. /// </summary>
  378. /// <param name="self"></param>
  379. /// <returns></returns>
  380. public static bool TokenIsNull(this Map self)
  381. {
  382. return self.DomainScene().GetComponent<GameDouyinComponent>().TokenIsNull;
  383. }
  384. /// <summary>
  385. /// 获得当前战场(当前塔位置),随机位置
  386. /// </summary>
  387. /// <param name="self"></param>
  388. /// <returns></returns>
  389. public static Vector2 GetRandomPlayerPos(this Map self)
  390. {
  391. Vector2[] TowerPos = { new Vector2() { X = 103, Y = 197 }, new Vector2() { X = 190, Y = 133 }, new Vector2() { X = 104, Y = 69 } };
  392. int index = self.CurBattleIndex;
  393. Vector2 tower = TowerPos[index];
  394. Random rand = new Random();
  395. float r;
  396. double ang;
  397. if (index == 0)
  398. {
  399. r = (float)Math.Sqrt(rand.Next(2500)) + 10;
  400. ang = rand.Next(22) / 180.0f * Math.PI + Math.PI * 78f / 180f + Math.PI;
  401. }
  402. else if (index == 1)
  403. {
  404. r = (float)Math.Sqrt(rand.Next(3136)) + 10;
  405. ang = rand.Next(20) / 180.0f * Math.PI + Math.PI * 76f / 180f;
  406. }
  407. else
  408. {
  409. r = (float)Math.Sqrt(rand.Next(2500)) + 10;
  410. ang = rand.Next(22) / 180.0f * Math.PI + Math.PI * 150f / 180f;
  411. }
  412. return new Vector2(tower.X + (float)(r * Math.Cos(ang)), tower.Y - (float)(r * Math.Sin(ang)));
  413. }
  414. }
  415. }