WolfHuntGameMode_LocalPK.cs 2.8 KB

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