MapComponent.cs 26 KB

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