80 lines
2.5 KiB
C#
80 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BF
|
|
{
|
|
public class BFFingerSnapshot
|
|
{
|
|
public float age;
|
|
public Vector2 screenPos;
|
|
|
|
public static List<BFFingerSnapshot> InactiveSnapshots = new List<BFFingerSnapshot>(1000);
|
|
|
|
public static BFFingerSnapshot Pop()
|
|
{
|
|
if (InactiveSnapshots.Count > 0)
|
|
{
|
|
var index = InactiveSnapshots.Count - 1;
|
|
var snapshot = InactiveSnapshots[index];
|
|
InactiveSnapshots.RemoveAt(index);
|
|
return snapshot;
|
|
}
|
|
return new BFFingerSnapshot();
|
|
}
|
|
|
|
public static void Push(BFFingerSnapshot snapshot)
|
|
{
|
|
InactiveSnapshots.Add(snapshot);
|
|
}
|
|
|
|
public static bool TryGetScreenPos(List<BFFingerSnapshot> snapshots, float targetAge, ref Vector2 screenPos)
|
|
{
|
|
if (snapshots != null && snapshots.Count > 0)
|
|
{
|
|
var snapshot = snapshots[0];
|
|
if (targetAge <= snapshot.age)
|
|
{
|
|
screenPos = snapshot.screenPos;
|
|
return true;
|
|
}
|
|
|
|
snapshot = snapshots[snapshots.Count - 1];
|
|
if (targetAge >= snapshot.age)
|
|
{
|
|
screenPos = snapshot.screenPos;
|
|
return true;
|
|
}
|
|
|
|
var lowerIndex = GetLowerIndex(snapshots, targetAge);
|
|
var upperIndex = lowerIndex + 1;
|
|
var lower = snapshots[lowerIndex];
|
|
var upper = upperIndex < snapshots.Count ? snapshots[upperIndex] : lower;
|
|
var across = Mathf.InverseLerp(lower.age, upper.age, targetAge);
|
|
screenPos = Vector2.Lerp(lower.screenPos, upper.screenPos, across);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static int GetLowerIndex(List<BFFingerSnapshot> snapshots, float targetAge)
|
|
{
|
|
if (snapshots != null)
|
|
{
|
|
var count = snapshots.Count;
|
|
if (count > 0)
|
|
{
|
|
for (int i = count - 1; i >= 0; i--)
|
|
{
|
|
if (snapshots[i].age <= targetAge)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
}
|
|
} |