using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class WolfHuntGameMode_LocalPK : WolfHuntGameMode, ChallengeGameModeLocalPK { 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() { GameMgr.ins.transform.Find("WolfActGrid").gameObject.SetActive(true); SetLevel(5); AddHuntGameView(); this.gameMgr.transform.Find("HunterGameView_LocalPK").gameObject.SetActive(true); AddReadyView(); } bool hasShootOut; public override bool DoNextShoot() { hasShootOut = false; if (IsAllPlayerDie()) { AnnounceGameOver(); return false; } bool canDo = base.DoNextShoot(); if (canDo && IsOtherPlayerNotDie()) { NextPlayerFinal(); } return canDo; } void AddReadyView() { GameObject view = Resources.Load("Prefabs/Views/PKGameReadyView_Challenge"); GameObject o = GameObject.Instantiate(view); PKGameReadyView_Challenge script = o.GetComponent(); script.currentPlayerIndex = currentPlayerIndex; } //切换到下一个玩家 void NextPlayerFinal() { NextPlayer(); BanBowReady(); AddReadyView(); } void NextPlayer() { 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(); if (v) v.RenderHPImmediate(); } void RecordByCurPlayerIndex() { playerHpList[currentPlayerIndex] = hp; } //localPK interface public int GetCurrentPlayIndex() { return currentPlayerIndex; } }