WolfHuntGameMode_LocalPK.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. /* 野狼关卡-本地pk模式 */
  6. public class WolfHuntGameMode_LocalPK : WolfHuntGameMode, ChallengeGameModeLocalPK
  7. {
  8. public int currentPlayerIndex = 0; // 双人0和1
  9. float singleShootReadyTime = 30f;
  10. float singleShootReadyTimeMax = 30f;
  11. int[] playerHpList = {20, 20};
  12. public bool[] playerDieList = {false, false};
  13. public WolfHuntGameMode_LocalPK(GameMgr gameMgr) : base(gameMgr) {
  14. hunterGamePlayerScoreCounter = new HunterGamePlayerScoreCounter(this);
  15. InitByCurPlayerIndex();
  16. GameEventCenter.ins.onBowArrowShootOut += (a, b) => {
  17. hasShootOut = true;
  18. };
  19. onHpZero += () => {//监听到当前玩家死亡
  20. playerDieList[currentPlayerIndex] = true;
  21. if (hasShootOut) return; //此时箭已射出,避免重复处理下面的逻辑,等NextShoot来处理
  22. //切换到下一个玩家
  23. if (IsOtherPlayerNotDie()) {
  24. ArmBow.ins.readyShoot();
  25. NextPlayerFinal();
  26. } else { //游戏结束
  27. AnnounceGameOver();
  28. }
  29. };
  30. // this.playerHpList[0] = this.playerHpList[1] = this.hpMax = this.hp = 2;
  31. // QuicklyCreateAnimalForDebug();
  32. }
  33. public override void Start()
  34. {
  35. GameMgr.ins.transform.Find("WolfActGrid").gameObject.SetActive(true);
  36. SetLevel(5);
  37. AddHuntGameView();
  38. this.gameMgr.transform.Find("HunterGameView_LocalPK").gameObject.SetActive(true);
  39. AddReadyView();
  40. }
  41. public override void Update() {
  42. base.Update();
  43. if (gameMgr.gameOver || pauseTimeCounting) return;
  44. if (!IsOtherPlayerNotDie()) { //另一个玩家已经死了,倒计时就不要走了
  45. singleShootReadyTime = singleShootReadyTimeMax;
  46. return;
  47. }
  48. singleShootReadyTime -= Time.deltaTime;
  49. if (singleShootReadyTime <= 0) {
  50. //切换玩家
  51. ArmBow.ins.readyShoot();
  52. NextPlayerFinal();
  53. }
  54. }
  55. bool hasShootOut;
  56. public override bool DoNextShoot() {
  57. hasShootOut = false;
  58. if (IsAllPlayerDie()) {
  59. AnnounceGameOver();
  60. return false;
  61. }
  62. bool canDo = base.DoNextShoot();
  63. if (canDo && IsOtherPlayerNotDie()) {
  64. NextPlayerFinal();
  65. }
  66. return canDo;
  67. }
  68. void AddReadyView()
  69. {
  70. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameReadyView_Challenge");
  71. GameObject o = GameObject.Instantiate(view);
  72. PKGameReadyView_Challenge script = o.GetComponent<PKGameReadyView_Challenge>();
  73. script.currentPlayerIndex = currentPlayerIndex;
  74. }
  75. //切换到下一个玩家
  76. void NextPlayerFinal() {
  77. NextPlayer();
  78. BanBowReady();
  79. AddReadyView();
  80. }
  81. void NextPlayer() {
  82. RecordByCurPlayerIndex();
  83. currentPlayerIndex = GetNextPlayerIndex();
  84. InitByCurPlayerIndex();
  85. singleShootReadyTime = singleShootReadyTimeMax;
  86. }
  87. int GetNextPlayerIndex() {
  88. return (currentPlayerIndex + 1) % 2;
  89. }
  90. bool IsAllPlayerDie() {
  91. return playerHpList[GetNextPlayerIndex()] <= 0 && hp <= 0;
  92. }
  93. bool IsOtherPlayerNotDie() {
  94. return playerHpList[GetNextPlayerIndex()] > 0;
  95. }
  96. void InitByCurPlayerIndex() {
  97. hp = playerHpList[currentPlayerIndex];
  98. HunterGameView v = GameObject.FindObjectOfType<HunterGameView>();
  99. if (v) v.RenderHPImmediate();
  100. }
  101. void RecordByCurPlayerIndex() {
  102. playerHpList[currentPlayerIndex] = hp;
  103. }
  104. //localPK interface
  105. public int GetCurrentPlayIndex() {
  106. return currentPlayerIndex;
  107. }
  108. public (float, float) GetSingleShootReadyTime() {
  109. return (singleShootReadyTime, singleShootReadyTimeMax);
  110. }
  111. HunterGamePlayerScoreCounter hunterGamePlayerScoreCounter;
  112. public HunterGamePlayerScoreCounter getHunterGamePlayerScoreCounter() {
  113. return hunterGamePlayerScoreCounter;
  114. }
  115. }