AkiraPixelWind/Assets/Scripts/Main/EffectObj/RoleFastShadow.cs
2023-01-09 23:13:17 +08:00

178 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<PlayRoleSnapshotClass> tempHistoryQueue = new Queue<PlayRoleSnapshotClass>();
List<PlayRoleSnapshotClass> KeyHistory = new List<PlayRoleSnapshotClass>();
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<SpriteRenderer>();
mBridgeTransfrom.gameObject.SetActive(false);
LoadData();
}
public void LoadData()
{
ClearHistory();
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;
this.transform.position = KeyHistory[0].Pos;
}
}
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();
}
}