HexStackView.cs 5.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using TMPro;
  5. namespace HexaCrazy
  6. {
  7. // Persistent layers: changing a stack updates only its pooled images, never the board.
  8. public sealed class HexStackView : MonoBehaviour
  9. {
  10. sealed class Layer {public RectTransform rect;public Image face,symbol;public float y;}
  11. sealed class RunBadge {public RectTransform rect;public Image icon;public TextMeshProUGUI count;}
  12. readonly List<Layer> layers=new List<Layer>();readonly List<int> colors=new List<int>();readonly List<RunBadge> runs=new List<RunBadge>();
  13. HexGameApp owner;RectTransform content,badge;TextMeshProUGUI countLabel;float width,height,cap,bump,topOffset;
  14. public int CellIndex {get;private set;}
  15. public int TileCount => colors.Count;
  16. public IReadOnlyList<int> Colors => colors;
  17. public int HiddenRunCount {get;private set;}
  18. public void Initialize(HexGameApp app,int cell,float w,float h,float maxRise)
  19. {
  20. owner=app;CellIndex=cell;width=w;height=h;cap=maxRise;
  21. content=HexUI.Node(transform,"Layers",w/2,h/2,w,h);
  22. badge=HexUI.Node(transform,"TopCount",w/2,h*.76f,w*.41f,h*.24f);
  23. HexUI.Image(badge,"Badge","badge",w*.205f,h*.12f,w*.41f,h*.24f);
  24. countLabel=HexUI.Text(badge,"Count","",w*.205f,h*.12f,w*.4f,h*.24f,w*.18f);
  25. badge.gameObject.SetActive(false);
  26. }
  27. public static float Depth(float w,float maxRise,int count)=>Mathf.Min(w*.072f,maxRise/Mathf.Max(1,count-1));
  28. public void SetTiles(IReadOnlyList<int> data,bool immediate=false)
  29. {
  30. bool same=colors.Count==data.Count;for(int i=0;same&&i<data.Count;i++)same=colors[i]==data[i];if(same)return;
  31. int oldCount=colors.Count;colors.Clear();for(int i=0;i<data.Count;i++)colors.Add(data[i]);
  32. float dep=Depth(width,cap,colors.Count);topOffset=Mathf.Max(0,colors.Count-1)*dep;
  33. while(layers.Count<colors.Count)
  34. {
  35. var rt=HexUI.Node(content,"Tile_"+layers.Count,width/2,height/2,width,height);
  36. 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)});
  37. }
  38. for(int i=0;i<layers.Count;i++)
  39. {
  40. var layer=layers[i];layer.rect.gameObject.SetActive(i<colors.Count);if(i>=colors.Count)continue;
  41. layer.face.sprite=HexUI.Sprite(HexUI.Chips[colors[i]]);layer.symbol.sprite=HexUI.Sprite(HexUI.Symbols[colors[i]]);
  42. float shade=1-Mathf.Min(colors.Count-1-i,5)*.06f;layer.face.color=layer.symbol.color=new Color(shade,shade,shade,1);
  43. layer.y=-height/2+i*dep;
  44. if(immediate||owner.Profile.Data.reduceMotion||i>=oldCount)layer.rect.anchoredPosition=new Vector2(width/2,layer.y);
  45. }
  46. 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++;}
  47. countLabel.text=colors.Count==0?"":"×"+n;
  48. badge.anchoredPosition=new Vector2(width/2,-height*.76f+topOffset);badge.SetAsLastSibling();
  49. DrawHiddenRuns(n);
  50. }
  51. void DrawHiddenRuns(int topCount)
  52. {
  53. 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;}
  54. HiddenRunCount=segments.Count;float bw=Mathf.Min(62,(width-8)/Mathf.Max(1,segments.Count)),bh=25;
  55. while(runs.Count<segments.Count)
  56. {
  57. var rt=HexUI.Node(transform,"BuriedRun_"+runs.Count,0,0,62,bh);HexUI.Image(rt,"Background","badge",31,bh/2,62,bh);
  58. 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)});
  59. }
  60. for(int i=0;i<runs.Count;i++)
  61. {
  62. var marker=runs[i];marker.rect.gameObject.SetActive(i<segments.Count);if(i>=segments.Count)continue;
  63. marker.rect.anchoredPosition=new Vector2(width/2+(i-(segments.Count-1)*.5f)*(bw+2),-height*.96f);marker.rect.localScale=Vector3.one*(bw/62);
  64. marker.icon.sprite=HexUI.Sprite(HexUI.Symbols[segments[i].x]);marker.count.text="×"+segments[i].y;marker.rect.SetAsLastSibling();
  65. }
  66. }
  67. public Vector2 TileDesignCenter(int index)
  68. {
  69. var p=RectTransformUtility.WorldToScreenPoint(null,layers[index].rect.position);return owner.ScreenToDesign(p);
  70. }
  71. public void Bump(){bump=.24f;}
  72. void LateUpdate()
  73. {
  74. if(owner==null||owner.Paused)return;float dt=Time.unscaledDeltaTime;
  75. 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)));}
  76. float lift=0;if(bump>0){bump=Mathf.Max(0,bump-dt);lift=Mathf.Sin((1-bump/.24f)*Mathf.PI)*8;}
  77. content.anchoredPosition=new Vector2(width/2,-height/2+lift);
  78. if(colors.Count>0)badge.anchoredPosition=new Vector2(width/2,-height*.76f+layers[colors.Count-1].rect.anchoredPosition.y+height/2+lift);
  79. }
  80. }
  81. }