using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; using TMPro; namespace HexaCrazy { public partial class HexGameApp { Button[] boardWells;HexStackView[] stackViews;RectTransform[] lockViews;TextMeshProUGUI[] lockLabels; List[] visibleTiles;bool presenting;int presentedScore;HexScoreBar scoreBar; readonly List flightPool=new List();readonly List activeFlights=new List(); public StepKind? AnimationStep {get;private set;} public int FlyingTileCount=>activeFlights.Count; int VisibleScore=>presenting?presentedScore:Game.Score; float RiseCap(int i)=>Mathf.Min(105,Mathf.Max(0,cellPositions[i].y-cellHeight*.5f-(Game.Spec.mode==1?305:265))); void ResetPresentation() { presenting=false;AnimationStep=null;visibleTiles=null;boardWells=null;stackViews=null;lockViews=null;lockLabels=null;scoreBar=null; activeFlights.Clear();flightPool.Clear(); } void RenderBoardViews() { if(boardLayer==null||Game==null)return; if(boardWells==null) { int count=Game.Cells.Count;boardWells=new Button[count];stackViews=new HexStackView[count];lockViews=new RectTransform[count];lockLabels=new TextMeshProUGUI[count]; foreach(int i in Enumerable.Range(0,count).OrderBy(k=>cellPositions[k].y)) { 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; var stack=HexUI.Node(well.transform,"BoardStack_"+i,cellWidth/2,cellHeight/2,cellWidth,cellHeight).gameObject.AddComponent(); stack.Initialize(this,i,cellWidth,cellHeight,RiseCap(i));stackViews[i]=stack; var locked=HexUI.Node(well.transform,"Lock",cellWidth/2,cellHeight/2,cellWidth,cellHeight);lockViews[i]=locked; HexUI.Image(locked,"LockIcon","icon_lock",cellWidth/2,cellHeight*.32f,cellWidth*.27f,cellWidth*.3f); lockLabels[i]=HexUI.Text(locked,"Remaining","",cellWidth/2,cellHeight*.70f,cellWidth*.8f,cellHeight*.34f,cellWidth*.24f); } } for(int i=0;iVisibleScore; boardWells[i].image.color=SelectedSlot>=0&&!locked&&tiles.Count==0?new Color(1.15f,1.1f,.7f):Color.white; lockViews[i].gameObject.SetActive(locked);lockLabels[i].text=locked?(cell.need-VisibleScore).ToString():""; stackViews[i].SetTiles(tiles,!presenting);stackViews[i].gameObject.SetActive(!locked); } void BeginPresentation() { visibleTiles=Game.Cells.Select(c=>new List(c.tiles)).ToArray();presentedScore=Game.Score;presenting=true; } RectTransform RentFlight(int color,int count,Vector2 origin,float width) { RectTransform rt=null; // Individual flying tiles are reused across all merge/clear waves in this level. if(count==1&&flightPool.Count>0){rt=flightPool[flightPool.Count-1];flightPool.RemoveAt(flightPool.Count-1);} if(rt==null)rt=HexUI.Tile(fxLayer,color,count,origin.x,origin.y,width,false); else { var face=rt.Find("Layer_0").GetComponent();face.sprite=HexUI.Sprite(HexUI.Chips[color]);rt.Find("OriginalSymbol").GetComponent().sprite=HexUI.Sprite(HexUI.Symbols[color]); } 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; float height=width*HexUI.HexHeightRatio,depth=HexStackView.Depth(width,105,count); for(int i=0;i();if(group==null)group=rt.gameObject.AddComponent(); group.alpha=1;group.blocksRaycasts=false;rt.SetAsLastSibling();activeFlights.Add(rt);return rt; } void ReturnFlight(RectTransform rt,bool pooled=true) { activeFlights.Remove(rt);if(rt==null)return;rt.gameObject.SetActive(false);if(pooled)flightPool.Add(rt);else Destroy(rt.gameObject); } Vector2 LandingPoint(int i,int layer)=>cellPositions[i]-Vector2.up*(layer*HexStackView.Depth(cellWidth,RiseCap(i),layer+1)); // All positions use the same design-space coordinates as the persistent stack layers. void MoveFlight(RectTransform rt,Vector2 from,Vector2 to,float t,bool collect) { 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); rt.anchoredPosition=new Vector2(p.x,-p.y);float scale=collect?Mathf.Lerp(1,.16f,e):1+Mathf.Sin(e*Mathf.PI)*.10f; rt.localScale=Vector3.one*scale;rt.localRotation=Quaternion.Euler(0,collect?0:e*360,0); if(collect)rt.GetComponent().alpha=Mathf.Lerp(1,.3f,e); } IEnumerator AnimatePresentationStep(RuleStep s) { AnimationStep=s.kind; if(s.kind==StepKind.Neighbor){visibleTiles[s.to].Add(s.color);RenderCell(s.to);stackViews[s.to].Bump();yield break;} if(s.kind==StepKind.Place) { DrawHand();var from=new Vector2(210+s.from*165,1058);var to=cellPositions[s.to];var hand=RentFlight(s.color,s.count,from,cellWidth); 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;} ReturnFlight(hand,false);if(Game.Over)yield break; visibleTiles[s.to].AddRange(Enumerable.Repeat(s.color,s.count));RenderCell(s.to);stackViews[s.to].Bump();yield break; } bool collect=s.kind==StepKind.Clear;int source=collect?s.to:s.from; float stagger=collect?.045f:.06f,travel=collect?.48f:.42f; var flights=new RectTransform[s.count];var origins=new Vector2[s.count];var targets=new Vector2[s.count];var landed=new bool[s.count]; int launched=0,arrived=0;float time=0; if(collect)SpawnRing(source); while(arrived=launched*stagger) { int i=launched,index=collect?s.startIndex+s.count-1-i:visibleTiles[source].Count-1; origins[i]=stackViews[source].TileDesignCenter(index); targets[i]=collect?scoreBar.HeadDesignPosition:LandingPoint(s.to,s.baseCount+i); visibleTiles[source].RemoveAt(index);RenderCell(source); flights[i]=RentFlight(s.color,1,origins[i],cellWidth);launched++; } for(int i=0;i=1) { ReturnFlight(flights[i]);landed[i]=true;arrived++; if(!collect){visibleTiles[s.to].Add(s.color);RenderCell(s.to);audioPlayer.Play("drop");} } } time+=Time.unscaledDeltaTime;yield return null; } if(Game.Over)yield break; stackViews[s.to].Bump(); if(!collect){float wait=0;while(wait<.11f&&!Game.Over){if(!Paused)wait+=Time.unscaledDeltaTime;yield return null;}} } void CommitPresentationStep(RuleStep s) { if(Profile.Data.reduceMotion) { if(s.kind==StepKind.Merge)visibleTiles[s.from]=new List(Game.Cells[s.from].tiles); visibleTiles[s.to]=new List(Game.Cells[s.to].tiles); } presentedScore=Game.Score;DrawBoard();UpdateHud(); if(s.kind==StepKind.Clear) { scoreBar.SetScore(presentedScore,Profile.Data.reduceMotion,true);SpawnScoreFloat(s.to,s.gain,s.chain); 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";} } } void SpawnRing(int cell) { var p=cellPositions[cell];var ring=HexRadial.Create(fxLayer,"CollectRing",p.x,p.y,20,true);var effect=ring.gameObject.AddComponent();effect.owner=this;effect.ring=true;effect.duration=.55f; } void SpawnScoreFloat(int cell,int gain,int chain) { var p=cellPositions[cell];float y=p.y-cellHeight*.5f-25; var root=HexUI.Node(fxLayer,"ScoreFloat",Mathf.Clamp(p.x,115,635),Mathf.Max(315,y),230,70); HexUI.Text(root,"Shadow","+"+gain,117,38,230,65,chain>1?43:36,false,new Color(.18f,.05f,.01f,.9f)); HexUI.Text(root,"Gain","+"+gain,115,35,230,65,chain>1?43:36,false,HexUI.Cream); var effect=root.gameObject.AddComponent();effect.owner=this;effect.reduced=Profile.Data.reduceMotion; if(chain>1) { var old=fxLayer.Find("ChainBanner");if(old!=null)Destroy(old.gameObject); 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); var pulse=banner.gameObject.AddComponent();pulse.owner=this;pulse.duration=.9f;pulse.reduced=Profile.Data.reduceMotion; } } } }