| using System.Collections.Generic; | 
| using System; | 
| using UnityEngine; | 
|   | 
| public class RecordPlayer | 
| { | 
|     protected BattleField battleField; | 
|   | 
|     private Queue<RecordAction> recordActionQueue = new Queue<RecordAction>(); | 
|     protected RecordAction currentRecordAction; | 
|   | 
|     protected List<RecordAction> immediatelyActionList = new List<RecordAction>(); | 
|   | 
|     private bool isWaitingNextAction = false; | 
|     private float waitTimer = 0f; | 
|     private const float waitInterval = 0.2f; | 
|   | 
|     private float speedRatio = 1.5f; | 
|   | 
|     private bool isForceFinish = false; | 
|   | 
|     public void Init(BattleField _battleField) | 
|     { | 
|         Release(); | 
|         battleField = _battleField; | 
|     } | 
|   | 
|     public bool IsPlaying() | 
|     { | 
|         bool isPlaying = currentRecordAction != null || recordActionQueue.Count > 0 || immediatelyActionList.Count > 0; | 
|         //BattleDebug.LogError("IsPlaying " + isPlaying + " currentRecordAction " + (currentRecordAction != null) + " recordActionQueue.Count " + recordActionQueue.Count + " immediatelyActionList.Count " + immediatelyActionList.Count); | 
|         return isPlaying; | 
|     } | 
|   | 
|     public void PlayRecord(RecordAction recordAction) | 
|     { | 
|         BattleDebug.LogError("Enqueue record action " + recordAction.GetType()); | 
|         if (isForceFinish) | 
|         { | 
|             recordAction.ForceFinish(); | 
|             return; | 
|         } | 
|         recordActionQueue.Enqueue(recordAction); | 
|     } | 
|   | 
|     public void PlayRecord(List<RecordAction> recordActions) | 
|     { | 
|         for (int i = 0; i < recordActions.Count; i++) | 
|         { | 
|             PlayRecord(recordActions[i]); | 
|         } | 
|     } | 
|   | 
|     public void InsertRecord(RecordAction recordAction) | 
|     { | 
|         if (isForceFinish) | 
|         { | 
|             recordAction.ForceFinish(); | 
|             return; | 
|         } | 
|   | 
|         BattleDebug.LogError("Insert record action " + recordAction.GetType()); | 
|         if (currentRecordAction != null) | 
|         { | 
|             Queue<RecordAction> tempQueue = new Queue<RecordAction>(); | 
|             tempQueue.Enqueue(recordAction); | 
|             while (recordActionQueue.Count > 0) | 
|             { | 
|                 tempQueue.Enqueue(recordActionQueue.Dequeue()); | 
|             } | 
|             recordActionQueue = tempQueue; | 
|         } | 
|         else | 
|         { | 
|             recordActionQueue.Enqueue(recordAction); | 
|         } | 
|     } | 
|   | 
|     public void ImmediatelyPlay(RecordAction recordAction) | 
|     { | 
|         if (isForceFinish) | 
|         { | 
|             recordAction.ForceFinish(); | 
|             return; | 
|         } | 
|         immediatelyActionList.Add(recordAction); | 
|     } | 
|   | 
|     protected void ImmediatelyPlayRun() | 
|     { | 
|         if (immediatelyActionList.Count > 0) | 
|         { | 
|             List<int> removeIndexList = new List<int>(); | 
|   | 
|             for (int i = immediatelyActionList.Count - 1; i >= 0; i--) | 
|             { | 
|                 var action = immediatelyActionList[i]; | 
|                 if (!action.IsFinished()) | 
|                 { | 
|                     action.Run(); | 
|                     continue; | 
|                 } | 
|   | 
|                 removeIndexList.Add(i); | 
|             } | 
|   | 
|             for (int i = removeIndexList.Count - 1; i >= 0; i--) | 
|             { | 
|                 int index = removeIndexList[i]; | 
|                 if (index < 0 || index >= immediatelyActionList.Count) | 
|                     continue; | 
|                 immediatelyActionList.RemoveAt(index); | 
|             } | 
|         } | 
|     } | 
|   | 
|     public virtual void Run() | 
|     { | 
|         ImmediatelyPlayRun(); | 
|   | 
|         // 等待下一个action | 
|         if (isWaitingNextAction) | 
|         { | 
|             waitTimer += Time.deltaTime * speedRatio; | 
|             if (waitTimer >= waitInterval) | 
|             { | 
|                 isWaitingNextAction = false; | 
|                 waitTimer = 0f; | 
|             } | 
|             else | 
|             { | 
|                 return; | 
|             } | 
|         } | 
|   | 
|         if (currentRecordAction == null) | 
|         { | 
|             if (recordActionQueue.Count <= 0) | 
|             { | 
|                 return; | 
|             } | 
|         } | 
|   | 
|         if (currentRecordAction != null && !currentRecordAction.IsFinished()) | 
|         { | 
|             currentRecordAction.Run(); | 
|             return; | 
|         } | 
|   | 
|         if (currentRecordAction != null && currentRecordAction.IsFinished()) | 
|         { | 
|             BattleDebug.LogError("record action " + currentRecordAction.GetType() + " play finished"); | 
|             currentRecordAction = null; | 
|             isWaitingNextAction = true; | 
|             waitTimer = 0f; | 
|             return; | 
|         } | 
|   | 
|         if (currentRecordAction == null) | 
|         { | 
|             if (recordActionQueue.Count > 0) | 
|             { | 
|                 currentRecordAction = recordActionQueue.Dequeue(); | 
|                 BattleDebug.LogError("play record action " + currentRecordAction.GetType()); | 
|             } | 
|         } | 
|     } | 
|   | 
|     //  先预留 感觉用的上 | 
|     public virtual void ResumeGame() | 
|     { | 
|   | 
|     } | 
|   | 
|     public virtual void PauseGame() | 
|     { | 
|   | 
|     } | 
|   | 
|   | 
|     public void HaveRest() | 
|     { | 
|         ForceFinish(); | 
|     } | 
|   | 
|     public void ForceFinish() | 
|     { | 
|         isForceFinish = true; | 
|         for (int i = immediatelyActionList.Count - 1; i >= 0; i--) | 
|         { | 
|             var action = immediatelyActionList[i]; | 
|             action.ForceFinish(); | 
|             immediatelyActionList.Remove(action); | 
|         } | 
|   | 
|         while (currentRecordAction != null) | 
|         { | 
|             var temp = currentRecordAction; | 
|             currentRecordAction = null; | 
|             temp.ForceFinish(); | 
|         } | 
|   | 
|         while (recordActionQueue.Count > 0) | 
|         { | 
|             recordActionQueue.Dequeue().ForceFinish(); | 
|         } | 
|     } | 
|   | 
|     public void Release() | 
|     { | 
|         if (null != currentRecordAction) | 
|         { | 
|             currentRecordAction.ForceFinish(); | 
|         } | 
|         currentRecordAction = null; | 
|         recordActionQueue.Clear(); | 
|         immediatelyActionList.Clear(); | 
|         isForceFinish = false; | 
|     } | 
|   | 
|     public void SetSpeedRatio(float ratio) | 
|     { | 
|         speedRatio = ratio; | 
|     } | 
| } |