MapComponent.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. using FL.Battle;
  2. using FL.Data;
  3. using FL.FGUI;
  4. using FL.Map;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UnityEngine;
  8. using XGame;
  9. using XGame.Database;
  10. using XGame.Framework;
  11. using XGame.Framework.Map;
  12. using XGame.Framework.Nodes;
  13. using XGame.Framework.Time;
  14. namespace FL.Nodes.GameMain
  15. {
  16. public class MapComponent : NodeComponent
  17. {
  18. private int _chapterId;
  19. private EMapSchedule _schedule;
  20. private ITimer _monsterTimer;
  21. /// <summary>
  22. /// boss战斗倒计时
  23. /// </summary>
  24. private ITimer _bossTimer;
  25. /// <summary>
  26. /// 是否挑战boss失败
  27. /// 失败后战斗进入该关卡的小怪节点循环
  28. /// UI显示挑战boss按钮
  29. /// </summary>
  30. private bool _isBossFailed;
  31. /// <summary>
  32. /// 是否在挑战boss
  33. /// </summary>
  34. private bool _isBossChallenge;
  35. private PlayerEntity _player;
  36. ///// <summary>
  37. ///// 怪物坐标相对出生点的偏移值
  38. ///// 怪物最多13个,分两排排列,索引如下
  39. ///// 二排 9 5 1 2 6 10
  40. ///// 一排 11 7 3 0 4 8 12
  41. ///// 0的左边偏移值为负数
  42. ///// </summary>
  43. //private Vector3 _monsterOffset = new Vector3(0.8f, 1.5f, 0);
  44. ///// <summary>
  45. ///// 空闲的怪物位置索引
  46. ///// </summary>
  47. //private List<int> _unusedIndexs;
  48. /// <summary>
  49. /// 怪物的实例对象
  50. /// </summary>
  51. private Dictionary<long, MonsterEntity> _monsterMap;
  52. /// <summary>
  53. /// 伙伴的实例对象
  54. /// </summary>
  55. private Dictionary<long, PartnerEntity> _partnerMap;
  56. private MapKey _currentMap;
  57. public override void OnEnable(object intent)
  58. {
  59. EventSingle.Instance.AddListener(EventDefine.GameMainMapRemoveEntity, OnRemoveEntity);
  60. EventSingle.Instance.AddListener(EventDefine.GameMainMapEndMask, OnMapEndMask);
  61. EventSingle.Instance.AddListener(EventDefine.PartnerDown, OnPartnerDown);
  62. EventSingle.Instance.AddListener(EventDefine.PartnerUp, OnPartnerUp);
  63. EventSingle.Instance.AddListener(EventDefine.PartnerPlanChange, OnPartnersChanged);
  64. EventSingle.Instance.AddListener(EventDefine.PartnerWearAll, OnPartnersChanged);
  65. EventSingle.Instance.AddListener(EventDefine.GameMainMapChallengeBoss, OnChallengeBoss);
  66. //InitMonsters();
  67. StartMap(PlayerData.Instance.chapterId);
  68. }
  69. public override void OnDisable()
  70. {
  71. EventSingle.Instance.RemoveListener(EventDefine.GameMainMapRemoveEntity, OnRemoveEntity);
  72. EventSingle.Instance.RemoveListener(EventDefine.GameMainMapEndMask, OnMapEndMask);
  73. EventSingle.Instance.RemoveListener(EventDefine.PartnerDown, OnPartnerDown);
  74. EventSingle.Instance.RemoveListener(EventDefine.PartnerUp, OnPartnerUp);
  75. EventSingle.Instance.RemoveListener(EventDefine.PartnerPlanChange, OnPartnersChanged);
  76. EventSingle.Instance.RemoveListener(EventDefine.PartnerWearAll, OnPartnersChanged);
  77. EventSingle.Instance.RemoveListener(EventDefine.GameMainMapChallengeBoss, OnChallengeBoss);
  78. _player = null;
  79. _partnerMap?.Clear();
  80. _monsterTimer?.Cancel();
  81. _monsterTimer = null;
  82. _bossTimer?.Cancel();
  83. _bossTimer = null;
  84. _isBossFailed = false;
  85. _isBossChallenge = false;
  86. _monsterMap?.Clear();
  87. //_unusedIndexs?.Clear();
  88. Context.Map.Close(_currentMap);
  89. _currentMap = null;
  90. }
  91. //private void InitMonsters()
  92. //{
  93. // if (_unusedIndexs == null)
  94. // _unusedIndexs = new List<int>();
  95. // else
  96. // _unusedIndexs.Clear();
  97. // for (var i = 0; i < 13; i++)
  98. // {
  99. // _unusedIndexs.Add(i);
  100. // }
  101. //}
  102. private void StartMap(int chapterId)
  103. {
  104. _chapterId = chapterId;
  105. _schedule = EMapSchedule.LoadingMap;
  106. var chapter = ChapterTableRepo.Get(_chapterId);
  107. var map = MapTableRepo.Get(chapter.Map);
  108. var mapKey = MapKeys.NameToKey(map.AssetName);
  109. if (mapKey == _currentMap)
  110. { // 是同一个地图
  111. StartChapter(true, false);
  112. }
  113. else
  114. {
  115. // 关闭当前地图
  116. Context.Map.Close(_currentMap);
  117. _currentMap = mapKey;
  118. // 加载新地图
  119. var mapAsync = Context.Map.RunAsync(mapKey);
  120. mapAsync.On(_ =>
  121. {
  122. StartChapter(true, true);
  123. });
  124. }
  125. }
  126. private void StartChapter(bool isChapterChanged, bool isMapChanged)
  127. {
  128. _schedule = EMapSchedule.Monster1;
  129. var chapter = ChapterTableRepo.Get(_chapterId);
  130. var map = MapTableRepo.Get(chapter.Map);
  131. var dto = new LoadEntitiesDto();
  132. dto.chapterId = _chapterId;
  133. dto.isChapterChanged = isChapterChanged;
  134. dto.isBossPart = chapter.Part_type == 1;
  135. dto.isPlayerMove = true;// isChapterChanged || !_isBossFailed;
  136. //战斗位置
  137. dto.battlePosition = GetBattlePosition(map, chapter.Part);// map.BattlePositions[chapter.Part - 1].ToVector3();
  138. var isGenPlayer = _player == null;
  139. if (isGenPlayer)
  140. {
  141. _player = GenPlayer(map.PlayerBirth.ToVector3());
  142. //玩家
  143. dto.player = _player;
  144. //伙伴
  145. var partners = GenPartners(_player.BirthPosition);
  146. AddPartners(partners);
  147. dto.partners = partners;
  148. }
  149. else if (isMapChanged)
  150. {
  151. //玩家
  152. dto.player = _player;
  153. //伙伴
  154. dto.partners = _partnerMap.Values.ToArray();
  155. }
  156. if (isChapterChanged || dto.isBossPart)
  157. {
  158. // 重置玩家血量
  159. _player.Attr.Dynamic.Clear();
  160. _player.Attr.ResetHp();
  161. }
  162. if (isChapterChanged)
  163. {
  164. // 随从可能有替换过,重置随从的出生点
  165. var birthPosition = _player.BirthPosition;
  166. foreach (var partner in _partnerMap.Values)
  167. {
  168. partner.BirthPosition = birthPosition + PartnersData.Instance.GetPartnerOffset(partner.Attr.UID);
  169. }
  170. }
  171. //怪物数据
  172. var monsters = GenMonsters();
  173. AddMonsters(monsters);
  174. dto.monsters = monsters;
  175. dto.monsterMoveDistance = map.MonsterBirth.Y - map.BattleFirst.Y;// map.MonsterPositions[chapter.Part - 1].ToVector3().y - dto.battlePosition.y;
  176. //定时器
  177. if (!dto.isBossPart)
  178. { // 普通节点,增加定时器
  179. //dto.monsterMoveDelay = 0;
  180. if (_monsterTimer != null)
  181. { // 理论上不会出现这个错误
  182. _monsterTimer.Cancel();
  183. Log.Error($"StartChapter _monsterTimer error. chapterId:{_chapterId}");
  184. }
  185. _monsterTimer = Context.Time.AddDelayLooperTimer(chapter.Interval * 20, chapter.Interval * 20, OnLoopGenMonsters);
  186. }
  187. else
  188. { // boss关卡,直接到最后
  189. //dto.monsterMoveDelay = 1000;
  190. _schedule = EMapSchedule.Monster5;
  191. Context.UI.OpenAsync(UIKeys.MainBossLaiXi);
  192. Context.Time.AddDelayTimer(1000, () =>
  193. { //1秒后自动关闭
  194. Context.UI.Close(UIKeys.MainBossLaiXi);
  195. if (chapter.Time > 0)
  196. {// boss战斗倒计时
  197. _bossTimer = Context.Time.AddDelayTimer(chapter.Time, () =>
  198. {
  199. CombatFailed();
  200. });
  201. EventSingle.Instance.Notify(EventDefine.GameMainMapBossStart, chapter.Id);
  202. }
  203. });
  204. }
  205. EventSingle.Instance.Notify(EventDefine.GameMainMapLoadEntities, dto);
  206. }
  207. private void OnLoopGenMonsters(int times)
  208. {
  209. _schedule += 1;
  210. if (_schedule == EMapSchedule.Monster5)
  211. {
  212. _monsterTimer.Cancel();
  213. _monsterTimer = null;
  214. }
  215. var chapter = ChapterTableRepo.Get(_chapterId);
  216. var map = MapTableRepo.Get(chapter.Map);
  217. var dto = new LoadEntitiesDto();
  218. dto.chapterId = _chapterId;
  219. //战斗位置
  220. dto.battlePosition = GetBattlePosition(map, chapter.Part);// map.BattlePositions[chapter.Part - 1].ToVector3();
  221. //怪物数据
  222. var monsters = GenMonsters();
  223. AddMonsters(monsters);
  224. dto.monsters = monsters;
  225. dto.monsterMoveDistance = map.MonsterBirth.Y - map.BattleFirst.Y;// map.MonsterPositions[chapter.Part - 1].ToVector3().y - dto.battlePosition.y;
  226. EventSingle.Instance.Notify(EventDefine.GameMainMapLoadEntities, dto);
  227. }
  228. private PlayerEntity GenPlayer(Vector3 birth)
  229. {
  230. var playerData = PlayerData.Instance;
  231. var player = new PlayerEntity()
  232. {
  233. BirthPosition = birth//玩家出生点
  234. };
  235. var entityAttr = new EntityAttributes(0, playerData.UID)
  236. {
  237. Name = playerData.Name,
  238. Radius = 1,
  239. AttackCD = 2000,
  240. };
  241. entityAttr.CopyFrom(playerData.Attr);
  242. player.Init(UIDDefine.New(), entityAttr, playerData.skillIds);
  243. return player;
  244. }
  245. private PartnerEntity[] GenPartners(Vector3 targetPosition)
  246. {
  247. var goneUpMap = PartnersData.Instance.UpPartnerUIDMap;
  248. var count = goneUpMap.Count;
  249. var partners = new PartnerEntity[count];
  250. var index = 0;
  251. foreach (var partnerUID in goneUpMap.Keys)
  252. {
  253. partners[index++] = GenPartner(targetPosition, partnerUID);
  254. }
  255. return partners;
  256. }
  257. private PartnerEntity GenPartner(Vector3 targetPosition, long partnerId)
  258. {
  259. var partner = PartnersData.Instance.GetPartnerAttrByUID(partnerId);
  260. var entity = new PartnerEntity()
  261. {
  262. BirthPosition = targetPosition + PartnersData.Instance.GetPartnerOffset(partner.UID),
  263. EpigraphId = 20300,
  264. };
  265. var entityAttr = new EntityAttributes(partner.TableId, partner.UID)
  266. {
  267. Name = partner.Name,
  268. Radius = partner.Radius,
  269. AttackCD = 6000,
  270. };
  271. entityAttr.CopyFrom(partner.Attr);
  272. var table = PartnerTableRepo.Get(partner.TableId);
  273. entity.Init(UIDDefine.New(), entityAttr, new int[] { partner.SkillId });
  274. return entity;
  275. }
  276. private void AddPartners(PartnerEntity[] partners)
  277. {
  278. if (_partnerMap == null)
  279. {
  280. _partnerMap = new Dictionary<long, PartnerEntity>();
  281. }
  282. if (partners == null || partners.Length == 0) return;
  283. foreach (var partner in partners)
  284. {
  285. _partnerMap.Add(partner.EntityId, partner);
  286. }
  287. }
  288. private Vector3 GetMonsterBirth(Vector3 position, int index, int cols, out int row)
  289. {
  290. row = 0;
  291. if (cols < 2)
  292. return position;
  293. var offset = 3;
  294. if (cols == 2)
  295. {
  296. position.x -= offset;
  297. offset *= 2;
  298. }
  299. else if (cols == 3)
  300. {
  301. position.x -= offset;
  302. }
  303. else
  304. {
  305. // 最多支持4列
  306. cols = 4;
  307. // -4.5, -1.5, 1.5, 4.5
  308. position.x -= (offset * 1.5f);
  309. }
  310. row = index / cols;
  311. position.x += (index % cols) * offset;
  312. return position;
  313. }
  314. ///// <summary>
  315. ///// 获取怪物的出生坐标和索引
  316. ///// </summary>
  317. ///// <param name="position"></param>
  318. ///// <param name="positionIdx"></param>
  319. ///// <returns></returns>
  320. //private Vector3 GetMonsterBirth(Vector3 position, out int positionIdx)
  321. //{
  322. // if (_unusedIndexs.Count > 0)
  323. // {
  324. // positionIdx = _unusedIndexs[0];
  325. // _unusedIndexs.RemoveAt(0);
  326. // }
  327. // else
  328. // {
  329. // positionIdx = 0;
  330. // }
  331. // if (positionIdx == 0)
  332. // {
  333. // return position;
  334. // }
  335. // var offset = Vector3.zero;
  336. // var xMultiple = (positionIdx + 1) / 2;
  337. // offset.x = _monsterOffset.x * xMultiple;
  338. // if (positionIdx % 2 != 0)
  339. // { // 索引为奇数的在左边
  340. // offset.x = -offset.x;
  341. // }
  342. // if (xMultiple % 2 != 0)
  343. // { // 倍数为奇数的在上面
  344. // offset.y = _monsterOffset.y;
  345. // }
  346. // var target = position + offset;
  347. // // 随机偏移xy(-0.2, 0.2)
  348. // target.x += Random.Range(-0.25f, 0.25f);
  349. // target.y += Random.Range(-0.25f, 0.25f);
  350. // return target;
  351. //}
  352. //private void PushMonstPositionIdx(int positionIdx)
  353. //{
  354. // if (_unusedIndexs.Contains(positionIdx))
  355. // return; // 避免重复记录
  356. // for (var i = 0; i < _unusedIndexs.Count; i++)
  357. // {
  358. // if (positionIdx < _unusedIndexs[i] && (i == 0 || positionIdx > _unusedIndexs[i - 1]))
  359. // { // 小于当前索引的值 且 大于(i - 1 )的值
  360. // _unusedIndexs.Insert(i, positionIdx);
  361. // return;
  362. // }
  363. // }
  364. // _unusedIndexs.Add(positionIdx);
  365. //}
  366. private MonsterEntity[] GenMonsters()
  367. {
  368. var chapter = ChapterTableRepo.Get(_chapterId);
  369. var map = MapTableRepo.Get(chapter.Map);
  370. Vector3 birthPosition = GetMonsterBirth(map, chapter.Part);// map.MonsterPositions[chapter.Part - 1].ToVector3();
  371. int[] monsterRefresh = null;
  372. switch (_schedule)
  373. {
  374. case EMapSchedule.Monster1:
  375. monsterRefresh = chapter.Monster_refresh1;
  376. break;
  377. case EMapSchedule.Monster2:
  378. monsterRefresh = chapter.Monster_refresh2;
  379. break;
  380. case EMapSchedule.Monster3:
  381. monsterRefresh = chapter.Monster_refresh3;
  382. break;
  383. case EMapSchedule.Monster4:
  384. monsterRefresh = chapter.Monster_refresh4;
  385. break;
  386. case EMapSchedule.Monster5:
  387. monsterRefresh = chapter.Monster_refresh5;
  388. break;
  389. default:
  390. break;
  391. }
  392. var monsterCount = (monsterRefresh != null && monsterRefresh.Length > 0) ? monsterRefresh[1] : 0;
  393. if (monsterCount > 0)
  394. { // 数量0-3
  395. var monsters = new MonsterEntity[monsterCount];
  396. var cols = monsterRefresh[2]; // 敌人列数
  397. var curve = monsterRefresh[3]; //移动轨迹
  398. var isBossPart = chapter.Part_type == 1;
  399. for (var i = 0; i < monsterCount; i++)
  400. {
  401. var position = GetMonsterBirth(birthPosition, i, cols, out var row);
  402. var monsterTab = MonsterTableRepo.Get(monsterRefresh[0]);
  403. var entityAttr = new EntityAttributes(monsterTab.Id, UIDDefine.New())
  404. {
  405. Name = monsterTab.Name,
  406. Radius = monsterTab.ModelSize * 0.5f,
  407. AttackCD = 3000,
  408. };
  409. entityAttr.Static.Hp = monsterTab.Hp;
  410. entityAttr.Static.HpLimit = monsterTab.Hp;
  411. entityAttr.Static.MoveSpeed = Mathf.RoundToInt(monsterTab.Speed);
  412. entityAttr.Static.Attack = monsterTab.Atk;
  413. entityAttr.CurrentHp = monsterTab.Hp;
  414. var entity = new MonsterEntity();
  415. entity.Init(UIDDefine.New(), entityAttr, monsterTab.Skills);
  416. entity.BirthPosition = position;
  417. entity.BirthDelay = row * chapter.Interval + (isBossPart ? 1000 : 0); // boss额外加1000毫秒延迟
  418. entity.BirthCurve = curve;
  419. entity.IsBoss = monsterTab.Type == 2;
  420. monsters[i] = entity;
  421. }
  422. return monsters;
  423. }
  424. return null;
  425. }
  426. private void AddMonsters(MonsterEntity[] monsters)
  427. {
  428. if (monsters == null || monsters.Length == 0) return;
  429. if (_monsterMap == null)
  430. {
  431. _monsterMap = new Dictionary<long, MonsterEntity>();
  432. }
  433. foreach (var monsterInfo in monsters)
  434. {
  435. _monsterMap.Add(monsterInfo.EntityId, monsterInfo);
  436. }
  437. }
  438. #region 事件监听
  439. private void OnRemoveEntity(int eventId, object args)
  440. {
  441. var entityId = (long)args;
  442. if (_monsterMap.TryGetValue(entityId, out var entity))
  443. {
  444. _monsterMap.Remove(entityId);
  445. //PushMonstPositionIdx(entity.PositionIdx);
  446. CheckMonsterAllDead();
  447. return;
  448. }
  449. if (_partnerMap.TryGetValue(entityId, out var partner))
  450. {
  451. _partnerMap.Remove(entityId);
  452. return;
  453. }
  454. if (entityId == _player.EntityId)
  455. {
  456. //TODO 失败结算
  457. CombatFailed();
  458. }
  459. }
  460. private void OnPartnerDown(int eventId, object args)
  461. {
  462. var partnerUID = (long)args;
  463. var entityId = GetPartnerEntityIdByUID(partnerUID);
  464. if (entityId == 0)
  465. return;
  466. EventSingle.Instance.Notify(EventDefine.GameMainMapRemoveEntity, entityId);
  467. }
  468. private void OnPartnerUp(int eventId, object args)
  469. {
  470. var partnerUID = (long)args;
  471. var entityId = GetPartnerEntityIdByUID(partnerUID);
  472. if (entityId != 0)
  473. return; // 已经有实例对象了
  474. var chapter = ChapterTableRepo.Get(_chapterId);
  475. var map = MapTableRepo.Get(chapter.Map);
  476. var dto = new LoadEntitiesDto();
  477. dto.chapterId = _chapterId;
  478. //战斗位置
  479. dto.battlePosition = GetBattlePosition(map, chapter.Part);// map.BattlePositions[chapter.Part - 1].ToVector3();
  480. var partner = GenPartner(dto.battlePosition, partnerUID);
  481. var partners = new PartnerEntity[] { partner };
  482. AddPartners(partners);
  483. dto.partners = partners;
  484. EventSingle.Instance.Notify(EventDefine.GameMainMapLoadEntities, dto);
  485. }
  486. private void OnPartnersChanged(int eventId, object args)
  487. {
  488. //TODO 临时代码
  489. if (_partnerMap == null) return;
  490. var partnerIds = _partnerMap.Keys.ToList();
  491. _partnerMap.Clear();
  492. foreach (var partnerId in partnerIds)
  493. {
  494. EventSingle.Instance.Notify(EventDefine.GameMainMapRemoveEntity, partnerId);
  495. }
  496. var chapter = ChapterTableRepo.Get(_chapterId);
  497. var map = MapTableRepo.Get(chapter.Map);
  498. var dto = new LoadEntitiesDto();
  499. dto.chapterId = _chapterId;
  500. //战斗位置
  501. dto.battlePosition = GetBattlePosition(map, chapter.Part);// map.BattlePositions[chapter.Part - 1].ToVector3();
  502. var partners = GenPartners(dto.battlePosition);
  503. AddPartners(partners);
  504. dto.partners = partners;
  505. EventSingle.Instance.Notify(EventDefine.GameMainMapLoadEntities, dto);
  506. }
  507. private void OnChallengeBoss(int eventId, object args)
  508. {
  509. CombatFailed(true);
  510. }
  511. //private void OnExchangePartner(int eventId, object args)
  512. //{
  513. // var addPartners = (List<int>)args;
  514. // var activePartnerIds = PartnersData.Instance.ActiveUIDs;
  515. // var count = addPartners.Count;
  516. // // 从后往前移除
  517. // for (var i = 0; i < count; i++)
  518. // {
  519. // var partnerId = activePartnerIds[activePartnerIds.Count - 1];
  520. // activePartnerIds.RemoveAt(activePartnerIds.Count - 1);
  521. // var EntityId = GetPartnerUIDByTableId(partnerId);
  522. // if (EntityId == 0)
  523. // continue;
  524. // EventSingle.Instance.Notify(EventDefine.GameMainMapRemoveEntity, EntityId);
  525. // }
  526. // activePartnerIds.AddRange(addPartners);
  527. // var chapter = ChapterTableRepo.Get(_chapterId);
  528. // var map = MapTableRepo.Get(chapter.Map);
  529. // var dto = new LoadEntitiesDto();
  530. // dto.chapterId = _chapterId;
  531. // //战斗位置
  532. // dto.battlePosition = map.BattlePositions[chapter.Part - 1].ToVector3();
  533. // var partners = GenPartners(dto.battlePosition, addPartners);
  534. // AddPartners(partners);
  535. // dto.partners = partners;
  536. // EventSingle.Instance.Notify(EventDefine.GameMainMapLoadEntities, dto);
  537. //}
  538. private void OnMapEndMask(int eventId, object args)
  539. {
  540. var sign = (EMapCombatSign)args;
  541. var chapter = ChapterTableRepo.Get(_chapterId);
  542. int chapterId;
  543. if (sign is EMapCombatSign.Success)
  544. {
  545. var nextChapter = ChapterTableRepo.Next(_chapterId);
  546. //没找到新关卡,重新开始
  547. if (nextChapter == null || nextChapter.Chapter != chapter.Chapter)
  548. { // 没有新关卡 || 章节id变化
  549. //TODO 章节切换
  550. }
  551. chapterId = nextChapter?.Id ?? PlayerData.Instance.chapterId;
  552. _isBossChallenge = false;
  553. }
  554. else if (sign is EMapCombatSign.Break)
  555. {
  556. var bossChapter = ChapterTableRepo.FindNearestBoss(_chapterId);
  557. Assert.IsNotNull(bossChapter, $"找不到该关卡的boss节点. Id:{_chapterId}");
  558. chapterId = bossChapter.Id;
  559. _isBossFailed = false;
  560. _isBossChallenge = true;
  561. }
  562. else
  563. { // 失败,重置到本关卡第一个节点
  564. if (chapter.Part_type == 1)
  565. { // boss节点失败,进入本关卡小怪节点循环
  566. _isBossFailed = true;
  567. }
  568. chapterId = _chapterId / 10 * 10 + 1;
  569. _isBossChallenge = false;
  570. }
  571. StartMap(chapterId);
  572. }
  573. #endregion
  574. private void CheckMonsterAllDead()
  575. {
  576. if (_monsterMap.Count > 0 || (_player != null && _player.IsDead)) return;
  577. // 怪物全部死亡
  578. var chapter = ChapterTableRepo.Get(_chapterId);
  579. if (chapter.Part_type == 1)
  580. { //boss关卡
  581. _isBossFailed = false;
  582. //TODO 结算
  583. _bossTimer?.Cancel();
  584. _bossTimer = null;
  585. Context.UI.OpenAsync(UIKeys.MainBattleOverVictory);
  586. Context.Time.AddDelayTimer(2000, () =>
  587. { //2秒后自动关闭
  588. Context.UI.Close(UIKeys.MainBattleOverVictory);
  589. EventSingle.Instance.Notify(EventDefine.GameMainMapStartMask, EMapCombatSign.Success);
  590. });
  591. }
  592. else if (_schedule == EMapSchedule.Monster5)
  593. {
  594. var nextId = _chapterId + 1;
  595. if (_isBossFailed)
  596. { // boss节点失败,进入本关卡小怪节点循环
  597. var nextChapter = ChapterTableRepo.Get(nextId);
  598. if (nextChapter == null || nextChapter.Part_type == 1)
  599. {
  600. nextId = _chapterId / 10 * 10 + 1;
  601. }
  602. }
  603. _chapterId = nextId;
  604. StartChapter(false, false);
  605. }
  606. }
  607. /// <summary>
  608. /// 战斗失败
  609. /// </summary>
  610. private void CombatFailed(bool isBreak = false)
  611. {
  612. _monsterTimer?.Cancel();
  613. _monsterTimer = null;
  614. if (!isBreak)
  615. Context.UI.OpenAsync(UIKeys.MainBattleOverFail);
  616. var chapter = ChapterTableRepo.Get(_chapterId);
  617. // 清空怪物数据
  618. //foreach (var item in _monsterMap)
  619. //{
  620. // PushMonstPositionIdx(item.Value.PositionIdx);
  621. //}
  622. _monsterMap.Clear();
  623. EventSingle.Instance.Notify(EventDefine.GameMainMapStartMask, isBreak ? EMapCombatSign.Break : (EMapCombatSign)(chapter.Part_type + 1));
  624. }
  625. private long GetPartnerEntityIdByUID(long uid)
  626. {
  627. foreach (var item in _partnerMap)
  628. {
  629. if (item.Value.Attr.UID == uid)
  630. return item.Key;
  631. }
  632. return 0;
  633. }
  634. private Vector3 GetBattlePosition(MapTable map, int mapPart)
  635. {
  636. if (_isBossFailed || _isBossChallenge)
  637. mapPart = 1; // boss失败后战斗位置不再前移
  638. return map.BattleFirst.ToVector3() + map.PartMove.ToVector3() * (mapPart - 1);
  639. }
  640. private Vector3 GetMonsterBirth(MapTable map, int mapPart)
  641. {
  642. if (_isBossFailed || _isBossChallenge)
  643. mapPart = 1; // boss失败后战斗位置不再前移
  644. return map.MonsterBirth.ToVector3() + map.PartMove.ToVector3() * (mapPart - 1);
  645. }
  646. }
  647. }