| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using DG.Tweening;
- using UnityEngine;
- using UnityEngine.UI;
- namespace DevilLink2.UI
- {
- /// <summary>Visual objects only; receives already awarded points and never accesses an economy.</summary>
- public sealed class ScoreFeedbackVisuals : IDisposable
- {
- private sealed class Pulse { public Text Label; public Vector3 Scale; public Tween Tween; }
- private sealed class Popup { public Text Label; public Vector2 Start; public float Age, Rise; }
- private readonly Dictionary<Text, Pulse> pulses = new Dictionary<Text, Pulse>();
- private readonly List<Popup> popups = new List<Popup>();
- public const int MaxPopups = 4;
- public const float PopupSeconds = .55f;
- public int PopupCount => popups.Count;
- public int LastPopupPoints { get; private set; }
- public int PulseRequests { get; private set; }
- public void Emphasize(Text label, float amount = .045f)
- {
- if (!label) return;
- ResetPulse(label); PulseRequests++;
- if (!Application.isPlaying || !label.isActiveAndEnabled) return;
- var pulse = new Pulse { Label = label, Scale = label.transform.localScale };
- pulses[label] = pulse;
- float progress = 0;
- pulse.Tween = DOTween.To(() => progress, value => {
- progress = value;
- if (label && pulses.TryGetValue(label, out var current) && ReferenceEquals(current, pulse)) label.transform.localScale = pulse.Scale * (1 + Mathf.Sin(value * Mathf.PI) * amount);
- }, 1, .24f).SetEase(Ease.Linear).SetUpdate(true).SetRecyclable(false).OnComplete(() => {
- if (pulses.TryGetValue(label, out var current) && ReferenceEquals(current, pulse)) ResetPulse(label);
- });
- }
- public void ResetPulse(Text label)
- {
- if (ReferenceEquals(label, null) || !pulses.TryGetValue(label, out var pulse)) return;
- pulses.Remove(label); pulse.Tween?.Kill(false);
- if (pulse.Label) pulse.Label.transform.localScale = pulse.Scale;
- }
- public void ShowPoints(Text template, RectTransform parent, Vector3 position, float cellSize, int actualPoints)
- {
- if (!template || !template.font || !parent || actualPoints <= 0) return;
- while (popups.Count >= MaxPopups) RemovePopup(0);
- var rect = (RectTransform)new GameObject("MatchScorePopup", typeof(RectTransform)).transform;
- rect.SetParent(parent, false); rect.position = position;
- rect.sizeDelta = new Vector2(Mathf.Max(12, cellSize - 4), cellSize * .5f);
- var label = rect.gameObject.AddComponent<Text>();
- label.font = template.font; label.fontSize = Mathf.Clamp(Mathf.FloorToInt(cellSize * .32f), 6, 14);
- label.resizeTextForBestFit = true; label.resizeTextMinSize = 6; label.resizeTextMaxSize = label.fontSize;
- label.fontStyle = FontStyle.Bold; label.alignment = TextAnchor.MiddleCenter;
- label.color = new Color32(239, 214, 157, 255); label.raycastTarget = false;
- label.supportRichText = false; label.text = "+" + actualPoints.ToString(CultureInfo.InvariantCulture);
- popups.Add(new Popup { Label = label, Start = rect.anchoredPosition, Rise = cellSize * .15f });
- LastPopupPoints = actualPoints;
- }
- // Reuses the HUD update loop; deterministic advancement also supports EditMode tests.
- public void Advance(float seconds)
- {
- if (seconds < 0 || float.IsNaN(seconds) || float.IsInfinity(seconds)) return;
- for (int i = popups.Count - 1; i >= 0; i--)
- {
- var popup = popups[i]; popup.Age += seconds;
- if (!popup.Label || popup.Age >= PopupSeconds) { RemovePopup(i); continue; }
- float t = popup.Age / PopupSeconds;
- popup.Label.rectTransform.anchoredPosition = popup.Start + Vector2.up * (popup.Rise * t);
- var color = popup.Label.color; color.a = 1 - t; popup.Label.color = color;
- }
- }
- private void RemovePopup(int index)
- {
- var popup = popups[index]; popups.RemoveAt(index);
- if (!popup.Label) return;
- popup.Label.gameObject.SetActive(false);
- if (Application.isPlaying) UnityEngine.Object.Destroy(popup.Label.gameObject);
- else UnityEngine.Object.DestroyImmediate(popup.Label.gameObject);
- }
- public void Clear()
- {
- foreach (var label in new List<Text>(pulses.Keys)) ResetPulse(label);
- while (popups.Count > 0) RemovePopup(popups.Count - 1);
- }
- public void Dispose() { Clear(); }
- }
- }
|