WolfHuntGameMode_LocalPK.cs 4.0 KB

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