GoldPickupNotifier.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. using TMPro;
  3. using System.Collections.Generic;
  4. public class GoldPickupNotifier : MonoBehaviour
  5. {
  6. public static GoldPickupNotifier Instance;
  7. public GameObject messagePanel;
  8. public TextMeshProUGUI messageText;
  9. public float displayDuration = 2f;
  10. public float fadeSpeed = 1f;
  11. [Header("Queue Settings")]
  12. [Tooltip("Combine gold amounts if picked up within this time window (seconds)")]
  13. public float combineWindow = 0.3f;
  14. [Tooltip("Enable to combine multiple pickups into one message")]
  15. public bool enableCombining = true;
  16. private float timer;
  17. private CanvasGroup canvasGroup;
  18. private Queue<int> messageQueue = new Queue<int>();
  19. private bool isDisplaying = false;
  20. private float lastPickupTime;
  21. private int pendingCombinedAmount;
  22. void Awake()
  23. {
  24. if (Instance == null)
  25. {
  26. Instance = this;
  27. }
  28. else
  29. {
  30. Destroy(gameObject);
  31. }
  32. }
  33. void Start()
  34. {
  35. if (messagePanel)
  36. {
  37. canvasGroup = messagePanel.GetComponent<CanvasGroup>();
  38. if (!canvasGroup) canvasGroup = messagePanel.AddComponent<CanvasGroup>();
  39. canvasGroup.alpha = 0;
  40. messagePanel.SetActive(false);
  41. }
  42. }
  43. public static void Show(int goldAmount)
  44. {
  45. if (Instance) Instance.EnqueueMessage(goldAmount);
  46. }
  47. void EnqueueMessage(int amount)
  48. {
  49. if (!messagePanel || !messageText) return;
  50. // Try to combine with pending amount if within time window
  51. if (enableCombining && Time.time - lastPickupTime < combineWindow)
  52. {
  53. pendingCombinedAmount += amount;
  54. lastPickupTime = Time.time;
  55. }
  56. else
  57. {
  58. // If we had a pending combined amount, queue it first
  59. if (pendingCombinedAmount > 0)
  60. {
  61. messageQueue.Enqueue(pendingCombinedAmount);
  62. pendingCombinedAmount = 0;
  63. }
  64. // Start new pending amount
  65. pendingCombinedAmount = amount;
  66. lastPickupTime = Time.time;
  67. }
  68. // If not currently displaying, show immediately
  69. if (!isDisplaying)
  70. {
  71. ShowNextMessage();
  72. }
  73. }
  74. void ShowNextMessage()
  75. {
  76. // Check if we should flush the pending combined amount
  77. if (pendingCombinedAmount > 0 && Time.time - lastPickupTime >= combineWindow)
  78. {
  79. messageQueue.Enqueue(pendingCombinedAmount);
  80. pendingCombinedAmount = 0;
  81. }
  82. if (messageQueue.Count == 0)
  83. {
  84. isDisplaying = false;
  85. return;
  86. }
  87. int amount = messageQueue.Dequeue();
  88. isDisplaying = true;
  89. messageText.text = $"+{amount} Gold";
  90. messagePanel.SetActive(true);
  91. canvasGroup.alpha = 1;
  92. timer = displayDuration;
  93. }
  94. void Update()
  95. {
  96. // Check if we should flush pending combined amount
  97. if (pendingCombinedAmount > 0 && Time.time - lastPickupTime >= combineWindow && !isDisplaying)
  98. {
  99. messageQueue.Enqueue(pendingCombinedAmount);
  100. pendingCombinedAmount = 0;
  101. ShowNextMessage();
  102. }
  103. if (timer > 0)
  104. {
  105. timer -= Time.deltaTime;
  106. if (timer <= fadeSpeed)
  107. {
  108. canvasGroup.alpha = timer / fadeSpeed;
  109. }
  110. if (timer <= 0)
  111. {
  112. messagePanel.SetActive(false);
  113. isDisplaying = false;
  114. // Show next message in queue if any
  115. if (messageQueue.Count > 0)
  116. {
  117. ShowNextMessage();
  118. }
  119. }
  120. }
  121. }
  122. }