using UnityEngine; using TMPro; using System.Collections.Generic; public class GoldPickupNotifier : MonoBehaviour { public static GoldPickupNotifier Instance; public GameObject messagePanel; public TextMeshProUGUI messageText; public float displayDuration = 2f; public float fadeSpeed = 1f; [Header("Queue Settings")] [Tooltip("Combine gold amounts if picked up within this time window (seconds)")] public float combineWindow = 0.3f; [Tooltip("Enable to combine multiple pickups into one message")] public bool enableCombining = true; private float timer; private CanvasGroup canvasGroup; private Queue messageQueue = new Queue(); private bool isDisplaying = false; private float lastPickupTime; private int pendingCombinedAmount; void Awake() { if (Instance == null) { Instance = this; } else { Destroy(gameObject); } } void Start() { if (messagePanel) { canvasGroup = messagePanel.GetComponent(); if (!canvasGroup) canvasGroup = messagePanel.AddComponent(); canvasGroup.alpha = 0; messagePanel.SetActive(false); } } public static void Show(int goldAmount) { if (Instance) Instance.EnqueueMessage(goldAmount); } void EnqueueMessage(int amount) { if (!messagePanel || !messageText) return; // Try to combine with pending amount if within time window if (enableCombining && Time.time - lastPickupTime < combineWindow) { pendingCombinedAmount += amount; lastPickupTime = Time.time; } else { // If we had a pending combined amount, queue it first if (pendingCombinedAmount > 0) { messageQueue.Enqueue(pendingCombinedAmount); pendingCombinedAmount = 0; } // Start new pending amount pendingCombinedAmount = amount; lastPickupTime = Time.time; } // If not currently displaying, show immediately if (!isDisplaying) { ShowNextMessage(); } } void ShowNextMessage() { // Check if we should flush the pending combined amount if (pendingCombinedAmount > 0 && Time.time - lastPickupTime >= combineWindow) { messageQueue.Enqueue(pendingCombinedAmount); pendingCombinedAmount = 0; } if (messageQueue.Count == 0) { isDisplaying = false; return; } int amount = messageQueue.Dequeue(); isDisplaying = true; messageText.text = $"+{amount} Gold"; messagePanel.SetActive(true); canvasGroup.alpha = 1; timer = displayDuration; } void Update() { // Check if we should flush pending combined amount if (pendingCombinedAmount > 0 && Time.time - lastPickupTime >= combineWindow && !isDisplaying) { messageQueue.Enqueue(pendingCombinedAmount); pendingCombinedAmount = 0; ShowNextMessage(); } if (timer > 0) { timer -= Time.deltaTime; if (timer <= fadeSpeed) { canvasGroup.alpha = timer / fadeSpeed; } if (timer <= 0) { messagePanel.SetActive(false); isDisplaying = false; // Show next message in queue if any if (messageQueue.Count > 0) { ShowNextMessage(); } } } } }