HexFeedback.cs 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using TMPro;
  4. namespace HexaCrazy
  5. {
  6. public static class HexEase
  7. {
  8. public static float Bezier(float x,float x1,float y1,float x2,float y2)
  9. {
  10. if(x<=0)return 0;if(x>=1)return 1;float lo=0,hi=1,t=0;
  11. for(int i=0;i<12;i++){t=(lo+hi)*.5f;float u=1-t;if(3*u*u*t*x1+3*u*t*t*x2+t*t*t<x)lo=t;else hi=t;}
  12. float v=1-t;return 3*v*v*t*y1+3*v*t*t*y2+t*t*t;
  13. }
  14. }
  15. [RequireComponent(typeof(CanvasRenderer))]
  16. public sealed class HexRadial : MaskableGraphic
  17. {
  18. public bool Ring;
  19. protected override void OnPopulateMesh(VertexHelper mesh)
  20. {
  21. mesh.Clear();const int segments=40,bands=6;var rect=rectTransform.rect;float radius=Mathf.Min(rect.width,rect.height)*.5f;
  22. for(int b=0;b<=bands;b++)
  23. {
  24. float r=b/(float)bands;float a=Ring?Mathf.Max(0,1-Mathf.Abs(r-.78f)/.22f):Mathf.Pow(1-r,1.6f);
  25. var tint=color;tint.a*=a;
  26. for(int i=0;i<=segments;i++){float angle=i*Mathf.PI*2/segments;mesh.AddVert(new Vector3(Mathf.Cos(angle),Mathf.Sin(angle))*radius*r,tint,Vector2.zero);}
  27. }
  28. for(int b=0;b<bands;b++)for(int i=0;i<segments;i++){int j=b*(segments+1)+i;mesh.AddTriangle(j,j+1,j+segments+1);mesh.AddTriangle(j+1,j+segments+2,j+segments+1);}
  29. }
  30. public static HexRadial Create(Transform p,string name,float x,float y,float size,bool ring=false)
  31. {var graphic=HexUI.Node(p,name,x,y,size,size).gameObject.AddComponent<HexRadial>();graphic.raycastTarget=false;graphic.Ring=ring;graphic.color=HexUI.Gold;return graphic;}
  32. }
  33. public sealed class HexFloatingFeedback : MonoBehaviour
  34. {
  35. public HexGameApp owner;public float duration=.8f;public bool ring,reduced;
  36. RectTransform rect;CanvasGroup group;Vector2 origin;float time;
  37. void Awake(){rect=(RectTransform)transform;origin=rect.anchoredPosition;group=gameObject.AddComponent<CanvasGroup>();group.blocksRaycasts=false;group.alpha=0;}
  38. void Update()
  39. {
  40. if(owner==null||owner.Paused)return;time+=Time.unscaledDeltaTime;float p=Mathf.Clamp01(time/duration);
  41. if(ring){rect.localScale=Vector3.one*Mathf.Lerp(.4f,6.4f,1-Mathf.Pow(1-p,2));group.alpha=(1-p)*.95f;}
  42. else
  43. {
  44. group.alpha=p<.22f?p/.22f:1-(p-.22f)/.78f;
  45. if(!reduced){rect.anchoredPosition=origin+Vector2.up*(55*p);rect.localScale=Vector3.one*(p<.22f?Mathf.Lerp(.7f,1.12f,p/.22f):Mathf.Lerp(1.12f,1,(p-.22f)/.78f));}
  46. }
  47. if(p>=1)Destroy(gameObject);
  48. }
  49. }
  50. public sealed class HexScoreBar : MonoBehaviour
  51. {
  52. HexGameApp owner;HexProgress fill;TextMeshProUGUI label;RectTransform head;float shown,start,elapsed=1,clock,hit;int target,limit;
  53. public float DisplayedScore=>shown;public int TargetScore=>target;public bool IsAnimating=>elapsed<.5f;
  54. public void Initialize(HexGameApp app,HexProgress progress,TextMeshProUGUI score,int max)
  55. {
  56. owner=app;fill=progress;label=score;limit=max;fill.FeedbackStyle=true;
  57. for(int i=1;i<=3;i++)HexUI.Image(transform,"QuarterMark"+i,null,8+330*i/4f,14,1.4f,15,new Color(.18f,.05f,.01f,.4f));
  58. head=HexUI.Node(transform,"ProgressHead",8,14,30,30);HexRadial.Create(head,"Glow",15,15,30);var core=HexRadial.Create(head,"Core",15,15,16);core.color=HexUI.Cream;
  59. SetScore(0,true);Refresh();
  60. }
  61. public void SetScore(int score,bool immediate=false,bool pulse=false)
  62. {
  63. if(score!=target){target=score;start=shown;elapsed=0;}
  64. if(immediate){shown=target;elapsed=.5f;}
  65. if(pulse)hit=.62f;Refresh();
  66. }
  67. public Vector2 HeadDesignPosition => owner.ScreenToDesign(RectTransformUtility.WorldToScreenPoint(null,head.position));
  68. void Update()
  69. {
  70. if(owner==null||owner.Paused)return;float dt=Time.unscaledDeltaTime;elapsed=Mathf.Min(.5f,elapsed+dt);clock+=dt;hit=Mathf.Max(0,hit-dt);
  71. shown=Mathf.Lerp(start,target,HexEase.Bezier(elapsed/.5f,.25f,.9f,.25f,1));Refresh();
  72. }
  73. void Refresh()
  74. {
  75. float fraction=Mathf.Clamp01(shown/Mathf.Max(1,limit));fill.Value=fraction;
  76. fill.Shine=owner.Profile.Data.reduceMotion?-1:((clock%2.1f)/2.1f/.55f)*1.5f-.45f;
  77. float flash=owner.Profile.Data.reduceMotion?0:Mathf.Sin((1-hit/.62f)*Mathf.PI)*Mathf.Clamp01(hit/.25f);
  78. fill.Flash=flash;transform.localScale=Vector3.one*(1+.035f*flash);
  79. head.gameObject.SetActive(fraction>.002f);head.anchoredPosition=new Vector2(8+330*fraction,-14);
  80. head.localScale=Vector3.one*(owner.Profile.Data.reduceMotion?1:1+.15f*Mathf.Sin(clock*Mathf.PI*2/1.3f)+flash*.35f);
  81. label.text=Mathf.RoundToInt(shown)+" / "+limit;
  82. }
  83. }
  84. }