WolfHuntGameMode_LocalPK.cs 3.7 KB

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