FateforgeMotion.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace Fateforge
  8. {
  9. public sealed partial class FateforgeApp
  10. {
  11. public string MotionPhase {get;private set;}="idle";
  12. public double DisplayedScore {get;private set;}
  13. public double DisplayedPreviewTotal=>displayedTotal;
  14. public int PresentationGroupCount {get;private set;}
  15. bool scoreAnimating,presentationPaused,numbersRunning;
  16. float idlePulse;
  17. RectTransform motionLayer;
  18. RollingCounter pointsCounter,multiplierCounter,totalCounter,scoreCounter,runScoreCounter,coinCounter;
  19. readonly List<Match> presentationMatches=new List<Match>();
  20. void InitializeMotion()
  21. {
  22. motionLayer=Ui.Rect(view.play,"PresentationEffects",0,0,750,1334);Ui.Stretch(motionLayer);
  23. pointsCounter=RollingCounter.Attach(view.previewPoints);multiplierCounter=RollingCounter.Attach(view.previewMultiplier);totalCounter=RollingCounter.Attach(view.previewTotal);scoreCounter=RollingCounter.Attach(view.score);
  24. runScoreCounter=RollingCounter.Attach(view.level);
  25. coinCounter=RollingCounter.Attach(view.coins);
  26. if(view.collectLever && view.collectLever.lever)view.collectLever.lever.gameObject.SetActive(false);
  27. }
  28. void UpdateMotion()
  29. {
  30. if(!HomeActive && activeSkill!="" && !Busy)
  31. {
  32. skillAge+=Delta;
  33. int index=activeSkill=="swap"?0:1;
  34. if(skillOrbs[index])skillOrbs[index].Show(true,true,skillAge);
  35. }
  36. if(HomeActive||Busy||Game==null||Game.State.stage!=Stage.Battle||MotionPhase!="idle"||presentationMatches.Count==0)return;
  37. idlePulse+=Delta;PaintGroups(presentationMatches,null,.035f+.025f*Mathf.Sin(idlePulse*2.6f),1);
  38. }
  39. void ResetPresentation()
  40. {
  41. numberEpoch++;numbersRunning=false;scoreAnimating=false;MotionPhase="idle";PresentationGroupCount=0;presentationMatches.Clear();
  42. if(bossIntroduction){Destroy(bossIntroduction.gameObject);bossIntroduction=null;}
  43. if(motionLayer)Ui.Clear(motionLayer);if(view.connections)view.connections.Clear();
  44. foreach(var card in cards.Values)if(card){var fx=card.GetComponent<CardMatchEffects>();if(fx)fx.ResetVisuals();}
  45. if(pointsCounter)pointsCounter.Cancel(displayedPoints);if(multiplierCounter)multiplierCounter.Cancel(displayedMultiplier);if(totalCounter)totalCounter.Cancel(displayedTotal);
  46. if(runScoreCounter && runScoreCounter.IsRolling) runScoreCounter.Finish();
  47. if(coinCounter && coinCounter.IsRolling) coinCounter.Finish();
  48. DisplayedScore=Game==null?0:Game.Board.score;if(scoreCounter)scoreCounter.Cancel(DisplayedScore);
  49. }
  50. CardMatchEffects CardEffects(CardView card)
  51. {
  52. var effects=card.GetComponent<CardMatchEffects>();if(!effects){effects=card.gameObject.AddComponent<CardMatchEffects>();effects.Configure(card,view.theme,view.art.Sprite("spark"));}return effects;
  53. }
  54. void PaintGroups(IEnumerable<Match> matches,Match focus=null,float pulse=0,float background=1)
  55. {
  56. var masks=new Dictionary<int,int>();var focused=new HashSet<int>();
  57. foreach(var match in matches)foreach(int i in match.cells)if(i>=0&&i<Game.Board.cells.Count){int id=Game.Board.cells[i].id;masks[id]=(masks.TryGetValue(id,out int current)?current:0)|(1<<match.family);}
  58. if(focus!=null)foreach(int i in focus.cells)if(i>=0&&i<Game.Board.cells.Count)focused.Add(Game.Board.cells[i].id);
  59. bool targeting=Game.State.stage==Stage.Battle && !string.IsNullOrEmpty(Game.Board.operation);
  60. int first=Game.Board.selected;
  61. int? pickedId=targeting && Game.Board.operation=="swap" && first>=0 && first<Game.Board.cells.Count
  62. ? Game.Board.cells[first].id : (int?)null;
  63. foreach(var pair in cards)
  64. {
  65. var card=pair.Value;var fx=CardEffects(card);
  66. fx.SetFamilies(!targeting && masks.TryGetValue(pair.Key,out int mask)?mask:0);
  67. bool selected=focus==null || focused.Contains(pair.Key);
  68. fx.Sample(!targeting && selected?pulse:0,selected?1:background);
  69. card.SetSelected(card.CardId==pickedId,view.theme);
  70. }
  71. if(view.connections)view.connections.Clear();
  72. }
  73. Vector2 MotionPoint(RectTransform rect)
  74. {return motionLayer.InverseTransformPoint(rect.TransformPoint(rect.rect.center));}
  75. TMP_Text FloatingNumber(string text,Color color,Vector2 position)
  76. {
  77. var label=Ui.Text(motionLayer,"MatchAddition",text,position.x,-position.y,150,58,41,view.theme);label.font=view.theme.titleFont;label.fontStyle=FontStyles.Bold;label.color=color;
  78. label.outlineWidth=.18f;label.outlineColor=new Color32(18,8,27,255);return label;
  79. }
  80. IEnumerator RevealGroup(List<Match> visible,Match match,int token,double points,double multiplier,bool settlement)
  81. {
  82. TMP_Text pointsLabel=null,multiplierLabel=null;Vector2 origin=Vector2.zero;
  83. var matched=match.cells.Where(i=>i>=0&&i<Game.Board.cells.Count&&cards.ContainsKey(Game.Board.cells[i].id)).Select(i=>cards[Game.Board.cells[i].id]).ToArray();
  84. if(matched.Length>0){foreach(var card in matched)origin+=MotionPoint(card.Rect);origin/=matched.Length;}
  85. if(settlement){pointsLabel=FloatingNumber("+"+English.Number(match.points),view.theme.cyan,origin+new Vector2(-44,22));multiplierLabel=FloatingNumber("+"+English.Multiplier(match.multiplier),new Color(1,.38f,.69f),origin+new Vector2(53,-12));}
  86. float duration=settlement?.58f:.30f,t=0;bool sent=false;Sound("match");
  87. while(t<duration&&token==epoch){t+=Delta;float u=Mathf.Clamp01(t/duration);float impact=Mathf.Clamp01(1-Mathf.Abs((u-.24f)/.34f));PaintGroups(visible,match,impact,settlement?.30f:1);
  88. if(pointsLabel){float fly=Mathf.SmoothStep(0,1,Mathf.Clamp01((u-.52f)/.48f));float pop=1+.17f*Mathf.Sin(Mathf.Clamp01(u/.6f)*Mathf.PI);Vector2 a=Vector2.Lerp(origin+new Vector2(-44,22+22*u),MotionPoint(view.previewPoints.rectTransform),fly),b=Vector2.Lerp(origin+new Vector2(53,-12+22*u),MotionPoint(view.previewMultiplier.rectTransform),fly);pointsLabel.rectTransform.localPosition=a;multiplierLabel.rectTransform.localPosition=b;pointsLabel.transform.localScale=multiplierLabel.transform.localScale=Vector3.one*Mathf.Lerp(pop,.55f,fly);pointsLabel.alpha=multiplierLabel.alpha=1-fly*.78f;}
  89. if(!sent&&u>=.54f){sent=true;RollNumbers(points,multiplier);}yield return null;
  90. }
  91. if(pointsLabel)Destroy(pointsLabel.gameObject);if(multiplierLabel)Destroy(multiplierLabel.gameObject);
  92. if(token==epoch&&!sent)RollNumbers(points,multiplier);
  93. }
  94. IEnumerator RevealMagnet(MagnetStep step,double priorPoints,double priorMultiplier,int token)
  95. {
  96. string name=view.art.Name(step.id);
  97. Message(name);
  98. PlayEffect(view.theme.magnetActivation,.85f);
  99. RollNumbers(step.points,step.multiplier);
  100. var slot=view.magnetRack.Find("Magnet_"+step.index) as RectTransform;
  101. if(!slot){yield return Delay(1.15f,token);yield break;}
  102. string effect="";
  103. double points=step.points-priorPoints, multiplier=step.multiplier-priorMultiplier;
  104. if(points!=0)effect=(points>0?"+":"")+English.Number(points)+" points";
  105. else if(System.Math.Abs(multiplier)>.00001)effect="+"+English.Multiplier(multiplier)+" mult";
  106. else if(step.id=="ticket_or")effect="+"+Game.State.tickets.Count+" coins";
  107. else if(step.id=="chevaliere")effect="+5 per matched card";
  108. var origin=MotionPoint(slot);
  109. string lettering="<smallcaps>"+name+"</smallcaps>"+(effect==""?"":"\n<size=122%>"+effect+"</size>");
  110. var ribbon=Ui.Glyph(motionLayer,"MagnetActivation",UiGlyph.Shape.Ribbon,0,0,310,88,view.theme.gold);
  111. var display=ribbon.rectTransform;display.pivot=Vector2.one*.5f;
  112. var label=Ui.Text(display,"Lettering",lettering,0,2,253,82,27,view.theme);
  113. label.font=view.theme.titleFont;label.fontStyle=FontStyles.Bold|FontStyles.Italic;
  114. label.characterSpacing=1.5f;label.lineSpacing=-13;
  115. label.enableVertexGradient=true;
  116. label.colorGradient=new VertexGradient(Color.white,new Color(1,.96f,.73f),new Color(1,.67f,.31f),new Color(1,.85f,.49f));
  117. label.color=Color.white;label.outlineColor=new Color32(49,13,32,255);label.outlineWidth=.17f;
  118. float width=Mathf.Clamp(label.GetPreferredValues(lettering).x+55,218,310);
  119. display.sizeDelta=new Vector2(width,88);label.rectTransform.sizeDelta=new Vector2(width-45,82);
  120. float margin=width*.55f+12;
  121. origin.x=Mathf.Clamp(origin.x,motionLayer.rect.xMin+margin,motionLayer.rect.xMax-margin);
  122. var fade=ribbon.gameObject.AddComponent<CanvasGroup>();fade.blocksRaycasts=false;
  123. for(int side=-1;side<=1;side+=2)
  124. Ui.Glyph(display,"Spark",UiGlyph.Shape.Glint,side*(width/2-15),35,10,18,view.theme.gold);
  125. for(float t=0;t<1.15f && token==epoch;t+=Delta)
  126. {
  127. float u=Mathf.Clamp01(t/1.15f);
  128. display.localPosition=origin+Vector2.down*Mathf.Lerp(102,64,u);
  129. float pop=Mathf.Sin(Mathf.Clamp01(u/.30f)*Mathf.PI);
  130. display.localScale=Vector3.one*(.98f+.09f*pop);
  131. display.localRotation=Quaternion.Euler(0,0,2*pop);
  132. fade.alpha=Mathf.Min(1,u*14,(1-u)*5);
  133. slot.localScale=Vector3.one*(1+.16f*Mathf.Sin(Mathf.Clamp01(u*2)*Mathf.PI));
  134. yield return null;
  135. }
  136. if(slot)slot.localScale=Vector3.one;
  137. if(ribbon)Destroy(ribbon.gameObject);
  138. }
  139. IEnumerator RollCounter(RollingCounter counter, long before, long after, int token, string prefix = "")
  140. {
  141. const float duration = .65f;
  142. counter.Begin(before, after, prefix);
  143. float elapsed = 0;
  144. while (elapsed < duration && token == epoch)
  145. {
  146. elapsed += Delta;
  147. counter.Sample(elapsed / duration);
  148. yield return null;
  149. }
  150. }
  151. IEnumerator TransferScore(ScoreResult result,int token)
  152. {
  153. MotionPhase="transfer";PaintGroups(result.matches);var starts=new List<Vector2>();
  154. var matched=result.matches.SelectMany(m=>m.cells).Distinct().Where(i=>i>=0&&i<Game.Board.cells.Count&&cards.ContainsKey(Game.Board.cells[i].id)).Select(i=>cards[Game.Board.cells[i].id]).ToArray();
  155. Vector2 end=MotionPoint(view.score.rectTransform);int count=matched.Length==0?0:Mathf.Clamp(matched.Length*5,18,52);
  156. for(int i=0;i<count;i++){var source=MotionPoint(matched[i%matched.Length].Rect);source+=new Vector2(Mathf.Sin(i*2.4f)*28,Mathf.Cos(i*1.7f)*25);starts.Add(source);}
  157. var streakRect=Ui.Rect(motionLayer,"ScoreTransfer",0,0,750,1334);Ui.Stretch(streakRect);var streaks=streakRect.gameObject.AddComponent<ScoreStreaks>();streaks.Configure(starts.ToArray(),end);
  158. double from=DisplayedScore,to=result.hasScoreSnapshot?result.scoreAfter:Game.Board.score;scoreCounter.Begin(from,to,false);float t=0;const float duration=1.55f; int tick=0;
  159. while(t<duration&&token==epoch){t+=Delta;float u=Mathf.Clamp01(t/duration);float progress=Mathf.Clamp01((u-.08f)/.84f);DisplayedScore=from+(to-from)*(1-System.Math.Pow(1-progress,3));scoreCounter.Sample(progress);
  160. int beat=Mathf.FloorToInt(progress*9);
  161. if(from!=to && beat>tick && progress<1){tick=beat;PlayEffect(view.theme.scoreTick,.50f);}
  162. streaks.Sample(u);
  163. view.score.rectTransform.localScale=Vector3.one*(1+.065f*Mathf.Sin(progress*Mathf.PI));yield return null;
  164. }
  165. if(streaks)Destroy(streaks.gameObject);view.score.rectTransform.localScale=Vector3.one;
  166. if(token==epoch){DisplayedScore=to;scoreCounter.Finish();scoreAnimating=false;if(from!=to)PlayEffect(view.theme.scoreImpact);}
  167. }
  168. }
  169. }