using System.Collections.Generic; using System; using UnityEngine; public class RecordPlayer { protected BattleField battleField; private Queue recordActionQueue = new Queue(); protected RecordAction currentRecordAction; protected List immediatelyActionList = new List(); private bool isWaitingNextAction = false; private float waitTimer = 0f; private const float waitInterval = 0.2f; private float speedRatio = 1.5f; public void Init(BattleField _battleField) { Release(); battleField = _battleField; } public bool IsPlaying() { return currentRecordAction != null || recordActionQueue.Count > 0 || immediatelyActionList.Count > 0; } public void PlayRecord(RecordAction recordAction) { // BattleDebug.LogError("Enqueue record action " + recordAction.GetType()); recordActionQueue.Enqueue(recordAction); } public void PlayRecord(List recordActions) { for (int i = 0; i < recordActions.Count; i++) { PlayRecord(recordActions[i]); } } public void InsertRecord(RecordAction recordAction) { // BattleDebug.LogError("Insert record action " + recordAction.GetType()); if (currentRecordAction != null) { Queue tempQueue = new Queue(); tempQueue.Enqueue(recordAction); while (recordActionQueue.Count > 0) { tempQueue.Enqueue(recordActionQueue.Dequeue()); } recordActionQueue = tempQueue; } else { recordActionQueue.Enqueue(recordAction); } } public void ImmediatelyPlay(RecordAction recordAction) { immediatelyActionList.Add(recordAction); } protected void ImmediatelyPlayRun() { if (immediatelyActionList.Count > 0) { List removeIndexList = new List(); 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--) { immediatelyActionList.RemoveAt(removeIndexList[i]); } } } 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() { for (int i = 0; i < immediatelyActionList.Count; i++) { immediatelyActionList[i].ForceFinish(); } immediatelyActionList.Clear(); if (currentRecordAction != null) { currentRecordAction.ForceFinish(); } currentRecordAction = null; while (recordActionQueue.Count > 0) { recordActionQueue.Dequeue().ForceFinish(); } } public void Release() { if (null != currentRecordAction) { currentRecordAction.ForceFinish(); } currentRecordAction = null; recordActionQueue.Clear(); immediatelyActionList.Clear(); } public void SetSpeedRatio(float ratio) { speedRatio = ratio; } }