| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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<Layer> layers=new List<Layer>();readonly List<int> colors=new List<int>();readonly List<RunBadge> runs=new List<RunBadge>();
- 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<int> 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<int> data,bool immediate=false)
- {
- bool same=colors.Count==data.Count;for(int i=0;same&&i<data.Count;i++)same=colors[i]==data[i];if(same)return;
- int oldCount=colors.Count;colors.Clear();for(int i=0;i<data.Count;i++)colors.Add(data[i]);
- float dep=Depth(width,cap,colors.Count);topOffset=Mathf.Max(0,colors.Count-1)*dep;
- while(layers.Count<colors.Count)
- {
- var rt=HexUI.Node(content,"Tile_"+layers.Count,width/2,height/2,width,height);
- layers.Add(new Layer{rect=rt,face=HexUI.Image(rt,"Face","chip_red",width/2,height/2,width,height),symbol=HexUI.Image(rt,"OriginalSymbol","01_Cherries",width/2,height*.40f,width*.60f,width*.60f)});
- }
- for(int i=0;i<layers.Count;i++)
- {
- var layer=layers[i];layer.rect.gameObject.SetActive(i<colors.Count);if(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<Vector2Int>();for(int i=0;i<colors.Count-topCount;){int j=i+1;while(j<colors.Count-topCount&&colors[j]==colors[i])j++;segments.Add(new Vector2Int(colors[i],j-i));i=j;}
- HiddenRunCount=segments.Count;float bw=Mathf.Min(62,(width-8)/Mathf.Max(1,segments.Count)),bh=25;
- while(runs.Count<segments.Count)
- {
- var rt=HexUI.Node(transform,"BuriedRun_"+runs.Count,0,0,62,bh);HexUI.Image(rt,"Background","badge",31,bh/2,62,bh);
- runs.Add(new RunBadge{rect=rt,icon=HexUI.Image(rt,"OriginalSymbol","01_Cherries",15,bh/2,23,23),count=HexUI.Text(rt,"Count","",44,bh/2,29,24,17)});
- }
- for(int i=0;i<runs.Count;i++)
- {
- var marker=runs[i];marker.rect.gameObject.SetActive(i<segments.Count);if(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;i<colors.Count;i++){var rt=layers[i].rect;rt.anchoredPosition=new Vector2(width/2,Mathf.Lerp(rt.anchoredPosition.y,layers[i].y,1-Mathf.Exp(-dt*28)));}
- float lift=0;if(bump>0){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);
- }
- }
- }
|