| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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<int>[] visibleTiles;bool presenting;int presentedScore;HexScoreBar scoreBar;
- readonly List<RectTransform> flightPool=new List<RectTransform>();readonly List<RectTransform> activeFlights=new List<RectTransform>();
- 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<HexStackView>();
- 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;i<Game.Cells.Count;i++)RenderCell(i);
- }
- void RenderCell(int i)
- {
- var cell=Game.Cells[i];var tiles=presenting?visibleTiles[i]:cell.tiles;bool locked=cell.need>VisibleScore;
- 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<int>(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<Image>();face.sprite=HexUI.Sprite(HexUI.Chips[color]);rt.Find("OriginalSymbol").GetComponent<Image>().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<count;i++)((RectTransform)rt.Find("Layer_"+i)).anchoredPosition=new Vector2(width/2,-height/2+i*depth);
- 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);
- var group=rt.GetComponent<CanvasGroup>();if(group==null)group=rt.gameObject.AddComponent<CanvasGroup>();
- 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<CanvasGroup>().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<s.count&&!Game.Over)
- {
- if(Paused){yield return null;continue;}
- while(launched<s.count&&time+0.0001f>=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<launched;i++)
- {
- if(landed[i])continue;float p=Mathf.Clamp01((time-i*stagger)/travel);MoveFlight(flights[i],origins[i],targets[i],p,collect);
- if(p>=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<int>(Game.Cells[s.from].tiles);
- visibleTiles[s.to]=new List<int>(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<HexFloatingFeedback>();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<HexFloatingFeedback>();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<HexFloatingFeedback>();pulse.owner=this;pulse.duration=.9f;pulse.reduced=Profile.Data.reduceMotion;
- }
- }
- }
- }
|