using UnityEngine; using UnityEngine.UI; /// /// 战斗窗口基类 /// public abstract class BaseBattleWin : UIBase { [Header("战斗场景挂载点")] [SerializeField] protected Transform mountPoint; [Header("战斗控制按钮")] [SerializeField] protected Button btnSpeed; // 速度控制 [SerializeField] protected Text textSpeed; // 速度显示 [SerializeField] protected Button btnPass; // 跳过战斗 [Header("战斗UI组件")] [SerializeField] protected TotalDamageDisplayer totalDamageDisplayer; // 伤害统计 [SerializeField] protected SkillTips skillTipsRed; // 红方技能提示 [SerializeField] protected SkillTips skillTipsBlue; // 蓝方技能提示 [SerializeField] protected Text txtBattleRound; // 回合信息 (Arena的txtWaveInfo, Story/Bone的txtBattleRound) protected BattleRootNode battleRootNode = null; protected BattleField battleField; // 当前战斗的战场实例 protected override void InitComponent() { base.InitComponent(); // 绑定通用按钮事件 if (btnSpeed != null) btnSpeed.AddListener(ChangeSpeed); if (btnPass != null) btnPass.AddListener(OnClickPass); } protected override void OnPreOpen() { base.OnPreOpen(); BattleManager.Instance.onBattleFieldCreate += OnCreateBattleField; RegisterBattleEvents(); // 在打开时需要暂停主线战斗 if (BattleManager.Instance.storyBattleField != null) { BattleManager.Instance.storyBattleField.IsPause = true; if (UIManager.Instance.IsOpened()) { UIManager.Instance.CloseWindow(); } } PlayBackGroundMusic(); } protected override void OnPreClose() { base.OnPreClose(); UIManager.Instance.CloseWindow(); BattleManager.Instance.onBattleFieldCreate -= OnCreateBattleField; UnregisterBattleEvents(); // 在关闭时恢复主线战斗 if (BattleManager.Instance.storyBattleField != null) BattleManager.Instance.storyBattleField.IsPause = false; if (battleField != null) { battleField.OnRoundChange -= OnRoundChange; } SoundPlayer.Instance.StopBackGroundMusic(); } protected override void OnClose() { base.OnClose(); if (battleRootNode != null) { battleRootNode.transform.SetParent(Launch.Instance.transform); battleRootNode.transform.localPosition = new Vector3(-10000, -10000, 0); } battleField = null; } /// /// 通用刷新逻辑 (模板方法) /// public override void Refresh() { base.Refresh(); // 1. 执行基类通用刷新逻辑 (清空旧状态) if (totalDamageDisplayer != null) totalDamageDisplayer.SetActive(false); if (skillTipsBlue != null) skillTipsBlue.SetActive(false); if (skillTipsRed != null) skillTipsRed.SetActive(false); // 刷新速度显示 if (textSpeed != null && BattleManager.Instance != null) textSpeed.text = (BattleManager.Instance.speedIndex + 1).ToString(); // 2. 调用子类特定的刷新逻辑 if (battleField != null) // 确保战场存在后再调用特定刷新 { RefreshSpecific(); } } /// /// 子类实现的特定刷新逻辑 /// protected abstract void RefreshSpecific(); /// /// 当战斗字段创建时 (子类必须重写, 以匹配特定的战场类型) /// /// /// protected abstract void OnCreateBattleField(string guid, BattleField field); // 示例: // protected override void OnCreateBattleField(string guid, BattleField field) // { // // 检查是否是当前窗口关心的战场类型 // if (field is ArenaBattleField arenaField) // { // SetBattleField(arenaField); // 调用SetBattleField // } // } /// /// 设置战斗字段 /// public virtual void SetBattleField(BattleField _battleField) { // 1. 清理旧的战场 if (battleField != null) { battleField.OnRoundChange -= OnRoundChange; battleField = null; } // 2. 设置新的战场 battleField = _battleField; if (battleField == null) return; // 3. 关联战斗场景根节点 if (battleRootNode != null) // 归还旧的 { battleRootNode.transform.localPosition = Vector3.zero; battleRootNode.transform.SetParent(Launch.Instance.transform); } battleRootNode = battleField.battleRootNode; if (battleRootNode == null) return; battleRootNode.transform.SetParent(mountPoint); // 挂载新的 battleRootNode.transform.localPosition = Vector3.zero; battleRootNode.transform.localScale = Vector3.one; // 4. 打开或获取HUD BattleHUDWin ui = UIManager.Instance.GetUI(); if (null == ui) { ui = UIManager.Instance.OpenWindow(); } ui.SetBattleField(battleField); // 5. 更新Canvas battleField.UpdateCanvas(canvas); // 6. 刷新UI Refresh(); // 7. 监听回合变更 battleField.OnRoundChange -= OnRoundChange; battleField.OnRoundChange += OnRoundChange; OnRoundChange(battleField.round, battleField.turnMax); // 立即执行一次以显示初始回合 } /// /// 跳过战斗 /// protected virtual void OnClickPass() { if (null == battleField) return; if (!FuncOpen.Instance.IsFuncOpen(BattleManager.Instance.passFuncId, true)) return; int passRound = BattleManager.Instance.defaultPassRound; var name = battleField.ToString(); // 检查是否为永久特权卡玩家 bool hasForeverPrivilege = InvestModel.Instance.IsInvested(InvestModel.foreverCardType); if (hasForeverPrivilege) { // 永久特权卡玩家逻辑 if (BattleConst.FieldNameToIndex.ContainsKey(name)) { int index = BattleConst.FieldNameToIndex[name]; if (BattleManager.Instance.foreverPrivilegePassDict.ContainsKey(index)) { passRound = BattleManager.Instance.foreverPrivilegePassDict[index]; } else { // 未配置的战场类型,使用普通玩家规则 if (BattleManager.Instance.passDict.ContainsKey(index)) { passRound = BattleManager.Instance.passDict[index]; } // 否则使用默认配置 } } // 如果战场类型不在 FieldNameToIndex 中,使用默认配置 } else { // 普通玩家逻辑 if (BattleConst.FieldNameToIndex.ContainsKey(name)) { int index = BattleConst.FieldNameToIndex[name]; if (BattleManager.Instance.passDict.ContainsKey(index)) { passRound = BattleManager.Instance.passDict[index]; } // 否则使用默认配置 } // 如果战场类型不在 FieldNameToIndex 中,使用默认配置 } int nowRound = battleField.round; int realPassRound = passRound + 1; // 配置是超过x回合可以跳,意味着x+1回合可以跳 if (nowRound < realPassRound) { SysNotifyMgr.Instance.ShowTip("BattlePass", realPassRound - nowRound); return; } battleField.ForceFinish(); } /// /// 改变速度 /// protected virtual void ChangeSpeed() { if (null == battleField) return; // 计算下一个速度档位的索引 int nextSpeedIndex = (BattleManager.Instance.speedIndex + 1) % BattleManager.Instance.speedGear.Length; // 检查下一档倍速功能是否开启 int nextSpeedFuncId = BattleManager.Instance.speedIndexfuncIdArr[nextSpeedIndex]; bool isOpen = FuncOpen.Instance.IsFuncOpen(nextSpeedFuncId); if (!isOpen && FuncOpenLVConfig.HasKey(nextSpeedFuncId)) { var config = FuncOpenLVConfig.Get(nextSpeedFuncId); SysNotifyMgr.Instance.ShowTip("BattleSpeedTip", TaskManager.Instance.GetNeedFinishTaskCount(config.LimitMissionID), nextSpeedIndex + 1); } BattleManager.Instance.speedIndex = !isOpen ? 0 : nextSpeedIndex; battleField.SetSpeedRatio(BattleManager.Instance.speedGear[BattleManager.Instance.speedIndex]); if (textSpeed != null) textSpeed.text = (BattleManager.Instance.speedIndex + 1).ToString(); } /// /// 注册战斗事件 /// protected virtual void RegisterBattleEvents() { EventBroadcast.Instance.AddListener(EventName.BATTLE_DAMAGE_TAKEN, OnDamageTaken); EventBroadcast.Instance.AddListener(EventName.BATTLE_CAST_SKILL, OnCastSkill); } /// /// 注销战斗事件 /// (BattleWin 应该重写此方法并置空) /// protected virtual void UnregisterBattleEvents() { EventBroadcast.Instance.RemoveListener(EventName.BATTLE_DAMAGE_TAKEN, OnDamageTaken); EventBroadcast.Instance.RemoveListener(EventName.BATTLE_CAST_SKILL, OnCastSkill); } /// /// 造成伤害 /// protected virtual void OnDamageTaken(BattleDmgInfo damageInfo) { if (battleField == null || damageInfo.battleFieldGuid != battleField.guid) return; // 通用逻辑:显示伤害数字 if (totalDamageDisplayer != null) totalDamageDisplayer.SetDamage(damageInfo); } /// /// 释放技能 (通用:显示技能Tips) /// protected virtual void OnCastSkill(string guid, SkillConfig skillConfig, TeamHero teamHero) { if (battleField == null || battleField.guid != guid) return; BattleObject battleObject = battleField.battleObjMgr.GetBattleObject(teamHero.ObjID); if (battleObject == null) return; bool isRed = battleObject.Camp == BattleCamp.Red; SkillTips tips = isRed ? skillTipsRed : skillTipsBlue; if (tips != null) tips.PlayMotion(battleField, isRed, teamHero, skillConfig); } /// /// 回合变更 (通用:刷新回合显示) /// protected virtual void OnRoundChange(int round, int maxRound) { if (txtBattleRound != null) { txtBattleRound.text = Language.Get("RoundText", round, maxRound); } } protected virtual void PlayBackGroundMusic() { SoundPlayer.Instance.PlayBackGroundMusic(39); } }