| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- namespace HexaCrazy
- {
- public static class HexEase
- {
- public static float Bezier(float x,float x1,float y1,float x2,float y2)
- {
- if(x<=0)return 0;if(x>=1)return 1;float lo=0,hi=1,t=0;
- 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;}
- float v=1-t;return 3*v*v*t*y1+3*v*t*t*y2+t*t*t;
- }
- }
- [RequireComponent(typeof(CanvasRenderer))]
- public sealed class HexRadial : MaskableGraphic
- {
- public bool Ring;
- protected override void OnPopulateMesh(VertexHelper mesh)
- {
- mesh.Clear();const int segments=40,bands=6;var rect=rectTransform.rect;float radius=Mathf.Min(rect.width,rect.height)*.5f;
- for(int b=0;b<=bands;b++)
- {
- float r=b/(float)bands;float a=Ring?Mathf.Max(0,1-Mathf.Abs(r-.78f)/.22f):Mathf.Pow(1-r,1.6f);
- var tint=color;tint.a*=a;
- 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);}
- }
- 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);}
- }
- public static HexRadial Create(Transform p,string name,float x,float y,float size,bool ring=false)
- {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;}
- }
- public sealed class HexFloatingFeedback : MonoBehaviour
- {
- public HexGameApp owner;public float duration=.8f;public bool ring,reduced;
- RectTransform rect;CanvasGroup group;Vector2 origin;float time;
- void Awake(){rect=(RectTransform)transform;origin=rect.anchoredPosition;group=gameObject.AddComponent<CanvasGroup>();group.blocksRaycasts=false;group.alpha=0;}
- void Update()
- {
- if(owner==null||owner.Paused)return;time+=Time.unscaledDeltaTime;float p=Mathf.Clamp01(time/duration);
- if(ring){rect.localScale=Vector3.one*Mathf.Lerp(.4f,6.4f,1-Mathf.Pow(1-p,2));group.alpha=(1-p)*.95f;}
- else
- {
- group.alpha=p<.22f?p/.22f:1-(p-.22f)/.78f;
- 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));}
- }
- if(p>=1)Destroy(gameObject);
- }
- }
- public sealed class HexScoreBar : MonoBehaviour
- {
- HexGameApp owner;HexProgress fill;TextMeshProUGUI label;RectTransform head;float shown,start,elapsed=1,clock,hit;int target,limit;
- public float DisplayedScore=>shown;public int TargetScore=>target;public bool IsAnimating=>elapsed<.5f;
- public void Initialize(HexGameApp app,HexProgress progress,TextMeshProUGUI score,int max)
- {
- owner=app;fill=progress;label=score;limit=max;fill.FeedbackStyle=true;
- 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));
- 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;
- SetScore(0,true);Refresh();
- }
- public void SetScore(int score,bool immediate=false,bool pulse=false)
- {
- if(score!=target){target=score;start=shown;elapsed=0;}
- if(immediate){shown=target;elapsed=.5f;}
- if(pulse)hit=.62f;Refresh();
- }
- public Vector2 HeadDesignPosition => owner.ScreenToDesign(RectTransformUtility.WorldToScreenPoint(null,head.position));
- void Update()
- {
- if(owner==null||owner.Paused)return;float dt=Time.unscaledDeltaTime;elapsed=Mathf.Min(.5f,elapsed+dt);clock+=dt;hit=Mathf.Max(0,hit-dt);
- shown=Mathf.Lerp(start,target,HexEase.Bezier(elapsed/.5f,.25f,.9f,.25f,1));Refresh();
- }
- void Refresh()
- {
- float fraction=Mathf.Clamp01(shown/Mathf.Max(1,limit));fill.Value=fraction;
- fill.Shine=owner.Profile.Data.reduceMotion?-1:((clock%2.1f)/2.1f/.55f)*1.5f-.45f;
- float flash=owner.Profile.Data.reduceMotion?0:Mathf.Sin((1-hit/.62f)*Mathf.PI)*Mathf.Clamp01(hit/.25f);
- fill.Flash=flash;transform.localScale=Vector3.one*(1+.035f*flash);
- head.gameObject.SetActive(fraction>.002f);head.anchoredPosition=new Vector2(8+330*fraction,-14);
- head.localScale=Vector3.one*(owner.Profile.Data.reduceMotion?1:1+.15f*Mathf.Sin(clock*Mathf.PI*2/1.3f)+flash*.35f);
- label.text=Mathf.RoundToInt(shown)+" / "+limit;
- }
- }
- }
|