using System; using System.Collections.Generic; using UnityEngine; namespace GrassCutter { public enum Ease { Linear, OutQuad, InQuad, InOutQuad, OutCubic, InCubic, InOutCubic, OutBack, InBack, OutElastic, OutBounce, OutExpo } public static class Easing { public static float Apply(Ease e, float t) { t = Mathf.Clamp01(t); switch (e) { case Ease.OutQuad: return 1f - (1f - t) * (1f - t); case Ease.InQuad: return t * t; case Ease.InOutQuad: return t < 0.5f ? 2f * t * t : 1f - Mathf.Pow(-2f * t + 2f, 2f) / 2f; case Ease.OutCubic: return 1f - Mathf.Pow(1f - t, 3f); case Ease.InCubic: return t * t * t; case Ease.InOutCubic: return t < 0.5f ? 4f * t * t * t : 1f - Mathf.Pow(-2f * t + 2f, 3f) / 2f; case Ease.OutBack: { const float c1 = 1.70158f, c3 = c1 + 1f; return 1f + c3 * Mathf.Pow(t - 1f, 3f) + c1 * Mathf.Pow(t - 1f, 2f); } case Ease.InBack: { const float c1 = 1.70158f, c3 = c1 + 1f; return c3 * t * t * t - c1 * t * t; } case Ease.OutElastic: { if (t <= 0f || t >= 1f) return t; const float c4 = (2f * Mathf.PI) / 3f; return Mathf.Pow(2f, -10f * t) * Mathf.Sin((t * 10f - 0.75f) * c4) + 1f; } case Ease.OutBounce: { const float n1 = 7.5625f, d1 = 2.75f; if (t < 1f / d1) return n1 * t * t; if (t < 2f / d1) { t -= 1.5f / d1; return n1 * t * t + 0.75f; } if (t < 2.5f / d1) { t -= 2.25f / d1; return n1 * t * t + 0.9375f; } t -= 2.625f / d1; return n1 * t * t + 0.984375f; } case Ease.OutExpo: return t >= 1f ? 1f : 1f - Mathf.Pow(2f, -10f * t); default: return t; } } } /// Tiny allocation-light tween runner. Tweens are keyed by an owner object so they can be killed. public class Tween : MonoBehaviour { class Item { public object owner; public float delay, duration, time; public Ease ease; public bool unscaled; public Action step; public Action done; public bool dead; } static Tween _inst; readonly List _items = new List(256); readonly List _adding = new List(64); static Tween Inst { get { if (_inst == null) { var go = new GameObject("[Tween]"); DontDestroyOnLoad(go); _inst = go.AddComponent(); } return _inst; } } /// Runs step(eased01) for duration seconds. public static void Run(object owner, float duration, Action step, Ease ease = Ease.OutCubic, float delay = 0f, Action done = null, bool unscaled = true) { Inst._adding.Add(new Item { owner = owner, duration = Mathf.Max(0.0001f, duration), delay = delay, ease = ease, step = step, done = done, unscaled = unscaled }); } public static void Delay(object owner, float delay, Action done, bool unscaled = true) { Run(owner, 0.0001f, null, Ease.Linear, delay, done, unscaled); } /// Whether anything is still tweening for this owner. public static bool Running(object owner) { if (_inst == null || owner == null) return false; foreach (var i in _inst._items) if (!i.dead && ReferenceEquals(i.owner, owner)) return true; foreach (var i in _inst._adding) if (!i.dead && ReferenceEquals(i.owner, owner)) return true; return false; } public static void Kill(object owner) { if (_inst == null || owner == null) return; foreach (var i in _inst._items) if (ReferenceEquals(i.owner, owner)) i.dead = true; foreach (var i in _inst._adding) if (ReferenceEquals(i.owner, owner)) i.dead = true; } public static void Scale(Transform t, Vector3 from, Vector3 to, float dur, Ease ease = Ease.OutBack, float delay = 0f, Action done = null) { Kill(t); PunchRest.Remove(t); t.localScale = from; Run(t, dur, k => { if (t) t.localScale = Vector3.LerpUnclamped(from, to, k); }, ease, delay, done); } /// Quick squash-and-stretch punch on a transform's scale. public static void Punch(Transform t, float amount = 0.25f, float dur = 0.35f) { if (!t) return; Kill(t); // a punch interrupted mid-wobble must not become the new rest scale, or rapid punches make things drift if (!PunchRest.TryGetValue(t, out var baseScale)) PunchRest[t] = baseScale = t.localScale; Run(t, dur, k => { if (!t) return; float s = Mathf.Sin(k * Mathf.PI * 3f) * (1f - k) * amount; t.localScale = new Vector3(baseScale.x * (1f + s), baseScale.y * (1f - s * 0.8f), baseScale.z * (1f + s)); }, Ease.Linear, 0f, () => { PunchRest.Remove(t); if (t) t.localScale = baseScale; }); } static readonly Dictionary PunchRest = new Dictionary(); void Update() { if (_adding.Count > 0) { _items.AddRange(_adding); _adding.Clear(); } float dt = Time.deltaTime, udt = Time.unscaledDeltaTime; for (int n = 0; n < _items.Count; n++) { var i = _items[n]; if (i.dead) continue; float d = i.unscaled ? udt : dt; if (i.delay > 0f) { i.delay -= d; continue; } i.time += d; float k = Mathf.Clamp01(i.time / i.duration); try { i.step?.Invoke(Easing.Apply(i.ease, k)); if (k >= 1f) { i.dead = true; i.done?.Invoke(); } } catch (Exception e) { i.dead = true; Debug.LogException(e); } } _items.RemoveAll(x => x.dead); } } }