| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Fateforge
- {
- public sealed partial class FateforgeApp
- {
- public string MotionPhase {get;private set;}="idle";
- public double DisplayedScore {get;private set;}
- public double DisplayedPreviewTotal=>displayedTotal;
- public int PresentationGroupCount {get;private set;}
- bool scoreAnimating,presentationPaused,numbersRunning;
- float idlePulse;
- RectTransform motionLayer;
- RollingCounter pointsCounter,multiplierCounter,totalCounter,scoreCounter,runScoreCounter,coinCounter;
- readonly List<Match> presentationMatches=new List<Match>();
- void InitializeMotion()
- {
- motionLayer=Ui.Rect(view.play,"PresentationEffects",0,0,750,1334);Ui.Stretch(motionLayer);
- pointsCounter=RollingCounter.Attach(view.previewPoints);multiplierCounter=RollingCounter.Attach(view.previewMultiplier);totalCounter=RollingCounter.Attach(view.previewTotal);scoreCounter=RollingCounter.Attach(view.score);
- runScoreCounter=RollingCounter.Attach(view.level);
- coinCounter=RollingCounter.Attach(view.coins);
- if(view.collectLever && view.collectLever.lever)view.collectLever.lever.gameObject.SetActive(false);
- }
- void UpdateMotion()
- {
- if(!HomeActive && activeSkill!="" && !Busy)
- {
- skillAge+=Delta;
- int index=activeSkill=="swap"?0:1;
- if(skillOrbs[index])skillOrbs[index].Show(true,true,skillAge);
- }
- if(HomeActive||Busy||Game==null||Game.State.stage!=Stage.Battle||MotionPhase!="idle"||presentationMatches.Count==0)return;
- idlePulse+=Delta;PaintGroups(presentationMatches,null,.035f+.025f*Mathf.Sin(idlePulse*2.6f),1);
- }
- void ResetPresentation()
- {
- numberEpoch++;numbersRunning=false;scoreAnimating=false;MotionPhase="idle";PresentationGroupCount=0;presentationMatches.Clear();
- if(bossIntroduction){Destroy(bossIntroduction.gameObject);bossIntroduction=null;}
- if(motionLayer)Ui.Clear(motionLayer);if(view.connections)view.connections.Clear();
- foreach(var card in cards.Values)if(card){var fx=card.GetComponent<CardMatchEffects>();if(fx)fx.ResetVisuals();}
- if(pointsCounter)pointsCounter.Cancel(displayedPoints);if(multiplierCounter)multiplierCounter.Cancel(displayedMultiplier);if(totalCounter)totalCounter.Cancel(displayedTotal);
- if(runScoreCounter && runScoreCounter.IsRolling) runScoreCounter.Finish();
- if(coinCounter && coinCounter.IsRolling) coinCounter.Finish();
- DisplayedScore=Game==null?0:Game.Board.score;if(scoreCounter)scoreCounter.Cancel(DisplayedScore);
- }
- CardMatchEffects CardEffects(CardView card)
- {
- var effects=card.GetComponent<CardMatchEffects>();if(!effects){effects=card.gameObject.AddComponent<CardMatchEffects>();effects.Configure(card,view.theme,view.art.Sprite("spark"));}return effects;
- }
- void PaintGroups(IEnumerable<Match> matches,Match focus=null,float pulse=0,float background=1)
- {
- var masks=new Dictionary<int,int>();var focused=new HashSet<int>();
- 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);}
- if(focus!=null)foreach(int i in focus.cells)if(i>=0&&i<Game.Board.cells.Count)focused.Add(Game.Board.cells[i].id);
- bool targeting=Game.State.stage==Stage.Battle && !string.IsNullOrEmpty(Game.Board.operation);
- int first=Game.Board.selected;
- int? pickedId=targeting && Game.Board.operation=="swap" && first>=0 && first<Game.Board.cells.Count
- ? Game.Board.cells[first].id : (int?)null;
- foreach(var pair in cards)
- {
- var card=pair.Value;var fx=CardEffects(card);
- fx.SetFamilies(!targeting && masks.TryGetValue(pair.Key,out int mask)?mask:0);
- bool selected=focus==null || focused.Contains(pair.Key);
- fx.Sample(!targeting && selected?pulse:0,selected?1:background);
- card.SetSelected(card.CardId==pickedId,view.theme);
- }
- if(view.connections)view.connections.Clear();
- }
- Vector2 MotionPoint(RectTransform rect)
- {return motionLayer.InverseTransformPoint(rect.TransformPoint(rect.rect.center));}
- TMP_Text FloatingNumber(string text,Color color,Vector2 position)
- {
- 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;
- label.outlineWidth=.18f;label.outlineColor=new Color32(18,8,27,255);return label;
- }
- IEnumerator RevealGroup(List<Match> visible,Match match,int token,double points,double multiplier,bool settlement)
- {
- TMP_Text pointsLabel=null,multiplierLabel=null;Vector2 origin=Vector2.zero;
- 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();
- if(matched.Length>0){foreach(var card in matched)origin+=MotionPoint(card.Rect);origin/=matched.Length;}
- 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));}
- float duration=settlement?.58f:.30f,t=0;bool sent=false;Sound("match");
- 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);
- 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;}
- if(!sent&&u>=.54f){sent=true;RollNumbers(points,multiplier);}yield return null;
- }
- if(pointsLabel)Destroy(pointsLabel.gameObject);if(multiplierLabel)Destroy(multiplierLabel.gameObject);
- if(token==epoch&&!sent)RollNumbers(points,multiplier);
- }
- IEnumerator RevealMagnet(MagnetStep step,double priorPoints,double priorMultiplier,int token)
- {
- string name=view.art.Name(step.id);
- Message(name);
- PlayEffect(view.theme.magnetActivation,.85f);
- RollNumbers(step.points,step.multiplier);
- var slot=view.magnetRack.Find("Magnet_"+step.index) as RectTransform;
- if(!slot){yield return Delay(1.15f,token);yield break;}
- string effect="";
- double points=step.points-priorPoints, multiplier=step.multiplier-priorMultiplier;
- if(points!=0)effect=(points>0?"+":"")+English.Number(points)+" points";
- else if(System.Math.Abs(multiplier)>.00001)effect="+"+English.Multiplier(multiplier)+" mult";
- else if(step.id=="ticket_or")effect="+"+Game.State.tickets.Count+" coins";
- else if(step.id=="chevaliere")effect="+5 per matched card";
- var origin=MotionPoint(slot);
- string lettering="<smallcaps>"+name+"</smallcaps>"+(effect==""?"":"\n<size=122%>"+effect+"</size>");
- var ribbon=Ui.Glyph(motionLayer,"MagnetActivation",UiGlyph.Shape.Ribbon,0,0,310,88,view.theme.gold);
- var display=ribbon.rectTransform;display.pivot=Vector2.one*.5f;
- var label=Ui.Text(display,"Lettering",lettering,0,2,253,82,27,view.theme);
- label.font=view.theme.titleFont;label.fontStyle=FontStyles.Bold|FontStyles.Italic;
- label.characterSpacing=1.5f;label.lineSpacing=-13;
- label.enableVertexGradient=true;
- label.colorGradient=new VertexGradient(Color.white,new Color(1,.96f,.73f),new Color(1,.67f,.31f),new Color(1,.85f,.49f));
- label.color=Color.white;label.outlineColor=new Color32(49,13,32,255);label.outlineWidth=.17f;
- float width=Mathf.Clamp(label.GetPreferredValues(lettering).x+55,218,310);
- display.sizeDelta=new Vector2(width,88);label.rectTransform.sizeDelta=new Vector2(width-45,82);
- float margin=width*.55f+12;
- origin.x=Mathf.Clamp(origin.x,motionLayer.rect.xMin+margin,motionLayer.rect.xMax-margin);
- var fade=ribbon.gameObject.AddComponent<CanvasGroup>();fade.blocksRaycasts=false;
- for(int side=-1;side<=1;side+=2)
- Ui.Glyph(display,"Spark",UiGlyph.Shape.Glint,side*(width/2-15),35,10,18,view.theme.gold);
- for(float t=0;t<1.15f && token==epoch;t+=Delta)
- {
- float u=Mathf.Clamp01(t/1.15f);
- display.localPosition=origin+Vector2.down*Mathf.Lerp(102,64,u);
- float pop=Mathf.Sin(Mathf.Clamp01(u/.30f)*Mathf.PI);
- display.localScale=Vector3.one*(.98f+.09f*pop);
- display.localRotation=Quaternion.Euler(0,0,2*pop);
- fade.alpha=Mathf.Min(1,u*14,(1-u)*5);
- slot.localScale=Vector3.one*(1+.16f*Mathf.Sin(Mathf.Clamp01(u*2)*Mathf.PI));
- yield return null;
- }
- if(slot)slot.localScale=Vector3.one;
- if(ribbon)Destroy(ribbon.gameObject);
- }
- IEnumerator RollCounter(RollingCounter counter, long before, long after, int token, string prefix = "")
- {
- const float duration = .65f;
- counter.Begin(before, after, prefix);
- float elapsed = 0;
- while (elapsed < duration && token == epoch)
- {
- elapsed += Delta;
- counter.Sample(elapsed / duration);
- yield return null;
- }
- }
- IEnumerator TransferScore(ScoreResult result,int token)
- {
- MotionPhase="transfer";PaintGroups(result.matches);var starts=new List<Vector2>();
- 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();
- Vector2 end=MotionPoint(view.score.rectTransform);int count=matched.Length==0?0:Mathf.Clamp(matched.Length*5,18,52);
- 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);}
- var streakRect=Ui.Rect(motionLayer,"ScoreTransfer",0,0,750,1334);Ui.Stretch(streakRect);var streaks=streakRect.gameObject.AddComponent<ScoreStreaks>();streaks.Configure(starts.ToArray(),end);
- 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;
- 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);
- int beat=Mathf.FloorToInt(progress*9);
- if(from!=to && beat>tick && progress<1){tick=beat;PlayEffect(view.theme.scoreTick,.50f);}
- streaks.Sample(u);
- view.score.rectTransform.localScale=Vector3.one*(1+.065f*Mathf.Sin(progress*Mathf.PI));yield return null;
- }
- if(streaks)Destroy(streaks.gameObject);view.score.rectTransform.localScale=Vector3.one;
- if(token==epoch){DisplayedScore=to;scoreCounter.Finish();scoreAnimating=false;if(from!=to)PlayEffect(view.theme.scoreImpact);}
- }
- }
- }
|