ScoreStreaks.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace Fateforge
  4. {
  5. // Deterministic presentation geometry; driven by the settlement clock, never gameplay RNG.
  6. [RequireComponent(typeof(RectTransform),typeof(CanvasRenderer))]
  7. public sealed class ScoreStreaks : MaskableGraphic
  8. {
  9. const int Count=78;
  10. readonly Vector2[] origins=new Vector2[Count];
  11. readonly Vector2[] controls=new Vector2[Count];
  12. readonly Vector2[] destinations=new Vector2[Count];
  13. readonly float[] delays=new float[Count],durations=new float[Count],tails=new float[Count];
  14. float progress;
  15. bool configured;
  16. protected override void Awake(){base.Awake();raycastTarget=false;}
  17. public void Configure(Vector2[] starts,Vector2 end)
  18. {
  19. raycastTarget=false;progress=0;configured=starts!=null&&starts.Length>0;
  20. if(configured)for(int i=0;i<Count;i++){
  21. Vector2 start=starts[i%starts.Length];float distance=Vector2.Distance(start,end);
  22. origins[i]=start+new Vector2((Noise(i,1)-.5f)*30,(Noise(i,2)-.5f)*18);
  23. destinations[i]=end+new Vector2((Noise(i,3)-.5f)*26,(Noise(i,4)-.5f)*12);
  24. float side=Mathf.Sign(start.x-end.x);if(side==0)side=Noise(i,5)>.5f?1:-1;
  25. controls[i]=Vector2.Lerp(origins[i],destinations[i],.48f)+new Vector2(side*(22+Noise(i,6)*Mathf.Min(150,distance*.3f)),30+Noise(i,7)*Mathf.Min(100,distance*.22f));
  26. delays[i]=Noise(i,8)*.21f;durations[i]=.55f+Noise(i,9)*.12f;tails[i]=.04f+Noise(i,10)*.04f;
  27. }
  28. SetVerticesDirty();
  29. }
  30. public void Sample(float value)
  31. {progress=Mathf.Clamp01(value);SetVerticesDirty();}
  32. protected override void OnPopulateMesh(VertexHelper mesh)
  33. {
  34. mesh.Clear();if(!configured||progress<=0||progress>=1)return;
  35. for(int i=0;i<Count;i++){
  36. float age=(progress-delays[i])/durations[i];if(age<=0||age>=1)continue;
  37. float head=1-Mathf.Pow(1-age,1.45f),tail=Mathf.Max(0,head-tails[i]);
  38. Vector2 a=Arc(origins[i],controls[i],destinations[i],tail),b=Arc(origins[i],controls[i],destinations[i],head);
  39. float opacity=Mathf.Clamp01(age*14)*Mathf.Clamp01((1-age)*8)*color.a;
  40. Stroke(mesh,a,b,5.5f,new Color(1,.50f,.08f,opacity*.08f),new Color(1,.65f,.14f,opacity*.15f));
  41. Stroke(mesh,a,b,3.8f,new Color(1,.65f,.12f,opacity*.03f),new Color(1,.79f,.32f,opacity*.24f));
  42. Stroke(mesh,a,b,1.3f,new Color(1,.73f,.18f,opacity*.10f),new Color(1,.95f,.72f,opacity));
  43. }
  44. }
  45. static Vector2 Arc(Vector2 start,Vector2 control,Vector2 end,float t)
  46. {float inverse=1-t;return inverse*inverse*start+2*inverse*t*control+t*t*end;}
  47. static void Stroke(VertexHelper mesh,Vector2 tail,Vector2 head,float width,Color tailColor,Color headColor)
  48. {
  49. var delta=head-tail;if(delta.sqrMagnitude<.01f)return;
  50. Vector2 normal=new Vector2(-delta.y,delta.x).normalized*width*.5f;int first=mesh.currentVertCount;
  51. mesh.AddVert(tail-normal,tailColor,Vector2.zero);mesh.AddVert(tail+normal,tailColor,Vector2.zero);
  52. mesh.AddVert(head+normal,headColor,Vector2.zero);mesh.AddVert(head-normal,headColor,Vector2.zero);
  53. mesh.AddTriangle(first,first+1,first+2);mesh.AddTriangle(first,first+2,first+3);
  54. }
  55. static float Noise(int index,int salt)
  56. {
  57. unchecked{uint value=(uint)(index*747796405+salt*2891336453L);value=((value>>((int)(value>>28)+4))^value)*277803737u;value=(value>>22)^value;return (value&0x00ffffff)/16777215f;}
  58. }
  59. }
  60. }