202 lines
5.0 KiB
C#
202 lines
5.0 KiB
C#
using System.Collections;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class ScrollingText : MonoBehaviour, IPoolable
|
|
{
|
|
public FloatingTextStyle normalHit;
|
|
public FloatingTextStyle critHit;
|
|
public FloatingTextStyle heal;
|
|
public FloatingTextStyle dodge;
|
|
public FloatingTextStyle block;
|
|
|
|
[SerializeField] private TMP_Text tmpText;
|
|
[SerializeField] private float moveSpeed = 1f;
|
|
|
|
private Coroutine currentRoutine;
|
|
|
|
float time;
|
|
Color startColor;
|
|
Vector3 startPos;
|
|
float t;
|
|
Color c;
|
|
float yOffset;
|
|
|
|
private void Reset()
|
|
{
|
|
// Auto-assign TMP_Text if missing
|
|
if (tmpText == null) tmpText = GetComponent<TMP_Text>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Displays text with color, scrolls upward, and fades out.
|
|
/// </summary>
|
|
public void Show(string text, Color color, float duration)
|
|
{
|
|
if (tmpText == null) return;
|
|
|
|
// Stop any existing animation
|
|
if (currentRoutine != null)
|
|
StopCoroutine(currentRoutine);
|
|
|
|
tmpText.text = text;
|
|
tmpText.color = color;
|
|
|
|
currentRoutine = StartCoroutine(AnimateText(duration));
|
|
}
|
|
|
|
public void ShowHit(string text)
|
|
{
|
|
if (tmpText == null) return;
|
|
|
|
if (currentRoutine != null)
|
|
StopCoroutine(currentRoutine);
|
|
|
|
tmpText.text = text;
|
|
tmpText.color = normalHit.color;
|
|
tmpText.fontSize *= normalHit.sizeMultiplier;
|
|
|
|
currentRoutine = StartCoroutine(AnimateText(normalHit));
|
|
}
|
|
public void ShowCrit(string text)
|
|
{
|
|
if (tmpText == null) return;
|
|
|
|
if (currentRoutine != null)
|
|
StopCoroutine(currentRoutine);
|
|
|
|
tmpText.text = text;
|
|
tmpText.color = critHit.color;
|
|
tmpText.fontSize *= critHit.sizeMultiplier;
|
|
|
|
currentRoutine = StartCoroutine(AnimateText(critHit));
|
|
}
|
|
public void ShowHeal(string text)
|
|
{
|
|
if (tmpText == null) return;
|
|
|
|
if (currentRoutine != null)
|
|
StopCoroutine(currentRoutine);
|
|
|
|
tmpText.text = text;
|
|
tmpText.color = heal.color;
|
|
tmpText.fontSize *= heal.sizeMultiplier;
|
|
|
|
currentRoutine = StartCoroutine(AnimateText(heal));
|
|
}
|
|
public void ShowDodge(string text)
|
|
{
|
|
if (tmpText == null) return;
|
|
|
|
if (currentRoutine != null)
|
|
StopCoroutine(currentRoutine);
|
|
|
|
tmpText.text = text;
|
|
tmpText.color = dodge.color;
|
|
tmpText.fontSize *= dodge.sizeMultiplier;
|
|
|
|
currentRoutine = StartCoroutine(AnimateText(dodge));
|
|
}
|
|
public void ShowBlock(string text)
|
|
{
|
|
if (tmpText == null) return;
|
|
|
|
if (currentRoutine != null)
|
|
StopCoroutine(currentRoutine);
|
|
|
|
tmpText.text = text;
|
|
tmpText.color = block.color;
|
|
tmpText.fontSize *= block.sizeMultiplier;
|
|
|
|
currentRoutine = StartCoroutine(AnimateText(block));
|
|
}
|
|
|
|
public void Show(string text, FloatingTextStyle style)
|
|
{
|
|
if (tmpText == null) return;
|
|
|
|
if (currentRoutine != null)
|
|
StopCoroutine(currentRoutine);
|
|
|
|
tmpText.text = text;
|
|
tmpText.color = style.color;
|
|
tmpText.fontSize *= style.sizeMultiplier;
|
|
|
|
currentRoutine = StartCoroutine(AnimateText(style));
|
|
}
|
|
|
|
private IEnumerator AnimateText(float duration)
|
|
{
|
|
time = 0f;
|
|
startColor = tmpText.color;
|
|
startPos = transform.localPosition;
|
|
|
|
while (time < duration)
|
|
{
|
|
time += Time.deltaTime;
|
|
t = time / duration;
|
|
|
|
// Move upward
|
|
transform.localPosition = startPos + Vector3.up * (moveSpeed * t);
|
|
|
|
// Fade out
|
|
c = startColor;
|
|
c.a = Mathf.Lerp(1f, 0f, t);
|
|
tmpText.color = c;
|
|
|
|
yield return null;
|
|
}
|
|
|
|
tmpText.text = ""; // Clear after animation
|
|
GameObjectPoolManager.Instance.Release(this.gameObject);
|
|
}
|
|
|
|
private IEnumerator AnimateText(FloatingTextStyle style)
|
|
{
|
|
time = 0f;
|
|
startPos = transform.localPosition;
|
|
startColor = style.color;
|
|
|
|
while (time < style.duration)
|
|
{
|
|
time += Time.deltaTime;
|
|
t = time / style.duration;
|
|
|
|
// Move upwards with curve
|
|
yOffset = style.moveYCurve.Evaluate(t) * style.moveSpeed;
|
|
transform.localPosition = startPos + Vector3.up * yOffset;
|
|
|
|
// Fade with curve
|
|
c = startColor;
|
|
c.a = style.alphaCurve.Evaluate(t);
|
|
tmpText.color = c;
|
|
|
|
yield return null;
|
|
}
|
|
|
|
tmpText.text = "";
|
|
GameObjectPoolManager.Instance.Release(this.gameObject);
|
|
}
|
|
|
|
public void OnGetFromPool()
|
|
{
|
|
|
|
}
|
|
|
|
public void OnReleaseToPool()
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
|
|
[System.Serializable]
|
|
public class FloatingTextStyle
|
|
{
|
|
public Color color = Color.white;
|
|
public float duration = 1.5f;
|
|
public float moveSpeed = 1f;
|
|
public AnimationCurve moveYCurve = AnimationCurve.Linear(0, 0, 1, 1);
|
|
public AnimationCurve alphaCurve = AnimationCurve.Linear(0, 1, 1, 0);
|
|
public float sizeMultiplier = 1f;
|
|
} |