BattleField.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class BattleField : LogicField
  5. {
  6. protected int mCurrentStateFrame = 0;
  7. protected int mBattleFrame = 0;
  8. protected List<Fighter> mFighters = new List<Fighter>();
  9. protected List<Fighter> mTeamFighters = new List<Fighter>();
  10. protected List<Fighter> mEnemyFighters = new List<Fighter>();
  11. protected List<ActorData> mTeamActors = new List<ActorData>();
  12. protected List<ActorData> mEnemyActors = new List<ActorData>();
  13. public List<Fighter> Fighters { get { return mFighters; } }
  14. public List<Fighter> TeamFighters { get { return mTeamFighters; } }
  15. public List<Fighter> EnemyFighters { get { return mEnemyFighters; } }
  16. public List<ActorData> TeamActors { get { return mTeamActors; } }
  17. public List<ActorData> EnemyActors { get { return mEnemyActors; } }
  18. public virtual int CurrentFieldState
  19. {
  20. get { return 0; }
  21. }
  22. public int CurrentStateFrame { get { return mCurrentStateFrame; } }
  23. public int BattleFrame { get { return mBattleFrame; } }
  24. public float FightingTime { get; protected set; }
  25. //最大战斗时长
  26. public float MaxFightingTime { get; protected set; }
  27. public FightingResult Result { get; protected set; }
  28. public virtual bool IsFightingState { get { return false; } }
  29. public virtual LevelItem BattleInfo { get { return null; } }
  30. public bool IsWin
  31. {
  32. get { return Result == FightingResult.Win; }
  33. }
  34. public virtual float FloorY
  35. {
  36. get { return 0; }
  37. }
  38. public Vector3 FloorCenter { get { return Position; } }
  39. public virtual Vector3 FighterForward { get { return Vector3.zero; } }
  40. public virtual Vector3 FieldCenter { get; set; }
  41. public virtual Vector3 FieldFocus { get; }
  42. public virtual bool killBoss { get; set; }
  43. public virtual bool CanKillBoss { get { return false; } }
  44. public virtual bool IsIdleState { get { return false; } }
  45. public virtual string Name
  46. {
  47. get { return "BattleField"; }
  48. }
  49. //是否正在播放录像
  50. private bool mIsPlayingRecorder = false;
  51. public bool IsPlayingRecorder
  52. {
  53. get { return mIsPlayingRecorder; }
  54. set { mIsPlayingRecorder = value; }
  55. }
  56. private BaseBattle inBattle = null;
  57. public BattleField(BaseBattle battle, Vector3 pos, Vector3 size, Vector3 forward)
  58. :base(pos,size,forward)
  59. {
  60. this.inBattle = battle;
  61. this.mBattleFrame = 0;
  62. }
  63. public virtual void Update(float deltaTime)
  64. {
  65. mCurrentStateFrame++;
  66. mBattleFrame++;
  67. if (IsFightingState)
  68. {
  69. //FightingTime += deltaTime;
  70. FightingTime = mCurrentStateFrame / Constants.frame_to_time;
  71. CheckBuffs(BuffTriggerType.Trigger_FixedTime);
  72. }
  73. }
  74. //往战场中添加战斗人员
  75. public virtual void AddFighter(Fighter fighter)
  76. {
  77. if (!mFighters.Contains(fighter))
  78. {
  79. if (fighter.IsTeamMember)
  80. {
  81. mTeamFighters.Add(fighter);
  82. if (!mTeamActors.Contains(fighter.Actor))
  83. mTeamActors.Add(fighter.Actor);
  84. }
  85. else
  86. {
  87. mEnemyFighters.Add(fighter);
  88. if (!mEnemyActors.Contains(fighter.Actor))
  89. mEnemyActors.Add(fighter.Actor);
  90. }
  91. mFighters.Add(fighter);
  92. fighter.OnEnterField(this);
  93. }
  94. }
  95. //从战场中移除战斗人员
  96. public virtual void RemoveFighter(Fighter fighter)
  97. {
  98. if (mFighters.Contains(fighter))
  99. {
  100. if (fighter.IsTeamMember)
  101. {
  102. mTeamFighters.Remove(fighter);
  103. mTeamActors.Remove(fighter.Actor);
  104. }
  105. else
  106. {
  107. mEnemyFighters.Remove(fighter);
  108. mEnemyActors.Remove(fighter.Actor);
  109. }
  110. mFighters.Remove(fighter);
  111. fighter.OnLeaveField(this);
  112. }
  113. }
  114. public virtual void OnFighterDie(Fighter fighter)
  115. {
  116. }
  117. public virtual void OnFighterRelive(Fighter fighter)
  118. {
  119. }
  120. public virtual void GetFieldSummonPos(int posVal, out Vector3 pos, out Vector3 rot)
  121. {
  122. pos = Vector3.zero;
  123. rot = Vector3.zero;
  124. }
  125. public virtual void Dispose()
  126. {
  127. ClearFighter();
  128. }
  129. List<Fighter> tempList = new List<Fighter>();
  130. //给玩家落位进行排序
  131. public void SortTeamFighters()
  132. {
  133. int tempCount = 0;
  134. for (int idx = (int)ProfessionType.Pro_Type_0; idx <= (int)ProfessionType.Pro_Type_Priest; idx++)
  135. {
  136. tempList.Clear();
  137. FindFightersByProfessionType(eTeamType.Friend,idx, ref tempList);
  138. for (int jdx = 0; jdx < tempList.Count; jdx++)
  139. {
  140. tempList[jdx].ReadyPosOrder = jdx + 1;
  141. }
  142. tempCount += tempList.Count;
  143. if (tempCount == mTeamFighters.Count) break;
  144. }
  145. }
  146. public void SortEnemyFighters()
  147. {
  148. int tempCount = 0;
  149. for (int idx = (int)ProfessionType.Pro_Type_0; idx <= (int)ProfessionType.Pro_Type_Priest; idx++)
  150. {
  151. tempList.Clear();
  152. FindFightersByProfessionType(eTeamType.Enemy,idx, ref tempList);
  153. for (int jdx = 0; jdx < tempList.Count; jdx++)
  154. {
  155. tempList[jdx].ReadyPosOrder = jdx + 1;
  156. }
  157. tempCount += tempList.Count;
  158. if (tempCount == mTeamFighters.Count) break;
  159. }
  160. }
  161. private void FindFightersByProfessionType(eTeamType teamType,int proType, ref List<Fighter> list)
  162. {
  163. if(teamType == eTeamType.Friend)
  164. {
  165. for (int idx = 0; idx < mTeamFighters.Count; idx++)
  166. {
  167. if (mTeamFighters[idx].Actor.Profession == (ProfessionType)proType && mTeamFighters[idx].IsPlayer)
  168. {
  169. list.Add(mTeamFighters[idx]);
  170. }
  171. }
  172. }
  173. else
  174. {
  175. for(int idx =0; idx < mEnemyFighters.Count;idx++)
  176. {
  177. if (mEnemyFighters[idx].Actor.Profession == (ProfessionType)proType && mEnemyFighters[idx].IsPlayer)
  178. {
  179. list.Add(mEnemyFighters[idx]);
  180. }
  181. }
  182. }
  183. }
  184. public void ClearFighter()
  185. {
  186. mFighters.Clear();
  187. mEnemyFighters.Clear();
  188. mTeamFighters.Clear();
  189. mTeamActors.Clear();
  190. mEnemyActors.Clear();
  191. }
  192. public void OnFightingStart()
  193. {
  194. FightingTime = 0;
  195. for (int i = 0; i < Fighters.Count; i++)
  196. {
  197. if (inBattle.IsPlayRecord)
  198. {
  199. inBattle.Recorder.ProcessFrameRecord(BattleRecorder.RecordType.FighterStartFighting, Fighters[i]);
  200. }
  201. else
  202. {
  203. Fighters[i].OnFightingStart();
  204. }
  205. }
  206. CheckFightersPassiveSkill();
  207. //刷新被动 buff
  208. inBattle.FighterMgr.FixedUpdate(0.0f);
  209. CheckBuffs(BuffTriggerType.Trigger_Begin_Fighting);
  210. BattleMgr.Instance.OnFightingStart();
  211. }
  212. public void OnFightingEnd()
  213. {
  214. for (int idx = 0; idx < Fighters.Count; idx++)
  215. {
  216. Fighter fighter = Fighters[idx];
  217. if (!fighter.IsDisposed && fighter.IsSpawned)
  218. fighter.OnFightingEnd();
  219. }
  220. }
  221. public void CheckFightersPassiveSkill()
  222. {
  223. for(int idx =0; idx < Fighters.Count;idx++)
  224. {
  225. Fighters[idx].CheckPassiveSkill();
  226. }
  227. }
  228. public void CheckFightersDropBuff()
  229. {
  230. //Debug.Log("------------CheckFightersDropBuff---------");
  231. for (int idx = 0; idx < Fighters.Count; idx++)
  232. {
  233. Fighters[idx].PrecacheBuff();
  234. }
  235. }
  236. public void CheckBuffs()
  237. {
  238. for (int idx = 0; idx < Fighters.Count; idx++)
  239. {
  240. Fighters[idx].ProcessTriggerBuff(Fighters[idx], BuffTriggerType.Trigger_Begin_Fighting);
  241. }
  242. }
  243. public void CheckBuffs(BuffTriggerType buffTriggerType)
  244. {
  245. for (int idx = 0; idx < Fighters.Count; idx++)
  246. {
  247. Fighters[idx].ProcessTriggerBuff(Fighters[idx], buffTriggerType);
  248. }
  249. }
  250. public virtual void DoPauseFight(bool pause)
  251. {
  252. }
  253. public void HideHeroNoPet()
  254. {
  255. if (mTeamFighters != null)
  256. {
  257. for (int idx = 0; idx < mTeamFighters.Count; idx++)
  258. {
  259. if (mTeamFighters[idx].IsPet)
  260. continue;
  261. mTeamFighters[idx].Ctrl.SetLayer(LayerMask.NameToLayer(BattleCamera.HideLayerName));
  262. }
  263. }
  264. }
  265. public void CloseAiHeroNoPet()
  266. {
  267. if (mTeamFighters != null)
  268. {
  269. for (int idx = 0; idx < mTeamFighters.Count; idx++)
  270. {
  271. if (mTeamFighters[idx].IsPet)
  272. continue;
  273. mTeamFighters[idx].AIEnable = false;
  274. }
  275. }
  276. }
  277. public void HideFighters()
  278. {
  279. if(mTeamFighters != null)
  280. {
  281. for(int idx = 0; idx < mTeamFighters.Count;idx++)
  282. {
  283. mTeamFighters[idx].Ctrl.SetLayer(LayerMask.NameToLayer(BattleCamera.HideLayerName));
  284. }
  285. }
  286. if(mEnemyFighters != null)
  287. {
  288. for(int idx =0; idx < mEnemyFighters.Count;idx++)
  289. {
  290. mEnemyFighters[idx].Ctrl.SetLayer(LayerMask.NameToLayer(BattleCamera.HideLayerName));
  291. }
  292. }
  293. }
  294. public void ShowFighters()
  295. {
  296. if (mTeamFighters != null)
  297. {
  298. for (int idx = 0; idx < mTeamFighters.Count; idx++)
  299. {
  300. mTeamFighters[idx].Ctrl.SetLayer(LayerMask.NameToLayer(BattleCamera.FighterLayerName));
  301. }
  302. }
  303. if (mEnemyFighters != null)
  304. {
  305. for (int idx = 0; idx < mEnemyFighters.Count; idx++)
  306. {
  307. mEnemyFighters[idx].Ctrl.SetLayer(LayerMask.NameToLayer(BattleCamera.FighterLayerName));
  308. }
  309. }
  310. }
  311. public virtual void PeaceEnd()
  312. {
  313. }
  314. #region 战斗结束判断
  315. public void CheckFightingResult(BaseBattle battle)
  316. {
  317. if (battle.EndCondList == null || battle.EndCondList.Count == 0)
  318. return;
  319. for (int idx =0; idx < battle.EndCondList.Count;idx++)
  320. {
  321. var cond = battle.EndCondList[idx];
  322. FightingResult ret = CheckEndCondition(cond);
  323. if (ret != FightingResult.None)
  324. {
  325. Result = ret;
  326. return;
  327. }
  328. }
  329. }
  330. private FightingResult CheckEndCondition(BattleEndCondition con)
  331. {
  332. FightingResult ret = FightingResult.None;
  333. if (con.endType == Constants.EndBattle_By_UndeadCount)
  334. {
  335. int teamAliveCnt = 0, enemyAliveCnt = 0;
  336. CheckUndeadCount(ref teamAliveCnt, ref enemyAliveCnt);
  337. if(con.resultType == Constants.ResultType_Normal)
  338. {
  339. if (teamAliveCnt == 0)
  340. ret = FightingResult.Failed;
  341. else if (enemyAliveCnt == 0)
  342. ret = FightingResult.Win;
  343. }
  344. else if(con.resultType == Constants.ResultType_Special)
  345. {
  346. if(teamAliveCnt == 0 || enemyAliveCnt == 0)
  347. ret = con.win ? FightingResult.Win : FightingResult.Failed;
  348. }
  349. }
  350. else if(con.endType == Constants.EndBattle_By_BattleTime)
  351. {
  352. if(CheckBattleTimeout())
  353. {
  354. if(con.resultType == Constants.ResultType_Normal)
  355. {
  356. ret = FightingResult.Failed;
  357. }else if(con.resultType == Constants.ResultType_Special)
  358. {
  359. ret = con.win ? FightingResult.Win : FightingResult.Failed;
  360. }
  361. }
  362. }
  363. else if(con.endType == Constants.EndBattle_By_TeamTotalDamage)
  364. {
  365. if(CheckTeamDamage(con.endVal))
  366. {
  367. if(con.resultType == Constants.ResultType_Normal)
  368. {
  369. ret = FightingResult.Win;
  370. }else if(con.resultType == Constants.ResultType_Special)
  371. {
  372. ret = con.win ? FightingResult.Win : FightingResult.Failed;
  373. }
  374. }
  375. }
  376. else if(con.endType == Constants.EndBattle_By_MainRoleDead)
  377. {
  378. if(CheckMainRoleDead())
  379. {
  380. if(con.resultType == Constants.ResultType_Normal)
  381. {
  382. ret = FightingResult.Failed;
  383. }else if(con.resultType == Constants.ResultType_Special)
  384. {
  385. ret = con.win ? FightingResult.Win : FightingResult.Failed;
  386. }
  387. }
  388. }
  389. else if(con.endType == Constants.EndBattle_By_BossDead)
  390. {
  391. if(CheckBossDead())
  392. {
  393. if(con.resultType == Constants.ResultType_Normal)
  394. {
  395. ret = FightingResult.Win;
  396. }else if(con.resultType == Constants.ResultType_Special)
  397. {
  398. ret = con.win ? FightingResult.Win : FightingResult.Failed;
  399. }
  400. }
  401. }
  402. else if(con.endType == Constants.EndBattle_By_PetDead)
  403. {
  404. if (CheckPetDead())
  405. {
  406. if (con.resultType == Constants.ResultType_Normal)
  407. {
  408. ret = FightingResult.Win;
  409. }
  410. else if (con.resultType == Constants.ResultType_Special)
  411. {
  412. ret = con.win ? FightingResult.Win : FightingResult.Failed;
  413. }
  414. }
  415. }
  416. return ret;
  417. }
  418. private void CheckUndeadCount(ref int teamAliveCount, ref int enemyAliveCount)
  419. {
  420. teamAliveCount = 0;
  421. enemyAliveCount = 0;
  422. for (int i = 0; i < mFighters.Count; i++)
  423. {
  424. Fighter fighter = mFighters[i];
  425. if (!fighter.IsSpawned || (!fighter.IsDisposed && fighter.IsAlive))
  426. {
  427. if (fighter.IsPet)//宠物不计入战斗结算条件
  428. continue;
  429. if (fighter.TeamSide == 0)
  430. teamAliveCount++;
  431. else
  432. enemyAliveCount++;
  433. }
  434. }
  435. }
  436. private bool CheckBattleTimeout()
  437. {
  438. return FightingTime > MaxFightingTime;
  439. }
  440. private bool CheckTeamDamage(int damage)
  441. {
  442. int teamTotalDamage = 0;
  443. for (int i = 0; i < mEnemyFighters.Count; i++)
  444. {
  445. Fighter fighter = mEnemyFighters[i];
  446. teamTotalDamage += fighter.TotalTakeDamage;
  447. }
  448. return teamTotalDamage > damage;
  449. }
  450. private bool CheckMainRoleDead()
  451. {
  452. bool ret = false;
  453. for (int i = 0; i < mTeamFighters.Count; i++)
  454. {
  455. Fighter fighter = mTeamFighters[i];
  456. if(fighter.IsMainRole)
  457. {
  458. ret = fighter.IsSpawned && !fighter.IsAlive;
  459. break;
  460. }
  461. }
  462. return ret;
  463. }
  464. private bool CheckPetDead()
  465. {
  466. bool ret = false;
  467. for (int i = 0; i < mTeamFighters.Count; i++)
  468. {
  469. Fighter fighter = mTeamFighters[i];
  470. if (fighter.IsPet)
  471. {
  472. ret = fighter.IsSpawned && !fighter.IsAlive;
  473. break;
  474. }
  475. }
  476. return ret;
  477. }
  478. private bool CheckBossDead()
  479. {
  480. int deadBossCnt = 0;
  481. int bossCnt = 0;
  482. for (int i = 0; i < mEnemyFighters.Count; i++)
  483. {
  484. Fighter fighter = mEnemyFighters[i];
  485. if (fighter.IsBoss)
  486. {
  487. bossCnt++;
  488. if (fighter.IsSpawned && !fighter.IsAlive)
  489. deadBossCnt++;
  490. }
  491. }
  492. return bossCnt == deadBossCnt;
  493. }
  494. #endregion
  495. }