Browse Source

狼关卡玩家hp分离

lvjincheng 4 years ago
parent
commit
13b6f02137

+ 7 - 0
Assets/BowArrow/Scenes/GameChallengeScene/HunterGameView.cs

@@ -92,4 +92,11 @@ public class HunterGameView : MonoBehaviour
         hpBar.fillAmount = Mathf.Lerp(hpBar.fillAmount, (float) gameMode1.hp / gameMode1.hpMax, Time.deltaTime * 6);
         hpText.text = $"{gameMode1.hp}/{gameMode1.hpMax}";
     }
+
+    public void RenderHPImmediate() {
+        try {
+            hpBar.fillAmount = (float) gameMode1.hp / gameMode1.hpMax;
+            hpText.text = $"{gameMode1.hp}/{gameMode1.hpMax}";
+        } catch (System.Exception) {}
+    }
 }

+ 14 - 4
Assets/BowArrow/Scenes/GameChallengeScene/WolfHuntGameMode.cs

@@ -95,7 +95,7 @@ public class WolfHuntGameMode : ChallengeGameMode
 
     bool canStartCreateAniaml = false; //等选完关卡才能开始倒计时
     float createAnimalCountDown = 0; //生产动物的倒计时
-    const float createAnimalCountDownDefault = 30;
+    float createAnimalCountDownDefault = 30;
     public override void Update()
     {
         base.Update();
@@ -118,6 +118,7 @@ public class WolfHuntGameMode : ChallengeGameMode
 
     public int hp = 20;
     public int hpMax = 20;
+    public System.Action onHpZero;
     public void OnAttacked(Wolf wolf, int hurtValue) {
         if (hp <= 0) return;
 
@@ -133,9 +134,13 @@ public class WolfHuntGameMode : ChallengeGameMode
         DoTweenHurt();
         if (hp <= 0) {
             hp = 0;
-            Sequence cb = DOTween.Sequence();
-            cb.AppendInterval(1f);
-            cb.AppendCallback(AnnounceGameOver);
+            if (onHpZero == null) {
+                Sequence cb = DOTween.Sequence();
+                cb.AppendInterval(1f);
+                cb.AppendCallback(AnnounceGameOver);
+            } else {
+                onHpZero.Invoke();
+            }
         }
     }
 
@@ -181,4 +186,9 @@ public class WolfHuntGameMode : ChallengeGameMode
         });
         hurtSeq.SetUpdate(true);
     }
+
+    public void QuicklyCreateAnimalForDebug() {
+        baseCreateDistance = 11;
+        createAnimalCountDownDefault = 5;
+    }
 }

+ 55 - 7
Assets/BowArrow/Scripts/Manager/GameMode/WolfHuntGameMode_LocalPK.cs

@@ -1,12 +1,29 @@
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
+using DG.Tweening;
 
 public class WolfHuntGameMode_LocalPK : WolfHuntGameMode
 {
     int currentPlayerIndex = 0; // 双人0和1
+    int[] playerHpList = {20, 20}; 
+
     public WolfHuntGameMode_LocalPK(GameMgr gameMgr) : base(gameMgr) {
-        
+        InitByCurPlayerIndex();
+        GameEventCenter.ins.onBowArrowShootOut += (a, b) => {
+            hasShootOut = true;
+        };
+        onHpZero += () => {//监听到当前玩家死亡
+            if (hasShootOut) return; //此时箭已射出,避免重复处理下面的逻辑,等NextShoot来处理
+            //切换到下一个玩家
+            if (IsOtherPlayerNotDie()) {
+                ArmBow.ins.readyShoot();
+                NextPlayerFinal();
+            } else { //游戏结束
+                AnnounceGameOver();
+            }
+        };
+        // QuicklyCreateAnimalForDebug();
     }
 
     public override void Start()
@@ -17,12 +34,16 @@ public class WolfHuntGameMode_LocalPK : WolfHuntGameMode
         AddReadyView();
     }
     
+    bool hasShootOut;
     public override bool DoNextShoot() { 
+        hasShootOut = false;
+        if (IsAllPlayerDie()) {
+            AnnounceGameOver();
+            return false;
+        }
         bool canDo = base.DoNextShoot();
-        if (canDo) {
-            NextPlayer();
-            BanBowReady();
-            AddReadyView();
+        if (canDo && IsOtherPlayerNotDie()) {
+            NextPlayerFinal();
         }
         return canDo;
     }
@@ -35,8 +56,35 @@ public class WolfHuntGameMode_LocalPK : WolfHuntGameMode
         script.currentPlayerIndex = currentPlayerIndex;
     }
 
+    //切换到下一个玩家
+    void NextPlayerFinal() {
+        NextPlayer();
+        BanBowReady();
+        AddReadyView();
+    }
+
     void NextPlayer() {
-        currentPlayerIndex++;
-        currentPlayerIndex %= 2;
+        RecordByCurPlayerIndex();
+        currentPlayerIndex = GetNextPlayerIndex();
+        InitByCurPlayerIndex();
+    }
+    int GetNextPlayerIndex() {
+        return (currentPlayerIndex + 1) % 2;
+    }
+    bool IsAllPlayerDie() {
+        return playerHpList[GetNextPlayerIndex()] <= 0 && hp <= 0;
+    }
+    bool IsOtherPlayerNotDie() {
+        return playerHpList[GetNextPlayerIndex()] > 0;
+    }
+
+    void InitByCurPlayerIndex() {
+        hp = playerHpList[currentPlayerIndex];
+        HunterGameView v = GameObject.FindObjectOfType<HunterGameView>();
+        if (v) v.RenderHPImmediate();
+    }
+
+    void RecordByCurPlayerIndex() {
+        playerHpList[currentPlayerIndex] = hp;
     }
 }