| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- 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<int> messageQueue = new Queue<int>();
- 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<CanvasGroup>();
- if (!canvasGroup) canvasGroup = messagePanel.AddComponent<CanvasGroup>();
- 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();
- }
- }
- }
- }
- }
|