AkiraPixelWind/Assets/Scripts/Main/Definition/DataStruct/Input/SnapshotData.cs

88 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using UnityEngine;
namespace Game
{
public class RoleSnapshotClass
{
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 SnapshotData
{
/// <summary>
/// 释放出来的行为历史
/// </summary>
List<RoleSnapshotClass> KeyHistory = new List<RoleSnapshotClass>();
static Queue<RoleSnapshotClass> tempHistoryQueue = new Queue<RoleSnapshotClass>();
const int HistoryLimit = 10;
public SnapshotData()
{
Init();
}
void Init()
{
ClearHistory();
}
RoleSnapshotClass EnqueueOneMotionHistory()
{
if (tempHistoryQueue.Count > 0)
return tempHistoryQueue.Dequeue();
return new RoleSnapshotClass();
}
public void AddSnap(Sprite sprite,Vector3 bridgeDir,Vector3 pos)
{
while (KeyHistory.Count > HistoryLimit)
{
KeyHistory[0].Reset();
tempHistoryQueue.Enqueue(KeyHistory[0]);
KeyHistory.RemoveAt(0);
}
RoleSnapshotClass snap = EnqueueOneMotionHistory();
snap.time = 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();
}
/// <summary>
/// 获得后记得深度拷贝 避免引用类型问题
/// </summary>
/// <returns></returns>
public RoleSnapshotClass[] GetHistoryArray()
{
return KeyHistory.ToArray();
}
}
}