MapComponent.cs 26 KB

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