| | |
| | | |
| | | protected bool isRunOnce = false; |
| | | |
| | | // ===== 父子关系和等待机制 ===== |
| | | // 父RecordAction引用(如果是通过其他RecordAction内部触发的) |
| | | public RecordAction parentAction; |
| | | |
| | | // 子RecordAction列表(此RecordAction内部触发的其他RecordAction) |
| | | protected List<RecordAction> childActionList = new List<RecordAction>(); |
| | | |
| | | // 是否是WaitingPlay模式(需要等待父节点动作完成) |
| | | public bool isWaitingPlay = false; |
| | | |
| | | // 自身动作是否完成(不包括子节点的完成状态) |
| | | protected bool isActionCompleted = false; |
| | | |
| | | public RecordAction(RecordActionType _actionType, BattleField _battleField, BattleObject _battleObj) |
| | | { |
| | | actionType = _actionType; |
| | |
| | | battleObject = _battleObj; |
| | | } |
| | | |
| | | public virtual void Played() |
| | | { |
| | | |
| | | } |
| | | |
| | | public RecordActionType actionType; |
| | | |
| | | // 添加子RecordAction |
| | | public virtual void AddChildAction(RecordAction child) |
| | | { |
| | | if (child == null) return; |
| | | |
| | | // 防止重复添加 |
| | | if (childActionList.Contains(child)) return; |
| | | |
| | | // 防止循环依赖 |
| | | if (child == this) |
| | | { |
| | | BattleDebug.LogError("RecordAction.AddChildAction: 不能将自己添加为子节点"); |
| | | return; |
| | | } |
| | | |
| | | childActionList.Add(child); |
| | | BattleDebug.LogError($"RecordAction.AddChildAction: {this.GetType().Name} 添加子节点 {child.GetType().Name}"); |
| | | } |
| | | |
| | | // 设置父RecordAction |
| | | public virtual void SetParentAction(RecordAction parent) |
| | | { |
| | | parentAction = parent; |
| | | if (parent != null) |
| | | { |
| | | BattleDebug.LogError($"RecordAction.SetParentAction: {this.GetType().Name} 的父节点设置为 {parent.GetType().Name}"); |
| | | } |
| | | } |
| | | |
| | | // 设置WaitingPlay标记 |
| | | public virtual void SetWaitingPlay(bool waiting) |
| | | { |
| | | isWaitingPlay = waiting; |
| | | BattleDebug.LogError($"RecordAction.SetWaitingPlay: {this.GetType().Name} WaitingPlay={waiting}"); |
| | | } |
| | | |
| | | // 检查自身动作是否完成(不包括子节点) |
| | | // 子类应该重写此方法来实现自己的动作完成判断 |
| | | public virtual bool IsActionCompleted() |
| | | { |
| | | return isActionCompleted; |
| | | } |
| | | |
| | | // 检查是否可以开始执行(WaitingPlay条件检查) |
| | | public virtual bool CanStartExecution() |
| | | { |
| | | // 如果不是WaitingPlay模式,直接可以执行 |
| | | if (!isWaitingPlay) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | // 如果没有父节点,也可以执行 |
| | | if (parentAction == null) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | // WaitingPlay模式下,需要等待直接父节点的动作完成(不管父节点是否WaitingPlay) |
| | | if (!parentAction.IsActionCompleted()) |
| | | { |
| | | BattleDebug.LogError($"RecordAction.CanStartExecution: {this.GetType().Name} 等待父节点 {parentAction.GetType().Name} 动作完成"); |
| | | return false; |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | public virtual bool IsNeedWaiting() |
| | | { |
| | | return false; |
| | | } |
| | | |
| | | // 检查是否完全完成(包括所有子节点) |
| | | public virtual bool IsFinished() |
| | | { |
| | | return isFinish; |
| | | // 首先自身动作必须完成 |
| | | if (!isActionCompleted && !isFinish) |
| | | { |
| | | return false; |
| | | } |
| | | |
| | | // 检查所有子节点是否完成 |
| | | foreach (var child in childActionList) |
| | | { |
| | | if (!child.IsFinished()) |
| | | { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | public virtual void Run() |
| | |
| | | public virtual void ForceFinish() |
| | | { |
| | | isFinish = true; |
| | | isActionCompleted = true; |
| | | |
| | | // 强制结束所有子节点 |
| | | for (int i = childActionList.Count - 1; i >= 0; i--) |
| | | { |
| | | var child = childActionList[i]; |
| | | child?.ForceFinish(); |
| | | } |
| | | |
| | | // 清理父子引用,防止内存泄漏 |
| | | childActionList.Clear(); |
| | | parentAction = null; |
| | | } |
| | | |
| | | public virtual string GetBattleFieldGuid() |