using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; namespace HexaCrazy { // Persistent layers: changing a stack updates only its pooled images, never the board. public sealed class HexStackView : MonoBehaviour { sealed class Layer {public RectTransform rect;public Image face,symbol;public float y;} sealed class RunBadge {public RectTransform rect;public Image icon;public TextMeshProUGUI count;} readonly List layers=new List();readonly List colors=new List();readonly List runs=new List(); HexGameApp owner;RectTransform content,badge;TextMeshProUGUI countLabel;float width,height,cap,bump,topOffset; public int CellIndex {get;private set;} public int TileCount => colors.Count; public IReadOnlyList Colors => colors; public int HiddenRunCount {get;private set;} public void Initialize(HexGameApp app,int cell,float w,float h,float maxRise) { owner=app;CellIndex=cell;width=w;height=h;cap=maxRise; content=HexUI.Node(transform,"Layers",w/2,h/2,w,h); badge=HexUI.Node(transform,"TopCount",w/2,h*.76f,w*.41f,h*.24f); HexUI.Image(badge,"Badge","badge",w*.205f,h*.12f,w*.41f,h*.24f); countLabel=HexUI.Text(badge,"Count","",w*.205f,h*.12f,w*.4f,h*.24f,w*.18f); badge.gameObject.SetActive(false); } public static float Depth(float w,float maxRise,int count)=>Mathf.Min(w*.072f,maxRise/Mathf.Max(1,count-1)); public void SetTiles(IReadOnlyList data,bool immediate=false) { bool same=colors.Count==data.Count;for(int i=0;same&&i=colors.Count)continue; layer.face.sprite=HexUI.Sprite(HexUI.Chips[colors[i]]);layer.symbol.sprite=HexUI.Sprite(HexUI.Symbols[colors[i]]); float shade=1-Mathf.Min(colors.Count-1-i,5)*.06f;layer.face.color=layer.symbol.color=new Color(shade,shade,shade,1); layer.y=-height/2+i*dep; if(immediate||owner.Profile.Data.reduceMotion||i>=oldCount)layer.rect.anchoredPosition=new Vector2(width/2,layer.y); } badge.gameObject.SetActive(colors.Count>0);int n=0;if(colors.Count>0){int top=colors[colors.Count-1];for(int i=colors.Count-1;i>=0&&colors[i]==top;i--)n++;} countLabel.text=colors.Count==0?"":"×"+n; badge.anchoredPosition=new Vector2(width/2,-height*.76f+topOffset);badge.SetAsLastSibling(); DrawHiddenRuns(n); } void DrawHiddenRuns(int topCount) { var segments=new List();for(int i=0;i=segments.Count)continue; marker.rect.anchoredPosition=new Vector2(width/2+(i-(segments.Count-1)*.5f)*(bw+2),-height*.96f);marker.rect.localScale=Vector3.one*(bw/62); marker.icon.sprite=HexUI.Sprite(HexUI.Symbols[segments[i].x]);marker.count.text="×"+segments[i].y;marker.rect.SetAsLastSibling(); } } public Vector2 TileDesignCenter(int index) { var p=RectTransformUtility.WorldToScreenPoint(null,layers[index].rect.position);return owner.ScreenToDesign(p); } public void Bump(){bump=.24f;} void LateUpdate() { if(owner==null||owner.Paused)return;float dt=Time.unscaledDeltaTime; for(int i=0;i0){bump=Mathf.Max(0,bump-dt);lift=Mathf.Sin((1-bump/.24f)*Mathf.PI)*8;} content.anchoredPosition=new Vector2(width/2,-height/2+lift); if(colors.Count>0)badge.anchoredPosition=new Vector2(width/2,-height*.76f+layers[colors.Count-1].rect.anchoredPosition.y+height/2+lift); } } }