using UnityEngine;
|
using System;
|
|
/// <summary>
|
/// PackageRegedit的扩展类
|
/// 提供新的包分发接口,支持将包分发到RecordAction的内部RecordPlayer
|
/// 不修改原有的PackageRegedit.Distribute接口,采用新接口绕过
|
/// </summary>
|
public class PackageRegeditEx
|
{
|
/// <summary>
|
/// 将包分发到指定RecordAction的内部RecordPlayer
|
/// 这个方法用于处理RecordAction内部产生的包(如技能内部的Buff添加、子技能等)
|
///
|
/// 使用场景:
|
/// 1. SkillBase中通过PackageRegedit.Distribute产生的包,应该使用SkillRecordAction的innerRecordPlayer
|
/// 2. 确保这些包的生命周期与父RecordAction绑定
|
/// 3. ForceFinish时可以一并处理
|
/// </summary>
|
/// <param name="pack">要分发的包</param>
|
/// <param name="parentRecordAction">父RecordAction,包应该由它的innerRecordPlayer管理</param>
|
public static void DistributeToRecordAction(GameNetPackBasic pack, RecordAction parentRecordAction)
|
{
|
if (pack == null)
|
{
|
Debug.LogError("PackageRegeditEx.DistributeToRecordAction: pack is null");
|
return;
|
}
|
|
if (parentRecordAction == null)
|
{
|
// 如果没有父RecordAction,使用原有的分发逻辑(会使用BattleField的主RecordPlayer)
|
Debug.LogWarning("PackageRegeditEx.DistributeToRecordAction: parentRecordAction is null, 使用默认分发");
|
PackageRegedit.Distribute(pack);
|
return;
|
}
|
|
// 标记这个包是在RecordAction内部处理的
|
// 这样可以在PackageRegedit的处理逻辑中识别并使用正确的RecordPlayer
|
pack.commonMark = true;
|
|
// 临时保存当前正在处理的RecordAction上下文
|
// 这样在DTC处理类中可以获取到父RecordAction,从而使用正确的RecordPlayer
|
CurrentRecordActionContext.Push(parentRecordAction);
|
|
try
|
{
|
// 调用原有的分发逻辑
|
PackageRegedit.Distribute(pack);
|
}
|
finally
|
{
|
// 恢复上下文
|
CurrentRecordActionContext.Pop();
|
}
|
}
|
|
/// <summary>
|
/// 获取当前应该使用的RecordPlayer
|
///
|
/// 判断逻辑:
|
/// 1. 如果有当前RecordAction上下文,使用RecordAction.innerRecordPlayer(RecordAction内部产生的)
|
/// 2. 否则使用BattleField.recordPlayer(顶层包,直接从服务器来的)
|
/// </summary>
|
/// <param name="battleField">战场</param>
|
/// <returns>应该使用的RecordPlayer</returns>
|
public static RecordPlayer GetTargetRecordPlayer(BattleField battleField)
|
{
|
if (battleField == null)
|
{
|
Debug.LogError("PackageRegeditEx.GetTargetRecordPlayer: battleField is null");
|
return null;
|
}
|
|
// 检查是否在RecordAction上下文中
|
var currentRecordAction = CurrentRecordActionContext.Current;
|
if (currentRecordAction != null)
|
{
|
// 使用RecordAction的innerRecordPlayer
|
// 原因:这个包是在RecordAction内部产生的,应该由RecordAction管理
|
var innerPlayer = currentRecordAction.GetInnerRecordPlayer();
|
if (innerPlayer != null)
|
{
|
return innerPlayer;
|
}
|
else
|
{
|
Debug.LogWarning($"PackageRegeditEx.GetTargetRecordPlayer: RecordAction {currentRecordAction.GetType().Name} 的 innerRecordPlayer 为 null,使用 BattleField.recordPlayer");
|
}
|
}
|
|
// 使用BattleField的主RecordPlayer
|
// 原因:这是顶层包,直接从服务器来的,或者没有RecordAction上下文
|
return battleField.recordPlayer;
|
}
|
}
|
|
/// <summary>
|
/// 当前RecordAction上下文
|
/// 使用栈结构管理嵌套的RecordAction上下文
|
/// 这样可以在包分发过程中知道当前处于哪个RecordAction内部
|
/// </summary>
|
public static class CurrentRecordActionContext
|
{
|
private static System.Collections.Generic.Stack<RecordAction> contextStack =
|
new System.Collections.Generic.Stack<RecordAction>();
|
|
public static RecordAction Current
|
{
|
get
|
{
|
if (contextStack.Count > 0)
|
{
|
return contextStack.Peek();
|
}
|
return null;
|
}
|
}
|
|
public static void Push(RecordAction recordAction)
|
{
|
if (recordAction != null)
|
{
|
contextStack.Push(recordAction);
|
}
|
}
|
|
public static void Pop()
|
{
|
if (contextStack.Count > 0)
|
{
|
contextStack.Pop();
|
}
|
}
|
|
public static void Clear()
|
{
|
contextStack.Clear();
|
}
|
|
public static int Depth
|
{
|
get { return contextStack.Count; }
|
}
|
}
|