LobbyModeMagic.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace DevilLink2.UI
  5. {
  6. // Illustrated scene layers. Only local art moves; the rim/title/progress and Button stay fixed.
  7. public sealed class LobbyModeMagic : MonoBehaviour
  8. {
  9. private LobbyScreen lobby;
  10. private int mode;
  11. private float clock;
  12. private Image subject, aura, footGlow, upperSand, lowerSand, veilLeft, veilRight;
  13. private Image backRing, floorRing, waterRing;
  14. private readonly Image[] sparks = new Image[6];
  15. private Sprite[] lotusFrames;
  16. private int lotusFrame = -1;
  17. private static readonly Color Gold = new Color(1f, .78f, .40f);
  18. private static readonly Color Rose = new Color(.95f, .30f, .72f);
  19. private static readonly Color Blue = new Color(.46f, .62f, 1f);
  20. private const string Folder = "LobbyModeAnimation/";
  21. private const string Scene = Folder + "SceneR1/";
  22. public static void Install(LobbyScreen owner, Image artwork, int index)
  23. {
  24. if (!artwork || artwork.transform.Find("ModeAnimation")) return;
  25. string key = index == 0 ? "Classic" : index == 1 ? "TimeAttack" : "Zen";
  26. Sprite scene = Load(Scene + key + "_Scene"), halo = Load(Folder + "SoftGlow"), star = Load(Folder + "Star");
  27. Sprite main = Load(index == 0 ? Folder + "Crown" : index == 1 ? Folder + "Hourglass" : Scene + "Lotus_Whole");
  28. Sprite veil = Load(Scene + "EnergyVeil"), rune = Load(Scene + "RuneRing"), water = Load(Scene + "WaterRipple");
  29. Sprite sandTop = index == 1 ? Load(Folder + "Sand_Upper") : null;
  30. Sprite sandBottom = index == 1 ? Load(Folder + "Sand_Lower") : null;
  31. if (!scene || !halo || !star || !main || !veil || !rune || !water ||
  32. (index == 1 && (!sandTop || !sandBottom))) {
  33. Debug.LogWarning("Lobby scene sprites are not imported; keeping original card: " + key, artwork);
  34. return;
  35. }
  36. artwork.sprite = scene;
  37. var root = UIBuilder.Rect("ModeAnimation", artwork.transform, 0, 0, 130, 238);
  38. // Invisible clipping bounds, not a visible frame. Excludes jewels, baked titles and progress.
  39. root.gameObject.AddComponent<RectMask2D>().padding = new Vector4(10, 94, 10, 32);
  40. var view = root.gameObject.AddComponent<LobbyModeMagic>();
  41. view.lobby = owner; view.mode = index;
  42. view.veilLeft = view.Picture("DriftingBackdropLeft", veil, 32, 83, 30, 96);
  43. view.veilRight = view.Picture("DriftingBackdropRight", veil, 98, 82, 29, 94);
  44. view.aura = view.Picture("Atmosphere", halo, 65, 88, 100, 94);
  45. if (index == 1) view.backRing = view.Ring("TemporalOrbit", rune, 65, 81, 91, .55f);
  46. view.footGlow = view.Picture("BaseEnergy", halo, 65, index == 0 ? 124 : 130, 86, 20);
  47. view.floorRing = view.Ring("BaseRing", index == 2 ? water : rune, 65, index == 0 ? 124 : 129, 87, .25f);
  48. if (index == 0) {
  49. view.subject = view.Picture("FloatingCrown", main, 65, 84, 88, 59.6f);
  50. for (int i = 0; i < 3; i++)
  51. view.sparks[i] = view.Picture("CrownJewel" + i, star, 24 + i * 20, 42, 6, 6, view.subject.transform);
  52. } else if (index == 1) {
  53. view.subject = view.Picture("WorkingHourglass", main, 65, 81, 62, 91);
  54. view.upperSand = view.Picture("UpperMagicSand", sandTop, 65, 79.2f, 22.8f, 24.6f);
  55. view.lowerSand = view.Picture("LowerMagicSand", sandBottom, 65, 112.9f, 24.6f, 24.6f);
  56. BottomPivot(view.upperSand, 65, 79.2f); BottomPivot(view.lowerSand, 65, 112.9f);
  57. for (int i = 0; i < 2; i++)
  58. view.sparks[i] = view.Picture("NeckStream" + i, halo, 65, 81, 2.2f, 3.6f);
  59. } else {
  60. // Every frame contains a whole bloom. No cut petals, independent rotations or gaps.
  61. view.lotusFrames = Resources.LoadAll<Sprite>(Scene + "Lotus_Breath");
  62. Array.Sort(view.lotusFrames, (a, b) => string.CompareOrdinal(a.name, b.name));
  63. if (view.lotusFrames.Length == 32) main = view.lotusFrames[0];
  64. else {
  65. view.lotusFrames = null;
  66. Debug.LogWarning("Lotus atlas must contain 32 frames; using intact whole-flower fallback.", artwork);
  67. }
  68. view.subject = view.Picture("WholeBreathingLotus", main, 65, 95, 104, 78);
  69. view.waterRing = view.Ring("OuterWaterRipple", water, 65, 129, 87, .25f);
  70. }
  71. // Two foreground motes per mode, pooled once and faded at the wrap point.
  72. for (int i = 4; i < 6; i++) view.sparks[i] = view.Picture("ForegroundMote" + i, index == 2 ? halo : star, 38 + (i - 4) * 51, 120, 4, 4);
  73. view.ApplyPose();
  74. }
  75. private static Sprite Load(string path) => Resources.Load<Sprite>(path);
  76. private Image Picture(string name, Sprite sprite, float cx, float cy, float w, float h, Transform parent = null)
  77. {
  78. var image = LobbyArtView.Picture(name, parent ? parent : transform, sprite, 0, 0, w, h);
  79. var r = image.rectTransform; r.anchorMin = r.anchorMax = new Vector2(0, 1); r.pivot = new Vector2(.5f, .5f);
  80. r.anchoredPosition = new Vector2(cx, -cy);
  81. image.color = Color.white; image.material = null; image.raycastTarget = false;
  82. return image;
  83. }
  84. private Image Ring(string name, Sprite sprite, float x, float y, float size, float flatten)
  85. {
  86. var plane = UIBuilder.Rect(name + "Plane", transform, x, y, 0, 0);
  87. plane.localScale = new Vector3(1, flatten, 1);
  88. return Picture(name, sprite, 0, 0, size, size, plane);
  89. }
  90. private static void BottomPivot(Image image, float x, float y)
  91. { image.rectTransform.pivot = new Vector2(.5f, 0); image.rectTransform.anchoredPosition = new Vector2(x, -y); }
  92. private static void Tint(Image image, Color tint, float alpha)
  93. { tint.a = Mathf.Clamp01(alpha); image.color = tint; }
  94. private static void Position(Image image, float x, float y)
  95. { image.rectTransform.anchoredPosition = new Vector2(x, -y); }
  96. private static void Turn(Image image, float angle)
  97. { image.rectTransform.localRotation = Quaternion.Euler(0, 0, angle); }
  98. private void Update()
  99. {
  100. if (!lobby || !lobby.IsVisible) return;
  101. clock += Time.unscaledDeltaTime;
  102. ApplyPose();
  103. }
  104. private void ApplyPose()
  105. {
  106. if (!aura) return;
  107. float period = mode == 0 ? 3.8f : mode == 1 ? 2.8f : 4.8f;
  108. float phase = clock * Mathf.PI * 2 / period;
  109. float breath = .5f - .5f * Mathf.Cos(phase);
  110. Color tint = mode == 0 ? Gold : mode == 1 ? Rose : Blue;
  111. // Independent atmospheric layers drift through the painted setting, not the UI layout.
  112. Position(veilLeft, 32 + 2 * Mathf.Sin(phase * .5f), 83 + 3 * Mathf.Cos(phase * .5f));
  113. Position(veilRight, 98 - 2 * Mathf.Sin(phase * .5f), 82 - 3 * Mathf.Cos(phase * .5f));
  114. Turn(veilLeft, 4 * Mathf.Sin(phase * .5f)); Turn(veilRight, -5 * Mathf.Cos(phase * .5f));
  115. Tint(veilLeft, tint, .21f + .07f * breath); Tint(veilRight, tint, .17f + .06f * (1 - breath));
  116. Tint(aura, tint, .08f + .045f * breath);
  117. Tint(footGlow, tint, .16f + .12f * breath);
  118. if (mode == 0) {
  119. Position(subject, 65, 84 - 2.8f * Mathf.Sin(phase));
  120. Turn(floorRing, clock * 9); Tint(floorRing, Gold, .20f + .12f * breath);
  121. floorRing.rectTransform.localScale = Vector3.one * (1 + .035f * Mathf.Sin(phase));
  122. for (int i = 0; i < 3; i++) {
  123. float shimmer = Mathf.Pow(.5f + .5f * Mathf.Sin(phase + i * 1.7f), 4);
  124. Tint(sparks[i], Gold, .04f + .55f * shimmer);
  125. }
  126. } else if (mode == 1) {
  127. upperSand.rectTransform.localScale = new Vector3(1, .68f + .22f * Mathf.Cos(phase), 1);
  128. lowerSand.rectTransform.localScale = new Vector3(1, .68f - .22f * Mathf.Cos(phase), 1);
  129. Tint(upperSand, Color.white, .72f); Tint(lowerSand, Color.white, .72f);
  130. Turn(backRing, clock * 18); Tint(backRing, Rose, .43f);
  131. Turn(floorRing, -clock * 12); Tint(floorRing, Rose, .36f + .08f * breath);
  132. for (int i = 0; i < 2; i++) {
  133. float t = Mathf.Repeat(clock / .82f + i * .5f, 1);
  134. Position(sparks[i], 65 + .5f * Mathf.Sin(t * Mathf.PI * 2), 74.6f + t * 27.3f);
  135. Tint(sparks[i], new Color(1, .87f, .96f), Mathf.Sin(t * Mathf.PI) * .9f);
  136. }
  137. } else {
  138. Position(subject, 65, 95 - 1.1f * Mathf.Sin(phase));
  139. if (lotusFrames != null) {
  140. int frame = Mathf.FloorToInt(Mathf.Repeat(clock / 4.8f, 1) * lotusFrames.Length);
  141. if (frame != lotusFrame) { lotusFrame = frame; subject.sprite = lotusFrames[frame]; }
  142. }
  143. Ripple(floorRing, Mathf.Repeat(clock / 3.2f, 1));
  144. Ripple(waterRing, Mathf.Repeat(clock / 3.2f + .5f, 1));
  145. }
  146. for (int i = 4; i < 6; i++) {
  147. float t = Mathf.Repeat(clock / (period + .8f) + (i - 4) * .5f, 1);
  148. Position(sparks[i], 38 + (i - 4) * 51 + 3 * Mathf.Sin(t * Mathf.PI * 2), 131 - t * 61);
  149. Tint(sparks[i], tint, Mathf.Sin(t * Mathf.PI) * (mode == 2 ? .45f : .55f));
  150. }
  151. }
  152. private static void Ripple(Image image, float t)
  153. {
  154. image.rectTransform.localScale = Vector3.one * (.45f + .76f * t);
  155. Tint(image, Blue, Mathf.Sin(t * Mathf.PI) * .38f);
  156. }
  157. private void OnEnable() { clock = 0; lotusFrame = -1; ApplyPose(); }
  158. private void OnDisable()
  159. {
  160. clock = 0; lotusFrame = -1;
  161. ApplyPose(); // Restore all local baselines; no pending coroutines, tweens or animation callbacks.
  162. }
  163. }
  164. }