DNPUpdater.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DamageNumbersPro;
  5. namespace DamageNumbersPro.Internal
  6. {
  7. public class DNPUpdater : MonoBehaviour
  8. {
  9. // Dicitonary
  10. static Dictionary<float, DNPUpdater> unscaledUpdaters;
  11. static Dictionary<float, DNPUpdater> scaledUpdaters;
  12. // Static
  13. public static Vector3 upVector;
  14. public static Vector3 rightVector;
  15. public static bool vectorsNeedUpdate;
  16. public static Quaternion cameraRotation;
  17. // Settings
  18. public bool isUnscaled = false;
  19. public float updateDelay = 0.0125f;
  20. public HashSet<DamageNumber> activePopups;
  21. public HashSet<DamageNumber> removedPopups;
  22. // Internal
  23. float lastUpdateTime = 0;
  24. float delta = 0;
  25. float time = 0;
  26. void Start()
  27. {
  28. StartCoroutine(UpdatePopups());
  29. }
  30. IEnumerator UpdatePopups()
  31. {
  32. // Delay
  33. WaitForSecondsRealtime delay = new WaitForSecondsRealtime(updateDelay);
  34. while(true)
  35. {
  36. // Vector Update
  37. vectorsNeedUpdate = true;
  38. // Update
  39. foreach (DamageNumber popup in activePopups)
  40. {
  41. if(popup != null)
  42. {
  43. popup.UpdateDamageNumber(delta, time);
  44. }
  45. else
  46. {
  47. removedPopups.Add(popup);
  48. }
  49. }
  50. // Clean Up
  51. if(removedPopups.Count > 0)
  52. {
  53. foreach (DamageNumber removed in removedPopups)
  54. {
  55. activePopups.Remove(removed);
  56. }
  57. removedPopups = new HashSet<DamageNumber>();
  58. }
  59. // Wait
  60. if (isUnscaled)
  61. {
  62. lastUpdateTime = Time.unscaledTime;
  63. yield return delay;
  64. time = Time.unscaledTime;
  65. delta = time - lastUpdateTime;
  66. }
  67. else
  68. {
  69. lastUpdateTime = Time.time;
  70. yield return delay;
  71. time = Time.time;
  72. delta = time - lastUpdateTime;
  73. }
  74. }
  75. }
  76. public static void RegisterPopup(bool unscaledTime, float updateDelay, DamageNumber popup)
  77. {
  78. ref Dictionary<float, DNPUpdater> updaters = ref unscaledTime ? ref unscaledUpdaters : ref scaledUpdaters;
  79. if (updaters == null)
  80. {
  81. updaters = new Dictionary<float, DNPUpdater>();
  82. }
  83. bool containsKey = updaters.ContainsKey(updateDelay);
  84. if (containsKey && updaters[updateDelay] != null)
  85. {
  86. updaters[updateDelay].activePopups.Add(popup);
  87. }
  88. else
  89. {
  90. if(containsKey)
  91. {
  92. updaters.Remove(updateDelay);
  93. }
  94. GameObject newUpdater = new GameObject("");
  95. newUpdater.hideFlags = HideFlags.HideInHierarchy;
  96. DNPUpdater dnpUpdater = newUpdater.AddComponent<DNPUpdater>();
  97. dnpUpdater.activePopups = new HashSet<DamageNumber>();
  98. dnpUpdater.removedPopups = new HashSet<DamageNumber>();
  99. dnpUpdater.isUnscaled = unscaledTime;
  100. dnpUpdater.updateDelay = updateDelay;
  101. DontDestroyOnLoad(newUpdater);
  102. updaters.Add(updateDelay, dnpUpdater);
  103. dnpUpdater.activePopups.Add(popup);
  104. }
  105. }
  106. public static void UnregisterPopup(bool unscaledTime, float updateDelay, DamageNumber popup)
  107. {
  108. Dictionary<float, DNPUpdater> updaters = unscaledTime ? unscaledUpdaters : scaledUpdaters;
  109. if (updaters != null && updaters.ContainsKey(updateDelay) && updaters[updateDelay].activePopups.Contains(popup))
  110. {
  111. updaters[updateDelay].removedPopups.Add(popup);
  112. }
  113. }
  114. public static void UpdateVectors(Transform popup)
  115. {
  116. vectorsNeedUpdate = false;
  117. upVector = popup.up;
  118. rightVector = popup.right;
  119. }
  120. }
  121. }