WolfHuntGameMode_LocalPK.cs 3.6 KB

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