using Game; using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayRoleSnapshotClass { public float time; public Sprite mSprite; public Vector3 BridgeDir; public Vector3 Pos; public void Reset() { time = 0; mSprite = null; BridgeDir = Vector3.zero; Pos = Vector3.zero; } } public class RoleFastShadow : MonoBehaviour { protected Transform mBridgeTransfrom; protected Transform mModelTransfrom; protected SpriteRenderer mSpriteRenderer; Vector3 offset = new Vector3(0, 0, 0.1f); public int CurrPlayIndex = -1; public float NextPlayTime = 0; public float SnapStepStartTime = 0; public Vector3 SnapStepStartPos = Vector3.zero; public Vector3 NextPos = Vector3.zero; public bool bCanPlay = false; public float StartPlayTime; #region 缓存 static Queue tempHistoryQueue = new Queue(); List KeyHistory = new List(); const int HistoryLimit = 30; static PlayRoleSnapshotClass EnqueueOneHistory() { if (tempHistoryQueue.Count > 0) return tempHistoryQueue.Dequeue(); return new PlayRoleSnapshotClass(); } public void AddSnap(float _time,Sprite sprite, Vector3 bridgeDir, Vector3 pos) { while (KeyHistory.Count > HistoryLimit) { KeyHistory[0].Reset(); tempHistoryQueue.Enqueue(KeyHistory[0]); KeyHistory.RemoveAt(0); } PlayRoleSnapshotClass snap = EnqueueOneHistory(); snap.time = _time; snap.mSprite = sprite; snap.BridgeDir = bridgeDir; snap.Pos = pos; KeyHistory.Add(snap); } public void ClearHistory() { for (int i = 0; i < KeyHistory.Count; i++) { KeyHistory[i].Reset(); tempHistoryQueue.Enqueue(KeyHistory[i]); } KeyHistory.Clear(); } #endregion void OnEnable() { mBridgeTransfrom = transform.Find("Bridge"); mModelTransfrom = mBridgeTransfrom.Find("Model"); mSpriteRenderer = mModelTransfrom.GetComponent(); mBridgeTransfrom.gameObject.SetActive(false); LoadData(); } public void LoadData() { RoleSnapshotClass[] srcData = GamePlayEntry.MainPlayer.Player.mRoleSnapshot.GetHistoryArray(); float time = 0; Vector3 pos; Vector3 dir = Vector3.zero; for (int i = 0; i < srcData.Length; i++) { if (i > 0) { time = srcData[i].time - srcData[0].time; time = time / 2f; dir = srcData[i].Pos - srcData[i - 1].Pos; //多一步的位置 pos = srcData[i].Pos + (dir * 4f); } else { pos = srcData[i].Pos; } AddSnap(time, srcData[i].mSprite, srcData[i].BridgeDir, pos); } if (KeyHistory.Count > 0) { bCanPlay = true; StartPlayTime = 0; CurrPlayIndex = -1; NextPlayTime = 0; StartPlayTime = Time.time; NextPos = Vector3.zero; } } public void Update() { if (NextPos != Vector3.zero) { if (NextPlayTime - SnapStepStartTime <= 0)//避免分母为0 return; Vector3 stepPr = ( (NextPos - SnapStepStartPos) * (Time.time - SnapStepStartTime) / (NextPlayTime - SnapStepStartTime) ); transform.position = SnapStepStartPos + stepPr + offset; } //未到达播放下一帧的事件 if (!bCanPlay || Time.time < NextPlayTime) return; CurrPlayIndex++; PlayRoleSnapshotClass snap = KeyHistory[CurrPlayIndex]; //播放 mBridgeTransfrom.gameObject.SetActive(true); mSpriteRenderer.sprite = snap.mSprite; mBridgeTransfrom.localEulerAngles = snap.BridgeDir; transform.position = snap.Pos + offset; SnapStepStartPos = transform.position; SnapStepStartTime = Time.time; //没有后续帧了 if (KeyHistory.Count < CurrPlayIndex + 2) { bCanPlay = false; Invoke("EndEff", 0.05f); } else//后续帧 { NextPlayTime = StartPlayTime + KeyHistory[CurrPlayIndex + 1].time; NextPos = KeyHistory[CurrPlayIndex + 1].Pos; } } //public void SetData(Sprite sprite, Vector3 BridgelocalEulerAngles, Vector3 targetWorldPos) //{ // mBridgeTransfrom.gameObject.SetActive(true); // mSpriteRenderer.sprite = sprite; // mBridgeTransfrom.localEulerAngles = BridgelocalEulerAngles; // transform.position = targetWorldPos + offset; // Invoke("EndEff", 0.3f); //} public void EndEff() { this.gameObject.SetActive(false); ClearHistory(); } }