HexGamePresentation.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using TMPro;
  7. namespace HexaCrazy
  8. {
  9. public partial class HexGameApp
  10. {
  11. Button[] boardWells;HexStackView[] stackViews;RectTransform[] lockViews;TextMeshProUGUI[] lockLabels;
  12. List<int>[] visibleTiles;bool presenting;int presentedScore;HexScoreBar scoreBar;
  13. readonly List<RectTransform> flightPool=new List<RectTransform>();readonly List<RectTransform> activeFlights=new List<RectTransform>();
  14. public StepKind? AnimationStep {get;private set;}
  15. public int FlyingTileCount=>activeFlights.Count;
  16. int VisibleScore=>presenting?presentedScore:Game.Score;
  17. float RiseCap(int i)=>Mathf.Min(105,Mathf.Max(0,cellPositions[i].y-cellHeight*.5f-(Game.Spec.mode==1?305:265)));
  18. void ResetPresentation()
  19. {
  20. presenting=false;AnimationStep=null;visibleTiles=null;boardWells=null;stackViews=null;lockViews=null;lockLabels=null;scoreBar=null;
  21. activeFlights.Clear();flightPool.Clear();
  22. }
  23. void RenderBoardViews()
  24. {
  25. if(boardLayer==null||Game==null)return;
  26. if(boardWells==null)
  27. {
  28. int count=Game.Cells.Count;boardWells=new Button[count];stackViews=new HexStackView[count];lockViews=new RectTransform[count];lockLabels=new TextMeshProUGUI[count];
  29. foreach(int i in Enumerable.Range(0,count).OrderBy(k=>cellPositions[k].y))
  30. {
  31. int index=i;var p=cellPositions[i];var well=HexUI.Button(boardLayer,"Cell_"+i,"",p.x,p.y,cellWidth,cellHeight,()=>TapCell(index),"well");boardWells[i]=well;
  32. var stack=HexUI.Node(well.transform,"BoardStack_"+i,cellWidth/2,cellHeight/2,cellWidth,cellHeight).gameObject.AddComponent<HexStackView>();
  33. stack.Initialize(this,i,cellWidth,cellHeight,RiseCap(i));stackViews[i]=stack;
  34. var locked=HexUI.Node(well.transform,"Lock",cellWidth/2,cellHeight/2,cellWidth,cellHeight);lockViews[i]=locked;
  35. HexUI.Image(locked,"LockIcon","icon_lock",cellWidth/2,cellHeight*.32f,cellWidth*.27f,cellWidth*.3f);
  36. lockLabels[i]=HexUI.Text(locked,"Remaining","",cellWidth/2,cellHeight*.70f,cellWidth*.8f,cellHeight*.34f,cellWidth*.24f);
  37. }
  38. }
  39. for(int i=0;i<Game.Cells.Count;i++)RenderCell(i);
  40. }
  41. void RenderCell(int i)
  42. {
  43. var cell=Game.Cells[i];var tiles=presenting?visibleTiles[i]:cell.tiles;bool locked=cell.need>VisibleScore;
  44. boardWells[i].image.color=SelectedSlot>=0&&!locked&&tiles.Count==0?new Color(1.15f,1.1f,.7f):Color.white;
  45. lockViews[i].gameObject.SetActive(locked);lockLabels[i].text=locked?(cell.need-VisibleScore).ToString():"";
  46. stackViews[i].SetTiles(tiles,!presenting);stackViews[i].gameObject.SetActive(!locked);
  47. }
  48. void BeginPresentation()
  49. {
  50. visibleTiles=Game.Cells.Select(c=>new List<int>(c.tiles)).ToArray();presentedScore=Game.Score;presenting=true;
  51. }
  52. RectTransform RentFlight(int color,int count,Vector2 origin,float width)
  53. {
  54. RectTransform rt=null;
  55. // Individual flying tiles are reused across all merge/clear waves in this level.
  56. if(count==1&&flightPool.Count>0){rt=flightPool[flightPool.Count-1];flightPool.RemoveAt(flightPool.Count-1);}
  57. if(rt==null)rt=HexUI.Tile(fxLayer,color,count,origin.x,origin.y,width,false);
  58. else
  59. {
  60. var face=rt.Find("Layer_0").GetComponent<Image>();face.sprite=HexUI.Sprite(HexUI.Chips[color]);rt.Find("OriginalSymbol").GetComponent<Image>().sprite=HexUI.Sprite(HexUI.Symbols[color]);
  61. }
  62. rt.name=count==1?"FlyingTile":"FlyingHand";rt.gameObject.SetActive(true);rt.anchoredPosition=new Vector2(origin.x,-origin.y);rt.localScale=Vector3.one;rt.localRotation=Quaternion.identity;
  63. float height=width*HexUI.HexHeightRatio,depth=HexStackView.Depth(width,105,count);
  64. for(int i=0;i<count;i++)((RectTransform)rt.Find("Layer_"+i)).anchoredPosition=new Vector2(width/2,-height/2+i*depth);
  65. var symbol=(RectTransform)rt.Find("OriginalSymbol");symbol.sizeDelta=new Vector2(width*.60f,width*.60f);symbol.anchoredPosition=new Vector2(width/2,-height*.40f+(count-1)*depth);
  66. var group=rt.GetComponent<CanvasGroup>();if(group==null)group=rt.gameObject.AddComponent<CanvasGroup>();
  67. group.alpha=1;group.blocksRaycasts=false;rt.SetAsLastSibling();activeFlights.Add(rt);return rt;
  68. }
  69. void ReturnFlight(RectTransform rt,bool pooled=true)
  70. {
  71. activeFlights.Remove(rt);if(rt==null)return;rt.gameObject.SetActive(false);if(pooled)flightPool.Add(rt);else Destroy(rt.gameObject);
  72. }
  73. Vector2 LandingPoint(int i,int layer)=>cellPositions[i]-Vector2.up*(layer*HexStackView.Depth(cellWidth,RiseCap(i),layer+1));
  74. // All positions use the same design-space coordinates as the persistent stack layers.
  75. void MoveFlight(RectTransform rt,Vector2 from,Vector2 to,float t,bool collect)
  76. {
  77. float e=HexEase.Bezier(t,.38f,.06f,.32f,1);var p=Vector2.Lerp(from,to,e);p.y-=Mathf.Sin(e*Mathf.PI)*(collect?48:68);
  78. rt.anchoredPosition=new Vector2(p.x,-p.y);float scale=collect?Mathf.Lerp(1,.16f,e):1+Mathf.Sin(e*Mathf.PI)*.10f;
  79. rt.localScale=Vector3.one*scale;rt.localRotation=Quaternion.Euler(0,collect?0:e*360,0);
  80. if(collect)rt.GetComponent<CanvasGroup>().alpha=Mathf.Lerp(1,.3f,e);
  81. }
  82. IEnumerator AnimatePresentationStep(RuleStep s)
  83. {
  84. AnimationStep=s.kind;
  85. if(s.kind==StepKind.Neighbor){visibleTiles[s.to].Add(s.color);RenderCell(s.to);stackViews[s.to].Bump();yield break;}
  86. if(s.kind==StepKind.Place)
  87. {
  88. DrawHand();var from=new Vector2(210+s.from*165,1058);var to=cellPositions[s.to];var hand=RentFlight(s.color,s.count,from,cellWidth);
  89. float elapsed=0;while(elapsed<.28f&&!Game.Over){if(!Paused){elapsed+=Time.unscaledDeltaTime;MoveFlight(hand,from,to,Mathf.Clamp01(elapsed/.28f),false);}yield return null;}
  90. ReturnFlight(hand,false);if(Game.Over)yield break;
  91. visibleTiles[s.to].AddRange(Enumerable.Repeat(s.color,s.count));RenderCell(s.to);stackViews[s.to].Bump();yield break;
  92. }
  93. bool collect=s.kind==StepKind.Clear;int source=collect?s.to:s.from;
  94. float stagger=collect?.045f:.06f,travel=collect?.48f:.42f;
  95. var flights=new RectTransform[s.count];var origins=new Vector2[s.count];var targets=new Vector2[s.count];var landed=new bool[s.count];
  96. int launched=0,arrived=0;float time=0;
  97. if(collect)SpawnRing(source);
  98. while(arrived<s.count&&!Game.Over)
  99. {
  100. if(Paused){yield return null;continue;}
  101. while(launched<s.count&&time+0.0001f>=launched*stagger)
  102. {
  103. int i=launched,index=collect?s.startIndex+s.count-1-i:visibleTiles[source].Count-1;
  104. origins[i]=stackViews[source].TileDesignCenter(index);
  105. targets[i]=collect?scoreBar.HeadDesignPosition:LandingPoint(s.to,s.baseCount+i);
  106. visibleTiles[source].RemoveAt(index);RenderCell(source);
  107. flights[i]=RentFlight(s.color,1,origins[i],cellWidth);launched++;
  108. }
  109. for(int i=0;i<launched;i++)
  110. {
  111. if(landed[i])continue;float p=Mathf.Clamp01((time-i*stagger)/travel);MoveFlight(flights[i],origins[i],targets[i],p,collect);
  112. if(p>=1)
  113. {
  114. ReturnFlight(flights[i]);landed[i]=true;arrived++;
  115. if(!collect){visibleTiles[s.to].Add(s.color);RenderCell(s.to);audioPlayer.Play("drop");}
  116. }
  117. }
  118. time+=Time.unscaledDeltaTime;yield return null;
  119. }
  120. if(Game.Over)yield break;
  121. stackViews[s.to].Bump();
  122. if(!collect){float wait=0;while(wait<.11f&&!Game.Over){if(!Paused)wait+=Time.unscaledDeltaTime;yield return null;}}
  123. }
  124. void CommitPresentationStep(RuleStep s)
  125. {
  126. if(Profile.Data.reduceMotion)
  127. {
  128. if(s.kind==StepKind.Merge)visibleTiles[s.from]=new List<int>(Game.Cells[s.from].tiles);
  129. visibleTiles[s.to]=new List<int>(Game.Cells[s.to].tiles);
  130. }
  131. presentedScore=Game.Score;DrawBoard();UpdateHud();
  132. if(s.kind==StepKind.Clear)
  133. {
  134. scoreBar.SetScore(presentedScore,Profile.Data.reduceMotion,true);SpawnScoreFloat(s.to,s.gain,s.chain);
  135. if(Profile.Data.tutorial==0){Profile.Data.tutorial=1;Profile.Save();var tutorial=fxLayer.Find("Tutorial");if(tutorial!=null)Destroy(tutorial.gameObject);hintText.text="DRAG TO AN EMPTY CELL";}
  136. }
  137. }
  138. void SpawnRing(int cell)
  139. {
  140. var p=cellPositions[cell];var ring=HexRadial.Create(fxLayer,"CollectRing",p.x,p.y,20,true);var effect=ring.gameObject.AddComponent<HexFloatingFeedback>();effect.owner=this;effect.ring=true;effect.duration=.55f;
  141. }
  142. void SpawnScoreFloat(int cell,int gain,int chain)
  143. {
  144. var p=cellPositions[cell];float y=p.y-cellHeight*.5f-25;
  145. var root=HexUI.Node(fxLayer,"ScoreFloat",Mathf.Clamp(p.x,115,635),Mathf.Max(315,y),230,70);
  146. HexUI.Text(root,"Shadow","+"+gain,117,38,230,65,chain>1?43:36,false,new Color(.18f,.05f,.01f,.9f));
  147. HexUI.Text(root,"Gain","+"+gain,115,35,230,65,chain>1?43:36,false,HexUI.Cream);
  148. var effect=root.gameObject.AddComponent<HexFloatingFeedback>();effect.owner=this;effect.reduced=Profile.Data.reduceMotion;
  149. if(chain>1)
  150. {
  151. var old=fxLayer.Find("ChainBanner");if(old!=null)Destroy(old.gameObject);
  152. var banner=HexUI.Node(fxLayer,"ChainBanner",375,300,360,55);HexUI.Image(banner,"Plate","panel",180,27.5f,360,55,null,true);HexUI.Text(banner,"Chain","CHAIN ×"+chain,180,27,340,48,30,true);
  153. var pulse=banner.gameObject.AddComponent<HexFloatingFeedback>();pulse.owner=this;pulse.duration=.9f;pulse.reduced=Profile.Data.reduceMotion;
  154. }
  155. }
  156. }
  157. }