WolfHuntGameMode_LocalPK.cs 4.0 KB

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