123 lines
4.0 KiB
C#
123 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace DamageNumbersPro.Internal
|
|
{
|
|
public class DNPUpdater : MonoBehaviour
|
|
{
|
|
// Dicitonary
|
|
private static Dictionary<float, DNPUpdater> unscaledUpdaters;
|
|
private static Dictionary<float, DNPUpdater> scaledUpdaters;
|
|
|
|
// Static
|
|
public static Vector3 upVector;
|
|
public static Vector3 rightVector;
|
|
public static bool vectorsNeedUpdate;
|
|
public static Quaternion cameraRotation;
|
|
|
|
// Settings
|
|
public bool isUnscaled;
|
|
public float updateDelay = 0.0125f;
|
|
public HashSet<DamageNumber> activePopups;
|
|
private float delta;
|
|
|
|
// Internal
|
|
private float lastUpdateTime;
|
|
public HashSet<DamageNumber> removedPopups;
|
|
private float time;
|
|
|
|
private void Start()
|
|
{
|
|
StartCoroutine(UpdatePopups());
|
|
}
|
|
|
|
private IEnumerator UpdatePopups()
|
|
{
|
|
// Delay
|
|
var delay = new WaitForSecondsRealtime(updateDelay);
|
|
|
|
while (true)
|
|
{
|
|
// Vector Update
|
|
vectorsNeedUpdate = true;
|
|
|
|
// Update
|
|
foreach (var popup in activePopups)
|
|
if (popup != null)
|
|
popup.UpdateDamageNumber(delta, time);
|
|
else
|
|
removedPopups.Add(popup);
|
|
|
|
// Clean Up
|
|
if (removedPopups.Count > 0)
|
|
{
|
|
foreach (var removed in removedPopups) activePopups.Remove(removed);
|
|
removedPopups = new HashSet<DamageNumber>();
|
|
}
|
|
|
|
// Wait
|
|
if (isUnscaled)
|
|
{
|
|
lastUpdateTime = Time.unscaledTime;
|
|
yield return delay;
|
|
time = Time.unscaledTime;
|
|
delta = time - lastUpdateTime;
|
|
}
|
|
else
|
|
{
|
|
lastUpdateTime = Time.time;
|
|
yield return delay;
|
|
time = Time.time;
|
|
delta = time - lastUpdateTime;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void RegisterPopup(bool unscaledTime, float updateDelay, DamageNumber popup)
|
|
{
|
|
ref var updaters = ref unscaledTime ? ref unscaledUpdaters : ref scaledUpdaters;
|
|
|
|
if (updaters == null) updaters = new Dictionary<float, DNPUpdater>();
|
|
|
|
var containsKey = updaters.ContainsKey(updateDelay);
|
|
if (containsKey && updaters[updateDelay] != null)
|
|
{
|
|
updaters[updateDelay].activePopups.Add(popup);
|
|
}
|
|
else
|
|
{
|
|
if (containsKey) updaters.Remove(updateDelay);
|
|
|
|
var newUpdater = new GameObject("");
|
|
newUpdater.hideFlags = HideFlags.HideInHierarchy;
|
|
|
|
var dnpUpdater = newUpdater.AddComponent<DNPUpdater>();
|
|
dnpUpdater.activePopups = new HashSet<DamageNumber>();
|
|
dnpUpdater.removedPopups = new HashSet<DamageNumber>();
|
|
dnpUpdater.isUnscaled = unscaledTime;
|
|
dnpUpdater.updateDelay = updateDelay;
|
|
DontDestroyOnLoad(newUpdater);
|
|
|
|
updaters.Add(updateDelay, dnpUpdater);
|
|
|
|
dnpUpdater.activePopups.Add(popup);
|
|
}
|
|
}
|
|
|
|
public static void UnregisterPopup(bool unscaledTime, float updateDelay, DamageNumber popup)
|
|
{
|
|
var updaters = unscaledTime ? unscaledUpdaters : scaledUpdaters;
|
|
|
|
if (updaters != null && updaters.ContainsKey(updateDelay) &&
|
|
updaters[updateDelay].activePopups.Contains(popup)) updaters[updateDelay].removedPopups.Add(popup);
|
|
}
|
|
|
|
public static void UpdateVectors(Transform popup)
|
|
{
|
|
vectorsNeedUpdate = false;
|
|
upVector = popup.up;
|
|
rightVector = popup.right;
|
|
}
|
|
}
|
|
} |