WolfHuntGameMode_LocalPK.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. public class WolfHuntGameMode_LocalPK : WolfHuntGameMode
  6. {
  7. int currentPlayerIndex = 0; // 双人0和1
  8. int[] playerHpList = {20, 20};
  9. public WolfHuntGameMode_LocalPK(GameMgr gameMgr) : base(gameMgr) {
  10. InitByCurPlayerIndex();
  11. GameEventCenter.ins.onBowArrowShootOut += (a, b) => {
  12. hasShootOut = true;
  13. };
  14. onHpZero += () => {//监听到当前玩家死亡
  15. if (hasShootOut) return; //此时箭已射出,避免重复处理下面的逻辑,等NextShoot来处理
  16. //切换到下一个玩家
  17. if (IsOtherPlayerNotDie()) {
  18. ArmBow.ins.readyShoot();
  19. NextPlayerFinal();
  20. } else { //游戏结束
  21. AnnounceGameOver();
  22. }
  23. };
  24. // QuicklyCreateAnimalForDebug();
  25. }
  26. public override void Start()
  27. {
  28. GameMgr.ins.transform.Find("WolfActGrid").gameObject.SetActive(true);
  29. SetLevel(5);
  30. AddHuntGameView();
  31. AddReadyView();
  32. }
  33. bool hasShootOut;
  34. public override bool DoNextShoot() {
  35. hasShootOut = false;
  36. if (IsAllPlayerDie()) {
  37. AnnounceGameOver();
  38. return false;
  39. }
  40. bool canDo = base.DoNextShoot();
  41. if (canDo && IsOtherPlayerNotDie()) {
  42. NextPlayerFinal();
  43. }
  44. return canDo;
  45. }
  46. void AddReadyView()
  47. {
  48. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameReadyView_Challenge");
  49. GameObject o = GameObject.Instantiate(view);
  50. PKGameReadyView_Challenge script = o.GetComponent<PKGameReadyView_Challenge>();
  51. script.currentPlayerIndex = currentPlayerIndex;
  52. }
  53. //切换到下一个玩家
  54. void NextPlayerFinal() {
  55. NextPlayer();
  56. BanBowReady();
  57. AddReadyView();
  58. }
  59. void NextPlayer() {
  60. RecordByCurPlayerIndex();
  61. currentPlayerIndex = GetNextPlayerIndex();
  62. InitByCurPlayerIndex();
  63. }
  64. int GetNextPlayerIndex() {
  65. return (currentPlayerIndex + 1) % 2;
  66. }
  67. bool IsAllPlayerDie() {
  68. return playerHpList[GetNextPlayerIndex()] <= 0 && hp <= 0;
  69. }
  70. bool IsOtherPlayerNotDie() {
  71. return playerHpList[GetNextPlayerIndex()] > 0;
  72. }
  73. void InitByCurPlayerIndex() {
  74. hp = playerHpList[currentPlayerIndex];
  75. HunterGameView v = GameObject.FindObjectOfType<HunterGameView>();
  76. if (v) v.RenderHPImmediate();
  77. }
  78. void RecordByCurPlayerIndex() {
  79. playerHpList[currentPlayerIndex] = hp;
  80. }
  81. }